Skip to content

Commit fe7a034

Browse files
author
Richard Frost
committed
Context menu can toggle filter for domain
1 parent d94c7fd commit fe7a034

File tree

2 files changed

+86
-31
lines changed

2 files changed

+86
-31
lines changed

eventPage.js

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
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+
114
// Open options page if extension icon is clicked
215
chrome.browserAction.onClicked.addListener(function() {chrome.runtime.openOptionsPage();});
316

417
// Actions for extension install or upgrade
518
chrome.runtime.onInstalled.addListener(function(details){
6-
if (details.reason == "install"){
19+
if (details.reason == 'install'){
720
chrome.runtime.openOptionsPage();
8-
} else if (details.reason == "update") {
21+
} else if (details.reason == 'update') {
922
// var thisVersion = chrome.runtime.getManifest().version;
1023
// console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
1124

1225
// TODO: Migrate wordList - Open options page to show new features
13-
chrome.runtime.openOptionsPage();
26+
// chrome.runtime.openOptionsPage();
1427
}
1528
});
1629

@@ -20,24 +33,23 @@ chrome.runtime.onMessage.addListener(
2033
if (request.counter) {
2134
chrome.browserAction.setBadgeText({text: request.counter, tabId: sender.tab.id});
2235
} 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);
2440
}
2541
}
2642
);
2743

2844
////
2945
// Context menu
3046
//
31-
function arrayContains(array, string) {
32-
return (array.indexOf(string) > -1);
33-
}
34-
3547
function addSelection(selection) {
36-
chrome.storage.sync.get({'words': {}}, function(storage) {
48+
chrome.storage.sync.get({"words": {}}, function(storage) {
3749
selection = (selection.trim()).toLowerCase();
3850
if (!arrayContains(Object.keys(storage.words), selection)) {
3951
storage.words[selection] = {"matchMethod": 0, "words": []};
40-
chrome.storage.sync.set({'words': storage.words}, function() {
52+
chrome.storage.sync.set({"words": storage.words}, function() {
4153
if (!chrome.runtime.lastError) {
4254
chrome.tabs.reload();
4355
}
@@ -47,10 +59,10 @@ function addSelection(selection) {
4759
}
4860

4961
function disableDomain(domain) {
50-
chrome.storage.sync.get({'disabledDomains': []}, function(storage) {
62+
chrome.storage.sync.get({"disabledDomains": []}, function(storage) {
5163
if (!arrayContains(storage.disabledDomains, domain)) {
5264
storage.disabledDomains.push(domain);
53-
chrome.storage.sync.set({'disabledDomains': storage.disabledDomains}, function() {
65+
chrome.storage.sync.set({"disabledDomains": storage.disabledDomains}, function() {
5466
if (!chrome.runtime.lastError) {
5567
chrome.tabs.reload();
5668
}
@@ -59,39 +71,80 @@ function disableDomain(domain) {
5971
});
6072
}
6173

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
62106
chrome.contextMenus.create({
63107
"id": "addSelection",
64108
"title": "Add selection to filter",
65109
"contexts": ["selection"]
66110
});
67-
chrome.contextMenus.onClicked.addListener(function(info, tab) {
68-
if (info.menuItemId == "addSelection") {
69-
addSelection(info.selectionText);
70-
}
71-
});
72111

73112
chrome.contextMenus.create({
74113
"id": "disableDomain",
75114
"title": "Disable filter for domain",
115+
"visible": true,
76116
"contexts": ["all"]
77117
});
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"]
84124
});
85125

86-
chrome.contextMenus.create({type: "separator"});
126+
chrome.contextMenus.create({id: "separator1", type: "separator"});
87127

88128
chrome.contextMenus.create({
89129
"id": "options",
90130
"title": "Options...",
91131
"contexts": ["all"]
92132
});
133+
93134
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;
96149
}
97150
});

filter.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ function cleanPage() {
101101
disabledDomains = storage.disabledDomains;
102102

103103
// Don't run if this is a disabled domain
104-
if (disabledPage()) {
105-
chrome.runtime.sendMessage({disabled: true});
104+
message = disabledPage();
105+
chrome.runtime.sendMessage(message);
106+
if (message.disabled) {
106107
return false;
107108
}
108109

@@ -137,20 +138,21 @@ function cleanPage() {
137138
}
138139

139140
function disabledPage() {
140-
disabled = false;
141+
result = { "disabled": false };
141142
domain = window.location.hostname;
142143

143144
for (var x = 0; x < disabledDomains.length; x++) {
144145
if (disabledDomains[x]) {
145146
domainRegex = new RegExp("(^|\.)" + disabledDomains[x]);
146147
if (domainRegex.test(domain)) {
147-
disabled = true;
148+
result.disabled = true;
149+
result.domain = disabledDomains[x];
148150
break;
149151
}
150152
}
151153
}
152154

153-
return disabled;
155+
return result;
154156
}
155157

156158
// Parse the profanity list

0 commit comments

Comments
 (0)