Skip to content

Commit 02b9957

Browse files
committed
不依赖外部网站访问进行安装
1 parent d3c6a85 commit 02b9957

File tree

14 files changed

+537
-199
lines changed

14 files changed

+537
-199
lines changed

src/app/service/service_worker/script.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ import { type ResourceService } from "./resource";
2828
import { type ValueService } from "./value";
2929
import { compileScriptCode, isEarlyStartScript } from "../content/utils";
3030
import { type SystemConfig } from "@App/pkg/config/config";
31-
import { localePath } from "@App/locales/locales";
3231
import { arrayMove } from "@dnd-kit/sortable";
33-
import { DocumentationSite } from "@App/app/const";
3432
import type {
3533
TScriptRunStatus,
3634
TDeleteScript,
@@ -81,12 +79,13 @@ export class ScriptService {
8179

8280
listenerScriptInstall() {
8381
// 初始化脚本安装监听
84-
chrome.webRequest.onBeforeRequest.addListener(
85-
(req: chrome.webRequest.OnBeforeRequestDetails) => {
86-
// 处理url, 实现安装脚本
87-
if (req.method !== "GET") {
88-
return undefined;
82+
chrome.webNavigation.onBeforeNavigate.addListener(
83+
(req: chrome.webNavigation.WebNavigationParentedCallbackDetails) => {
84+
const lastError = chrome.runtime.lastError;
85+
if (lastError) {
86+
console.error(lastError.message);
8987
}
88+
// 处理url, 实现安装脚本
9089
let targetUrl: string | null = null;
9190
// 判断是否为 file:///*/*.user.js
9291
if (req.url.startsWith("file://") && req.url.endsWith(".user.js")) {
@@ -150,13 +149,12 @@ export class ScriptService {
150149
});
151150
},
152151
{
153-
urls: [
154-
`${DocumentationSite}/docs/script_installation/*`,
155-
`${DocumentationSite}/en/docs/script_installation/*`,
156-
"https://www.tampermonkey.net/script_installation.php*",
157-
"file:///*/*.user.js*",
152+
url: [
153+
{ schemes: ["http", "https"], hostEquals: "docs.scriptcat.org", pathPrefix: "/docs/script_installation/" },
154+
{ schemes: ["http", "https"], hostEquals: "docs.scriptcat.org", pathPrefix: "/en/docs/script_installation/" },
155+
{ schemes: ["http", "https"], hostEquals: "www.tampermonkey.net", pathPrefix: "/script_installation.php" },
156+
{ schemes: ["file"], pathSuffix: ".user.js" },
158157
],
159-
types: ["main_frame"],
160158
}
161159
);
162160
// 兼容 chrome 内核 < 128 处理
@@ -187,7 +185,7 @@ export class ScriptService {
187185
action: {
188186
type: "redirect" as chrome.declarativeNetRequest.RuleActionType,
189187
redirect: {
190-
regexSubstitution: `${DocumentationSite}${localePath}/docs/script_installation/#url=\\0`,
188+
regexSubstitution: `chrome-extension://${chrome.runtime.id}/src/install.html?url=\\0`,
191189
},
192190
},
193191
condition: condition,
@@ -206,6 +204,18 @@ export class ScriptService {
206204
}
207205

208206
public async openInstallPageByUrl(url: string, source: InstallSource): Promise<{ success: boolean; msg: string }> {
207+
try {
208+
const installPageUrl = await this.getInstallPageUrl(url, source);
209+
if (!installPageUrl) throw new Error("getInstallPageUrl failed");
210+
await openInCurrentTab(installPageUrl);
211+
return { success: true, msg: "" };
212+
} catch (err: any) {
213+
console.error(err);
214+
return { success: false, msg: err.message };
215+
}
216+
}
217+
218+
public async getInstallPageUrl(url: string, source: InstallSource): Promise<string> {
209219
const uuid = uuidv4();
210220
try {
211221
await this.openUpdateOrInstallPage(uuid, url, source, false);
@@ -217,11 +227,10 @@ export class ScriptService {
217227
},
218228
30 * 1000
219229
);
220-
await openInCurrentTab(`/src/install.html?uuid=${uuid}`);
221-
return { success: true, msg: "" };
230+
return `/src/install.html?uuid=${uuid}`;
222231
} catch (err: any) {
223232
console.error(err);
224-
return { success: false, msg: err.message };
233+
return "";
225234
}
226235
}
227236

src/locales/ach-UG/translation.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
"script_requires": "crwdns8440:0crwdne8440:0",
283283
"cookie_warning": "crwdns8442:0crwdne8442:0",
284284
"scheduled_script_description_title": "crwdns8706:0crwdne8706:0",
285-
"scheduled_script_description_description": "crwdns8708:0{{expression}}crwdnd8708:0{{time}}crwdne8708:0",
285+
"scheduled_script_description_description_expr": "crwdns8708:0",
286+
"scheduled_script_description_description_next": "crwdns8708:0",
286287
"background_script_description": "crwdns8444:0crwdne8444:0",
287288
"install_success": "crwdns8446:0crwdne8446:0",
288289
"install": {
@@ -322,6 +323,8 @@
322323
"status_autoclose": "crwdns12778:0crwdne12778:0",
323324
"header_other_update": "crwdns12784:0crwdne12784:0"
324325
},
326+
"install_page_loading": "Installation page loading",
327+
"invalid_page": "Invalid page",
325328
"background_script_tag": "crwdns8460:0crwdne8460:0",
326329
"scheduled_script_tag": "crwdns8462:0crwdne8462:0",
327330
"background_script": "crwdns8464:0crwdne8464:0",

src/locales/de-DE/translation.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
"script_requires": "Skript referenziert die folgenden externen Ressourcen",
283283
"cookie_warning": "Achtung: Dieses Skript beantragt Cookie-Operationsberechtigung. Dies ist eine gefährliche Berechtigung, bitte stellen Sie die Sicherheit des Skripts sicher.",
284284
"scheduled_script_description_title": "Dies ist ein geplantes Skript. Wenn aktiviert, wird es zu bestimmten Zeiten automatisch ausgeführt und kann im Panel manuell gesteuert werden.",
285-
"scheduled_script_description_description": "Geplante Aufgaben-Ausdruck: {{expression}}, letzte Ausführungszeit: {{time}}",
285+
"scheduled_script_description_description_expr": "Geplante Aufgaben-Ausdruck",
286+
"scheduled_script_description_description_next": "Letzte Ausführungszeit",
286287
"background_script_description": "Dies ist ein Hintergrundskript. Wenn aktiviert, wird es automatisch einmal ausgeführt, wenn der Browser geöffnet wird, und kann im Panel manuell gesteuert werden.",
287288
"install_success": "Installation erfolgreich",
288289
"install": {
@@ -322,6 +323,8 @@
322323
"status_autoclose": "Automatisches Schließen in $0 Sekunden",
323324
"header_other_update": "Andere verfügbare Updates"
324325
},
326+
"install_page_loading": "Installationsseite wird geladen",
327+
"invalid_page": "Ungültige Seite",
325328
"background_script_tag": "Dies ist ein Hintergrundskript",
326329
"scheduled_script_tag": "Dies ist ein geplantes Skript",
327330
"background_script": "Hintergrundskript",

src/locales/en-US/translation.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
"script_requires": "Script requires the following external resources",
283283
"cookie_warning": "Please note, this script requests access to Cookie permissions, which is a dangerous permission. Please verify the security of the script.",
284284
"scheduled_script_description_title": "This is a scheduled script, which will automatically run at a specific time once enabled and can be manually controlled in the panel.",
285-
"scheduled_script_description_description": "Scheduled task expression: {{expression}}, most recent run time: {{time}}",
285+
"scheduled_script_description_description_expr": "Scheduled task expression:",
286+
"scheduled_script_description_description_next": "Most recent run time:",
286287
"background_script_description": "This is a background script, which will automatically run once when the browser opens once enabled, and can be manually controlled in the panel.",
287288
"install_success": "Install Successful",
288289
"install": {
@@ -322,6 +323,8 @@
322323
"status_autoclose": "Auto-closing in $0 seconds",
323324
"header_other_update": "Other available updates"
324325
},
326+
"install_page_loading": "Installation page loading",
327+
"invalid_page": "Invalid page",
325328
"background_script_tag": "This is a Background Script",
326329
"scheduled_script_tag": "This is a Scheduled Script",
327330
"background_script": "Background Script",

src/locales/ja-JP/translation.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
"script_requires": "スクリプトは以下の外部リソースを参照しています",
283283
"cookie_warning": "注意:このスクリプトはCookieの操作権限を申請します。これは危険な権限ですので、スクリプトの安全性を確認してください。",
284284
"scheduled_script_description_title": "これはスケジュールスクリプトです。有効にすると特定の時間に自動実行され、手動操作も可能です。",
285-
"scheduled_script_description_description": "スケジュールタスク式:{{expression}}、最近の実行時間:{{time}}",
285+
"scheduled_script_description_description_expr": "スケジュールタスク表現:",
286+
"scheduled_script_description_description_next": "最近の実行時間:",
286287
"background_script_description": "これはバックグラウンドスクリプトです。有効にするとブラウザを開いたときに自動的に一度実行され、パネルで手動制御できます。",
287288
"install_success": "インストールに成功しました",
288289
"install": {
@@ -322,6 +323,8 @@
322323
"status_autoclose": "$0 秒後に自動的に閉じます",
323324
"header_other_update": "その他の利用可能な更新"
324325
},
326+
"install_page_loading": "インストールページを読み込み中",
327+
"invalid_page": "無効なページ",
325328
"background_script_tag": "これはバックグラウンドスクリプトです",
326329
"scheduled_script_tag": "これはスケジュールスクリプトです",
327330
"background_script": "バックグラウンドスクリプト",

src/locales/ru-RU/translation.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
"script_requires": "Скрипт ссылается на следующие внешние ресурсы",
283283
"cookie_warning": "Обратите внимание, что этот скрипт запрашивает разрешения на операции с Cookie. Это опасное разрешение, пожалуйста, убедитесь в безопасности скрипта.",
284284
"scheduled_script_description_title": "Это запланированный скрипт. После включения он будет автоматически выполняться в определенное время и может управляться вручную с панели.",
285-
"scheduled_script_description_description": "Выражение планировщика: {{expression}}, последнее время выполнения: {{time}}",
285+
"scheduled_script_description_description_expr": "Выражение планировщика",
286+
"scheduled_script_description_description_next": "Последнее время выполнения:",
286287
"background_script_description": "Это фоновый скрипт. После включения он будет автоматически выполняться один раз при открытии браузера и может управляться вручную с панели.",
287288
"install_success": "Установка успешна",
288289
"install": {
@@ -322,6 +323,8 @@
322323
"status_autoclose": "Автоматическое закрытие через $0 сек.",
323324
"header_other_update": "Другие доступные обновления"
324325
},
326+
"install_page_loading": "Загрузка страницы установки",
327+
"invalid_page": "Недействительная страница",
325328
"background_script_tag": "Это фоновый скрипт",
326329
"scheduled_script_tag": "Это запланированный скрипт",
327330
"background_script": "Фоновый скрипт",

src/locales/vi-VN/translation.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
"script_requires": "Script yêu cầu các tài nguyên bên ngoài sau",
283283
"cookie_warning": "Xin lưu ý, script này yêu cầu quyền truy cập cookie, đây là một quyền nguy hiểm. Vui lòng xác minh tính bảo mật của script.",
284284
"scheduled_script_description_title": "Đây là script hẹn giờ, sẽ tự động chạy vào một thời điểm cụ thể sau khi được bật và có thể được điều khiển thủ công trong bảng điều khiển.",
285-
"scheduled_script_description_description": "Biểu thức tác vụ hẹn giờ: {{expression}}, thời gian chạy gần nhất: {{time}}",
285+
"scheduled_script_description_description_expr": "Biểu thức tác vụ hẹn giờ:",
286+
"scheduled_script_description_description_next": "Thời gian chạy gần nhất:",
286287
"background_script_description": "Đây là script nền, sẽ tự động chạy một lần khi trình duyệt mở sau khi được bật và có thể được điều khiển thủ công trong bảng điều khiển.",
287288
"install_success": "Cài đặt thành công",
288289
"install": {
@@ -322,6 +323,8 @@
322323
"status_autoclose": "Tự đóng trong $0 giây",
323324
"header_other_update": "Các bản cập nhật khác"
324325
},
326+
"install_page_loading": "Đang tải trang cài đặt",
327+
"invalid_page": "Trang không hợp lệ",
325328
"background_script_tag": "Đây là một script nền",
326329
"scheduled_script_tag": "Đây là một script hẹn giờ",
327330
"background_script": "Script nền",

src/locales/zh-CN/translation.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
"script_requires": "脚本引用了下列外部资源",
283283
"cookie_warning": "请注意,本脚本会申请 Cookie 的操作权限,这是一个危险的权限,请确认脚本的安全性。",
284284
"scheduled_script_description_title": "这是一个定时脚本,启用后将在特定时间自动运行,并可在面板中手动控制。",
285-
"scheduled_script_description_description": "定时任务表达式:{{expression}},最近一次运行时间:{{time}}",
285+
"scheduled_script_description_description_expr": "定时任务表达式:",
286+
"scheduled_script_description_description_next": "最近一次运行时间:",
286287
"background_script_description": "这是一个后台脚本,启用后将在浏览器打开时自动运行一次,并可在面板中手动控制。",
287288
"install_success": "安装成功",
288289
"install": {
@@ -322,6 +323,8 @@
322323
"status_autoclose": "$0 秒后自动关闭",
323324
"header_other_update": "其他可用更新"
324325
},
326+
"install_page_loading": "安装页面加载中",
327+
"invalid_page": "无效页面",
325328
"background_script_tag": "这是一个后台脚本",
326329
"scheduled_script_tag": "这是一个定时脚本",
327330
"background_script": "后台脚本",

src/locales/zh-TW/translation.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@
282282
"script_requires": "腳本引用了以下外部資源",
283283
"cookie_warning": "請注意,此腳本會要求 Cookie 的操作權限,這是一項危險的權限,請確認腳本的安全性。",
284284
"scheduled_script_description_title": "這是一個排程腳本,啟用後將在特定時間自動執行,並可在控制面板中手動控制。",
285-
"scheduled_script_description_description": "排程任務表達式:{{expression}},最近一次執行時間:{{time}}",
285+
"scheduled_script_description_description_expr": "排程任務表達式:",
286+
"scheduled_script_description_description_next": "最近一次執行時間:",
286287
"background_script_description": "這是一個背景腳本,啟用後將在瀏覽器開啟時自動執行一次,並可在控制面板中手動控制。",
287288
"install_success": "安裝成功",
288289
"install": {
@@ -322,6 +323,8 @@
322323
"status_autoclose": "$0 秒後自動關閉",
323324
"header_other_update": "其他可用更新"
324325
},
326+
"install_page_loading": "安裝頁載入中",
327+
"invalid_page": "無效頁面",
325328
"background_script_tag": "這是一個背景腳本",
326329
"scheduled_script_tag": "這是一個排程腳本",
327330
"background_script": "背景腳本",

src/manifest.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"notifications",
4343
"clipboardWrite",
4444
"unlimitedStorage",
45-
"declarativeNetRequest"
45+
"declarativeNetRequest",
46+
"webNavigation"
4647
],
4748
"optional_permissions": [
4849
"userScripts"
@@ -54,5 +55,11 @@
5455
"pages": [
5556
"src/sandbox.html"
5657
]
57-
}
58+
},
59+
"web_accessible_resources": [
60+
{
61+
"resources": ["/src/install.html"],
62+
"matches": ["<all_urls>"]
63+
}
64+
]
5865
}

0 commit comments

Comments
 (0)