Refactor contextMenus and commands
This commit is contained in:
parent
a717e632c6
commit
80130797ce
124
background.js
124
background.js
|
@ -21,6 +21,7 @@ function webNavigationListener(method, data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// catch direct URL hash modifications not invoked via HTML5 history API
|
// catch direct URL hash modifications not invoked via HTML5 history API
|
||||||
|
|
||||||
var tabUrlHasHash = {};
|
var tabUrlHasHash = {};
|
||||||
chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
|
chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
|
||||||
if (info.status == "loading" && info.url) {
|
if (info.status == "loading" && info.url) {
|
||||||
|
@ -39,7 +40,11 @@ chrome.tabs.onRemoved.addListener(function(tabId, info) {
|
||||||
delete tabUrlHasHash[tabId];
|
delete tabUrlHasHash[tabId];
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
// messaging
|
||||||
|
|
||||||
|
chrome.runtime.onMessage.addListener(onBackgroundMessage);
|
||||||
|
|
||||||
|
function onBackgroundMessage(request, sender, sendResponse) {
|
||||||
switch (request.method) {
|
switch (request.method) {
|
||||||
case "getStyles":
|
case "getStyles":
|
||||||
var styles = getStyles(request, sendResponse);
|
var styles = getStyles(request, sendResponse);
|
||||||
|
@ -65,78 +70,78 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
||||||
openURL(request);
|
openURL(request);
|
||||||
break;
|
break;
|
||||||
case "styleDisableAll":
|
case "styleDisableAll":
|
||||||
chrome.contextMenus.update("disableAll", {checked: request.disableAll});
|
// fallthru to prefChanged
|
||||||
break;
|
request = {prefName: 'disableAll', value: request.disableAll};
|
||||||
case "prefChanged":
|
case "prefChanged":
|
||||||
if (request.prefName == "show-badge") {
|
if (typeof request.value == 'boolean' && contextMenus[request.prefName]) {
|
||||||
chrome.contextMenus.update("show-badge", {checked: request.value});
|
chrome.contextMenus.update(request.prefName, {checked: request.value});
|
||||||
}
|
|
||||||
else if (request.prefName === 'disableAll') {
|
|
||||||
chrome.contextMenus.update("disableAll", {checked: request.value});
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "refreshAllTabs":
|
case "refreshAllTabs":
|
||||||
refreshAllTabs().then(sendResponse);
|
refreshAllTabs().then(sendResponse);
|
||||||
return KEEP_CHANNEL_OPEN;
|
return KEEP_CHANNEL_OPEN;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
// commands (global hotkeys)
|
||||||
|
|
||||||
|
const browserCommands = {
|
||||||
|
openManage() {
|
||||||
|
openURL({url: '/manage.html'});
|
||||||
|
},
|
||||||
|
styleDisableAll(state) {
|
||||||
|
prefs.set('disableAll',
|
||||||
|
typeof state == 'boolean' ? state : !prefs.get('disableAll'));
|
||||||
|
},
|
||||||
|
};
|
||||||
// Not available in Firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1240350
|
// Not available in Firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1240350
|
||||||
if ("commands" in chrome) {
|
if ('commands' in chrome) {
|
||||||
chrome.commands.onCommand.addListener(function(command) {
|
chrome.commands.onCommand.addListener(command => browserCommands[command]());
|
||||||
switch (command) {
|
|
||||||
case "openManage":
|
|
||||||
openURL({url: chrome.extension.getURL("manage.html")});
|
|
||||||
break;
|
|
||||||
case "styleDisableAll":
|
|
||||||
disableAllStylesToggle();
|
|
||||||
chrome.contextMenus.update("disableAll", {checked: prefs.get("disableAll")});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// contextMenus API is present in ancient Chrome but it throws an exception
|
// context menus
|
||||||
// upon encountering the unsupported parameter value "browser_action", so we have to catch it.
|
|
||||||
runTryCatch(function() {
|
const contextMenus = {
|
||||||
chrome.contextMenus.create({
|
'show-badge': {
|
||||||
id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"),
|
title: 'menuShowBadge',
|
||||||
type: "checkbox", contexts: ["browser_action"], checked: prefs.get("show-badge")
|
click: info => prefs.set(info.menuItemId, info.checked),
|
||||||
}, function() { var clearError = chrome.runtime.lastError });
|
},
|
||||||
chrome.contextMenus.create({
|
'disableAll': {
|
||||||
id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"),
|
title: 'disableAllStyles',
|
||||||
type: "checkbox", contexts: ["browser_action"], checked: prefs.get("disableAll")
|
click: browserCommands.styleDisableAll,
|
||||||
}, function() { var clearError = chrome.runtime.lastError });
|
},
|
||||||
chrome.contextMenus.create({
|
'open-manager': {
|
||||||
id: "open-manager", title: chrome.i18n.getMessage("openStylesManager"),
|
title: 'openStylesManager',
|
||||||
type: "normal", contexts: ["browser_action"]
|
click: browserCommands.openManage,
|
||||||
}, function() {var clearError = chrome.runtime.lastError});
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
chrome.contextMenus.onClicked.addListener((info, tab) =>
|
||||||
|
contextMenus[info.menuItemId].click(info, tab));
|
||||||
|
|
||||||
|
Object.keys(contextMenus).forEach(id => {
|
||||||
|
const item = Object.assign({id}, contextMenus[id]);
|
||||||
|
const prefValue = prefs.readOnlyValues[id];
|
||||||
|
const isBoolean = typeof prefValue == 'boolean';
|
||||||
|
item.title = chrome.i18n.getMessage(item.title);
|
||||||
|
if (isBoolean) {
|
||||||
|
item.type = 'checkbox';
|
||||||
|
item.checked = prefValue;
|
||||||
|
}
|
||||||
|
if (!item.contexts) {
|
||||||
|
item.contexts = ['browser_action'];
|
||||||
|
}
|
||||||
|
delete item.click;
|
||||||
|
chrome.contextMenus.create(item, ignoreChromeError);
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.contextMenus.onClicked.addListener(function(info, tab) {
|
|
||||||
if (info.menuItemId == "disableAll") {
|
|
||||||
disableAllStylesToggle(info.checked);
|
|
||||||
}
|
|
||||||
else if (info.menuItemId === 'show-badge') {
|
|
||||||
prefs.set(info.menuItemId, info.checked);
|
|
||||||
}
|
|
||||||
else if (info.menuItemId === 'open-manager') {
|
|
||||||
openURL({url: chrome.extension.getURL("manage.html")});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function disableAllStylesToggle(newState) {
|
// Get the DB so that any first run actions will be performed immediately
|
||||||
if (newState === undefined || newState === null) {
|
// when the background page loads.
|
||||||
newState = !prefs.get("disableAll");
|
|
||||||
}
|
|
||||||
prefs.set("disableAll", newState);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the DB so that any first run actions will be performed immediately when the background page loads.
|
|
||||||
getDatabase(function() {}, reportError);
|
getDatabase(function() {}, reportError);
|
||||||
|
|
||||||
// When an edit page gets attached or detached, remember its state so we can do the same to the next one to open.
|
// When an edit page gets attached or detached, remember its state
|
||||||
|
// so we can do the same to the next one to open.
|
||||||
var editFullUrl = chrome.extension.getURL("edit.html");
|
var editFullUrl = chrome.extension.getURL("edit.html");
|
||||||
chrome.tabs.onAttached.addListener(function(tabId, data) {
|
chrome.tabs.onAttached.addListener(function(tabId, data) {
|
||||||
chrome.tabs.get(tabId, function(tabData) {
|
chrome.tabs.get(tabId, function(tabData) {
|
||||||
|
@ -193,7 +198,7 @@ function injectContentScripts() {
|
||||||
file: cs.js[0],
|
file: cs.js[0],
|
||||||
runAt: cs.run_at,
|
runAt: cs.run_at,
|
||||||
allFrames: cs.all_frames,
|
allFrames: cs.all_frames,
|
||||||
}, result => chrome.runtime.lastError); // ignore lastError just in case
|
}, ignoreChromeError);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// inject the content script just once
|
// inject the content script just once
|
||||||
|
@ -204,3 +209,8 @@ function injectContentScripts() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function ignoreChromeError() {
|
||||||
|
chrome.runtime.lastError;
|
||||||
|
}
|
||||||
|
|
|
@ -23,6 +23,10 @@ function notifyAllTabs(request) {
|
||||||
if (typeof applyOnMessage !== 'undefined') {
|
if (typeof applyOnMessage !== 'undefined') {
|
||||||
applyOnMessage(reqPopup);
|
applyOnMessage(reqPopup);
|
||||||
}
|
}
|
||||||
|
// notify self: pref changed by background page
|
||||||
|
if (request.method == 'prefChanged' && typeof onBackgroundMessage !== 'undefined') {
|
||||||
|
onBackgroundMessage(request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
10
storage.js
10
storage.js
|
@ -584,9 +584,13 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// make sure right click context menu is in the right state when prefs are loaded
|
if (typeof contextMenus !== 'undefined') {
|
||||||
chrome.contextMenus.update("disableAll", {checked: prefs.get("disableAll")});
|
for (let id in contextMenus) {
|
||||||
chrome.contextMenus.update("show-badge", {checked: prefs.get("show-badge")});
|
if (typeof values[id] == 'boolean') {
|
||||||
|
me.broadcast(id, values[id], {noSync: true});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
chrome.storage.onChanged.addListener(function(changes, area) {
|
chrome.storage.onChanged.addListener(function(changes, area) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user