1- chrome . storage . onChanged . addListener ( ( changes , areaName ) => {
2- console . log ( "storage change" , changes , areaName ) ;
3- }
4- )
1+ chrome . storage . onChanged . addListener ( ( changes , areaName ) => {
2+ if ( changes . chooseTheme . oldValue ) {
3+ chrome . scripting . unregisterContentScripts ( {
4+ ids : [ changes . chooseTheme . oldValue ] ,
5+ } ) ;
6+ }
7+
8+ if ( changes . chooseTheme . newValue ) {
9+ const new_flomo_theme = {
10+ id : changes . chooseTheme . newValue ,
11+ matches : [ "https://v.flomoapp.com/*" , "https://flomoapp.com/*" ] ,
12+ css : [ "include/theme/" + changes . chooseTheme . newValue ] ,
13+ runAt : "document_end" ,
14+ } ;
15+ chrome . scripting . registerContentScripts ( [ new_flomo_theme ] ) ;
16+
17+ // 使新的css生效
18+ chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
19+ // 注入新的内容脚本,如果当前页面是chrome:// 则不注入
20+ if ( tabs [ 0 ] . url . indexOf ( "chrome://" ) > - 1 ) {
21+ return ;
22+ }
23+ console . log ( "注入新的css" + tabs [ 0 ] . id ) ;
24+ chrome . scripting . insertCSS ( {
25+ target : {
26+ tabId : tabs [ 0 ] . id ,
27+ } ,
28+ files : [ "include/theme/" + changes . chooseTheme . newValue ] ,
29+ } ) ;
30+ } ) ;
31+ }
32+ } ) ;
533
6- chrome . tabs . onCreated . addListener (
7- ( tab ) => {
8- console . log ( "tab created" , tab ) ;
9-
34+ const DEFAULT_THEME = "stackoverflow-dark.css" ;
35+ chrome . runtime . onInstalled . addListener ( ( ) => {
36+ chrome . storage . sync . set ( { chooseTheme : DEFAULT_THEME } , ( ) => {
37+ console . log ( "保存成功" ) ;
38+ } ) ;
39+ } ) ;
40+
41+ function getTheme ( ) {
42+ // 获取当前主题
43+ chrome . storage . sync . get ( "chooseTheme" , ( result ) => {
44+ if ( result . chooseTheme ) {
45+ console . log ( "当前主题为" , result . chooseTheme ) ;
46+ DEFAULT_THEME = result . chooseTheme ;
1047 }
11- )
48+ } ) ;
49+ return DEFAULT_THEME ;
50+ }
51+
52+ const flomo_content_script = {
53+ id : "flomo_content_script" ,
54+ matches : [ "https://v.flomoapp.com/*" , "https://flomoapp.com/*" ] ,
55+ js : [ "scripts/content.js" , "include/highlight.min.js" ] ,
56+ runAt : "document_end" ,
57+ } ;
58+ chrome . scripting . registerContentScripts ( [ flomo_content_script ] ) ;
0 commit comments