@@ -28,9 +28,7 @@ import { type ResourceService } from "./resource";
2828import { type ValueService } from "./value" ;
2929import { compileScriptCode , isEarlyStartScript } from "../content/utils" ;
3030import { type SystemConfig } from "@App/pkg/config/config" ;
31- import { localePath } from "@App/locales/locales" ;
3231import { arrayMove } from "@dnd-kit/sortable" ;
33- import { DocumentationSite } from "@App/app/const" ;
3432import type {
3533 TScriptRunStatus ,
3634 TDeleteScript ,
@@ -80,13 +78,35 @@ export class ScriptService {
8078 }
8179
8280 listenerScriptInstall ( ) {
81+ const handleIncomingUrl = async ( targetUrl : string ) => {
82+ const url = await this . getInstallPageUrl ( targetUrl , "user" ) ;
83+ if ( ! url ) throw new Error ( "getInstallPageUrl failed" ) ;
84+ return url ;
85+ } ;
86+
87+ // Runs in MV3 background (service worker)
88+ chrome . runtime . onMessage . addListener ( ( msg , sender , sendResponse ) => {
89+ switch ( msg . type ) {
90+ case "INSTALL_PAGE:SUBMIT_URL" : {
91+ // Do work (can be async)
92+ handleIncomingUrl ( msg . url )
93+ . then ( ( result ) => sendResponse ( { ok : true , result } ) )
94+ . catch ( ( err ) => sendResponse ( { ok : false , error : String ( err ) } ) ) ;
95+ return true ; // <-- keep the message channel open for async sendResponse
96+ }
97+ default :
98+ // ignore
99+ }
100+ } ) ;
101+
83102 // 初始化脚本安装监听
84- chrome . webRequest . onBeforeRequest . addListener (
85- ( req : chrome . webRequest . OnBeforeRequestDetails ) => {
86- // 处理url, 实现安装脚本
87- if ( req . method !== "GET" ) {
88- return undefined ;
103+ chrome . webNavigation . onBeforeNavigate . addListener (
104+ ( req : chrome . webNavigation . WebNavigationParentedCallbackDetails ) => {
105+ const lastError = chrome . runtime . lastError ;
106+ if ( lastError ) {
107+ console . error ( lastError . message ) ;
89108 }
109+ // 处理url, 实现安装脚本
90110 let targetUrl : string | null = null ;
91111 // 判断是否为 file:///*/*.user.js
92112 if ( req . url . startsWith ( "file://" ) && req . url . endsWith ( ".user.js" ) ) {
@@ -150,13 +170,16 @@ export class ScriptService {
150170 } ) ;
151171 } ,
152172 {
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*" ,
173+ url : [
174+ // { hostEquals: "docs.scriptcat.org", pathPrefix: "/docs/script_installation/" },
175+ // { hostEquals: "docs.scriptcat.org", pathPrefix: "/en/docs/script_installation/" },
176+ { hostEquals : "www.tampermonkey.net" , pathPrefix : "/script_installation.php" } ,
177+ { schemes : [ "file" ] , pathSuffix : ".user.js" } ,
178+ {
179+ hostEquals : chrome . runtime . id ,
180+ pathPrefix : "/src/script_installation_page.html" ,
181+ } ,
158182 ] ,
159- types : [ "main_frame" ] ,
160183 }
161184 ) ;
162185 // 兼容 chrome 内核 < 128 处理
@@ -187,7 +210,7 @@ export class ScriptService {
187210 action : {
188211 type : "redirect" as chrome . declarativeNetRequest . RuleActionType ,
189212 redirect : {
190- regexSubstitution : `${ DocumentationSite } ${ localePath } /docs/script_installation/# url=\\0` ,
213+ regexSubstitution : `chrome-extension:// ${ chrome . runtime . id } /src/script_installation_page.html? url=\\0` ,
191214 } ,
192215 } ,
193216 condition : condition ,
@@ -206,6 +229,18 @@ export class ScriptService {
206229 }
207230
208231 public async openInstallPageByUrl ( url : string , source : InstallSource ) : Promise < { success : boolean ; msg : string } > {
232+ try {
233+ const installPageUrl = await this . getInstallPageUrl ( url , source ) ;
234+ if ( ! installPageUrl ) throw new Error ( "getInstallPageUrl failed" ) ;
235+ await openInCurrentTab ( installPageUrl ) ;
236+ return { success : true , msg : "" } ;
237+ } catch ( err : any ) {
238+ console . error ( err ) ;
239+ return { success : false , msg : err . message } ;
240+ }
241+ }
242+
243+ public async getInstallPageUrl ( url : string , source : InstallSource ) : Promise < string > {
209244 const uuid = uuidv4 ( ) ;
210245 try {
211246 await this . openUpdateOrInstallPage ( uuid , url , source , false ) ;
@@ -217,11 +252,10 @@ export class ScriptService {
217252 } ,
218253 30 * 1000
219254 ) ;
220- await openInCurrentTab ( `/src/install.html?uuid=${ uuid } ` ) ;
221- return { success : true , msg : "" } ;
255+ return `/src/install.html?uuid=${ uuid } ` ;
222256 } catch ( err : any ) {
223257 console . error ( err ) ;
224- return { success : false , msg : err . message } ;
258+ return "" ;
225259 }
226260 }
227261
0 commit comments