@@ -21,6 +21,7 @@ import { ResourceService } from "./resource";
2121import { ValueService } from "./value" ;
2222import { compileScriptCode } from "../content/utils" ;
2323import { SystemConfig } from "@App/pkg/config/config" ;
24+ import i18n , { localePath } from "@App/locales/locales" ;
2425
2526export class ScriptService {
2627 logger : Logger ;
@@ -59,50 +60,55 @@ export class ScriptService {
5960 // 读取脚本url内容, 进行安装
6061 const logger = this . logger . with ( { url : targetUrl } ) ;
6162 logger . debug ( "install script" ) ;
62- this . openInstallPageByUrl ( targetUrl , "user" ) . catch ( ( e ) => {
63- logger . error ( "install script error" , Logger . E ( e ) ) ;
64- // 如果打开失败, 则重定向到安装页
65- chrome . scripting . executeScript ( {
66- target : { tabId : req . tabId } ,
67- func : function ( ) {
68- history . back ( ) ;
69- } ,
70- } ) ;
71- // 并不再重定向当前url
72- chrome . declarativeNetRequest . updateDynamicRules (
73- {
74- removeRuleIds : [ 2 ] ,
75- addRules : [
76- {
77- id : 2 ,
78- priority : 1 ,
79- action : {
80- type : chrome . declarativeNetRequest . RuleActionType . ALLOW ,
63+ this . openInstallPageByUrl ( targetUrl , "user" )
64+ . catch ( ( e ) => {
65+ logger . error ( "install script error" , Logger . E ( e ) ) ;
66+ // 不再重定向当前url
67+ chrome . declarativeNetRequest . updateDynamicRules (
68+ {
69+ removeRuleIds : [ 2 ] ,
70+ addRules : [
71+ {
72+ id : 2 ,
73+ priority : 1 ,
74+ action : {
75+ type : chrome . declarativeNetRequest . RuleActionType . ALLOW ,
76+ } ,
77+ condition : {
78+ regexFilter : targetUrl ,
79+ resourceTypes : [ chrome . declarativeNetRequest . ResourceType . MAIN_FRAME ] ,
80+ requestMethods : [ chrome . declarativeNetRequest . RequestMethod . GET ] ,
81+ } ,
8182 } ,
82- condition : {
83- regexFilter : targetUrl ,
84- resourceTypes : [ chrome . declarativeNetRequest . ResourceType . MAIN_FRAME ] ,
85- requestMethods : [ chrome . declarativeNetRequest . RequestMethod . GET ] ,
86- } ,
87- } ,
88- ] ,
89- } ,
90- ( ) => {
91- if ( chrome . runtime . lastError ) {
92- console . error ( chrome . runtime . lastError ) ;
83+ ] ,
84+ } ,
85+ ( ) => {
86+ if ( chrome . runtime . lastError ) {
87+ console . error ( chrome . runtime . lastError ) ;
88+ }
9389 }
94- }
95- ) ;
96- } ) ;
90+ ) ;
91+ } )
92+ . finally ( ( ) => {
93+ // 回退到到安装页
94+ chrome . scripting . executeScript ( {
95+ target : { tabId : req . tabId } ,
96+ func : function ( ) {
97+ history . back ( ) ;
98+ } ,
99+ } ) ;
100+ } ) ;
97101 } ,
98102 {
99103 urls : [
100- "https://docs.scriptcat.org/docs/script_installation" ,
104+ "https://docs.scriptcat.org/docs/script_installation/" ,
105+ "https://docs.scriptcat.org/en/docs/script_installation/" ,
101106 "https://www.tampermonkey.net/script_installation.php" ,
102107 ] ,
103108 types : [ "main_frame" ] ,
104109 }
105110 ) ;
111+ // 获取i18n
106112 // 重定向到脚本安装页
107113 chrome . declarativeNetRequest . updateDynamicRules (
108114 {
@@ -114,7 +120,7 @@ export class ScriptService {
114120 action : {
115121 type : chrome . declarativeNetRequest . RuleActionType . REDIRECT ,
116122 redirect : {
117- regexSubstitution : " https://docs.scriptcat.org/docs/script_installation#url=\\0" ,
123+ regexSubstitution : ` https://docs.scriptcat.org${ localePath } /docs/script_installation/ #url=\\0` ,
118124 } ,
119125 } ,
120126 condition : {
@@ -479,6 +485,15 @@ export class ScriptService {
479485 return this . checkUpdate ( uuid , "user" ) ;
480486 }
481487
488+ isInstalled ( { name, namespace } : { name : string ; namespace : string } ) {
489+ return this . scriptDAO . findByNameAndNamespace ( name , namespace ) . then ( ( script ) => {
490+ if ( script ) {
491+ return { installed : true , version : script . metadata . version && script . metadata . version [ 0 ] } ;
492+ }
493+ return { installed : false } ;
494+ } ) ;
495+ }
496+
482497 init ( ) {
483498 this . listenerScriptInstall ( ) ;
484499
@@ -494,6 +509,7 @@ export class ScriptService {
494509 this . group . on ( "resetMatch" , this . resetMatch . bind ( this ) ) ;
495510 this . group . on ( "resetExclude" , this . resetExclude . bind ( this ) ) ;
496511 this . group . on ( "requestCheckUpdate" , this . requestCheckUpdate . bind ( this ) ) ;
512+ this . group . on ( "isInstalled" , this . isInstalled . bind ( this ) ) ;
497513
498514 // 定时检查更新, 每10分钟检查一次
499515 chrome . alarms . create ( "checkScriptUpdate" , {
0 commit comments