1
+ ////
2
+ // Helper functions
3
+ function arrayContains ( array , string ) {
4
+ return ( array . indexOf ( string ) > - 1 ) ;
5
+ }
6
+
7
+ function removeFromArray ( array , element ) {
8
+ return array . filter ( e => e !== element ) ;
9
+ }
10
+
11
+ ////
12
+ // Actions and messaging
13
+
1
14
// Open options page if extension icon is clicked
2
15
chrome . browserAction . onClicked . addListener ( function ( ) { chrome . runtime . openOptionsPage ( ) ; } ) ;
3
16
4
17
// Actions for extension install or upgrade
5
18
chrome . runtime . onInstalled . addListener ( function ( details ) {
6
- if ( details . reason == " install" ) {
19
+ if ( details . reason == ' install' ) {
7
20
chrome . runtime . openOptionsPage ( ) ;
8
- } else if ( details . reason == " update" ) {
21
+ } else if ( details . reason == ' update' ) {
9
22
// var thisVersion = chrome.runtime.getManifest().version;
10
23
// console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
11
24
12
25
// TODO: Migrate wordList - Open options page to show new features
13
- chrome . runtime . openOptionsPage ( ) ;
26
+ // chrome.runtime.openOptionsPage();
14
27
}
15
28
} ) ;
16
29
@@ -20,24 +33,23 @@ chrome.runtime.onMessage.addListener(
20
33
if ( request . counter ) {
21
34
chrome . browserAction . setBadgeText ( { text : request . counter , tabId : sender . tab . id } ) ;
22
35
} else if ( request . disabled ) {
23
- chrome . browserAction . setIcon ( { path : "icons/icon19-disabled.png" , tabId : sender . tab . id } ) ;
36
+ chrome . browserAction . setIcon ( { path : 'icons/icon19-disabled.png' , tabId : sender . tab . id } ) ;
37
+ showEnableDomainMenuItem ( request . domain ) ;
38
+ } else if ( request . disabled === false ) {
39
+ hideEnableDomainMenuItem ( request . domain ) ;
24
40
}
25
41
}
26
42
) ;
27
43
28
44
////
29
45
// Context menu
30
46
//
31
- function arrayContains ( array , string ) {
32
- return ( array . indexOf ( string ) > - 1 ) ;
33
- }
34
-
35
47
function addSelection ( selection ) {
36
- chrome . storage . sync . get ( { ' words' : { } } , function ( storage ) {
48
+ chrome . storage . sync . get ( { " words" : { } } , function ( storage ) {
37
49
selection = ( selection . trim ( ) ) . toLowerCase ( ) ;
38
50
if ( ! arrayContains ( Object . keys ( storage . words ) , selection ) ) {
39
51
storage . words [ selection ] = { "matchMethod" : 0 , "words" : [ ] } ;
40
- chrome . storage . sync . set ( { ' words' : storage . words } , function ( ) {
52
+ chrome . storage . sync . set ( { " words" : storage . words } , function ( ) {
41
53
if ( ! chrome . runtime . lastError ) {
42
54
chrome . tabs . reload ( ) ;
43
55
}
@@ -47,10 +59,10 @@ function addSelection(selection) {
47
59
}
48
60
49
61
function disableDomain ( domain ) {
50
- chrome . storage . sync . get ( { ' disabledDomains' : [ ] } , function ( storage ) {
62
+ chrome . storage . sync . get ( { " disabledDomains" : [ ] } , function ( storage ) {
51
63
if ( ! arrayContains ( storage . disabledDomains , domain ) ) {
52
64
storage . disabledDomains . push ( domain ) ;
53
- chrome . storage . sync . set ( { ' disabledDomains' : storage . disabledDomains } , function ( ) {
65
+ chrome . storage . sync . set ( { " disabledDomains" : storage . disabledDomains } , function ( ) {
54
66
if ( ! chrome . runtime . lastError ) {
55
67
chrome . tabs . reload ( ) ;
56
68
}
@@ -59,39 +71,80 @@ function disableDomain(domain) {
59
71
} ) ;
60
72
}
61
73
74
+ // Remove all entries that disable the filter for domain
75
+ function enableDomain ( domain ) {
76
+ chrome . storage . sync . get ( { "disabledDomains" : [ ] } , function ( storage ) {
77
+ var newDisabledDomains = storage . disabledDomains ;
78
+
79
+ for ( var x = 0 ; x < storage . disabledDomains . length ; x ++ ) {
80
+ domainRegex = new RegExp ( '(^|\.)' + storage . disabledDomains [ x ] ) ;
81
+ if ( domainRegex . test ( domain ) ) {
82
+ newDisabledDomains = removeFromArray ( newDisabledDomains , storage . disabledDomains [ x ] ) ;
83
+ }
84
+ }
85
+
86
+ chrome . storage . sync . set ( { "disabledDomains" : newDisabledDomains } , function ( ) {
87
+ if ( ! chrome . runtime . lastError ) {
88
+ chrome . tabs . reload ( ) ;
89
+ }
90
+ } ) ;
91
+ } ) ;
92
+ }
93
+
94
+ function hideEnableDomainMenuItem ( domain ) {
95
+ chrome . contextMenus . update ( 'disableDomain' , { "visible" : true } ) ;
96
+ chrome . contextMenus . update ( 'enableDomain' , { "visible" : false } ) ;
97
+ }
98
+
99
+ function showEnableDomainMenuItem ( domain ) {
100
+ chrome . contextMenus . update ( 'disableDomain' , { "visible" : false } ) ;
101
+ chrome . contextMenus . update ( 'enableDomain' , { "visible" : true } ) ;
102
+ }
103
+
104
+ ////
105
+ // Menu Items
62
106
chrome . contextMenus . create ( {
63
107
"id" : "addSelection" ,
64
108
"title" : "Add selection to filter" ,
65
109
"contexts" : [ "selection" ]
66
110
} ) ;
67
- chrome . contextMenus . onClicked . addListener ( function ( info , tab ) {
68
- if ( info . menuItemId == "addSelection" ) {
69
- addSelection ( info . selectionText ) ;
70
- }
71
- } ) ;
72
111
73
112
chrome . contextMenus . create ( {
74
113
"id" : "disableDomain" ,
75
114
"title" : "Disable filter for domain" ,
115
+ "visible" : true ,
76
116
"contexts" : [ "all" ]
77
117
} ) ;
78
- chrome . contextMenus . onClicked . addListener ( function ( info , tab ) {
79
- if ( info . menuItemId == "disableDomain" ) {
80
- var url = new URL ( tab . url ) ;
81
- var domain = url . hostname ;
82
- disableDomain ( domain ) ;
83
- }
118
+
119
+ chrome . contextMenus . create ( {
120
+ "id" : "enableDomain" ,
121
+ "title" : "Enable filter for domain" ,
122
+ "visible" : false ,
123
+ "contexts" : [ "all" ]
84
124
} ) ;
85
125
86
- chrome . contextMenus . create ( { type : "separator" } ) ;
126
+ chrome . contextMenus . create ( { id : "separator1" , type : "separator" } ) ;
87
127
88
128
chrome . contextMenus . create ( {
89
129
"id" : "options" ,
90
130
"title" : "Options..." ,
91
131
"contexts" : [ "all" ]
92
132
} ) ;
133
+
93
134
chrome . contextMenus . onClicked . addListener ( function ( info , tab ) {
94
- if ( info . menuItemId == "options" ) {
95
- chrome . runtime . openOptionsPage ( ) ;
135
+ switch ( info . menuItemId ) {
136
+ case "addSelection" :
137
+ addSelection ( info . selectionText ) ; break ;
138
+ case "disableDomain" :
139
+ var url = new URL ( tab . url ) ;
140
+ var domain = url . hostname ;
141
+ disableDomain ( domain ) ; break ;
142
+ case "enableDomain" :
143
+ var url = new URL ( tab . url ) ;
144
+ var domain = url . hostname ;
145
+ enableDomain ( domain ) ; break ;
146
+ break ;
147
+ case "options" :
148
+ chrome . runtime . openOptionsPage ( ) ; break ;
96
149
}
97
150
} ) ;
0 commit comments