From bd9a91951f88fdcf4f5fa69002336fa98fffbc12 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 25 Mar 2015 20:59:10 +0300 Subject: [PATCH] Add 2 global behavior toggles to the toolbar button context menu --- _locales/en/messages.json | 4 ++++ background.js | 39 +++++++++++++++++++++++++++++++++++---- manifest.json | 1 + storage.js | 4 +++- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 38befb21..24d3985e 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -181,6 +181,10 @@ "message": "Stylish", "description": "Title for the manage page" }, + "menuShowBadge": { + "message": "Show active style count", + "description": "Label (must be very short) for the checkbox in the toolbar button context menu controlling toolbar badge text." + }, "noStylesForSite": { "message": "No styles installed for this site.", "description": "Text displayed when no styles are installed for the current site" diff --git a/background.js b/background.js index 980f776a..1284d5a5 100644 --- a/background.js +++ b/background.js @@ -37,6 +37,14 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { case "openURL": openURL(request); break; + case "styleDisableAll": + chrome.contextMenus.update("disableAll", {checked: request.disableAll}); + break; + case "prefChanged": + if (request.prefName == "show-badge") { + chrome.contextMenus.update("show-badge", {checked: request.value}); + } + break; } }); @@ -46,14 +54,37 @@ chrome.commands.onCommand.addListener(function(command) { openURL({url: chrome.extension.getURL("manage.html")}); break; case "styleDisableAll": - var newState = !prefs.getPref("disableAll"); - prefs.setPref("disableAll", newState); - notifyAllTabs({method: "styleDisableAll", disableAll: newState}); - chrome.extension.sendMessage({method: "updatePopup", reason: "styleDisableAll", disableAll: newState}); + disableAllStylesToggle(); + chrome.contextMenus.update("disableAll", {checked: prefs.getPref("disableAll")}); break; } }); +chrome.contextMenus.create({ + id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"), + type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("show-badge") +}); +chrome.contextMenus.create({ + id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"), + type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("disableAll") +}); +chrome.contextMenus.onClicked.addListener(function(info, tab) { + if (info.menuItemId == "disableAll") { + disableAllStylesToggle(info.checked); + } else { + prefs.setPref(info.menuItemId, info.checked); + } +}); + +function disableAllStylesToggle(newState) { + if (newState === undefined || newState === null) { + newState = !prefs.getPref("disableAll"); + } + prefs.setPref("disableAll", newState); + notifyAllTabs({method: "styleDisableAll", disableAll: newState}); + chrome.extension.sendMessage({method: "updatePopup", reason: "styleDisableAll", disableAll: newState}); +} + function getStyles(options, callback) { var enabled = fixBoolean(options.enabled); diff --git a/manifest.json b/manifest.json index 2a92bce5..be1b0632 100644 --- a/manifest.json +++ b/manifest.json @@ -12,6 +12,7 @@ "permissions": [ "tabs", "webNavigation", + "contextMenus", "http://userstyles.org/", "https://userstyles.org/" ], diff --git a/storage.js b/storage.js index e22a5937..a9d5f2e6 100644 --- a/storage.js +++ b/storage.js @@ -217,7 +217,9 @@ var prefs = { var newValue = this.getPref(key); if (newValue !== oldValue) { - notifyAllTabs({method: "prefChanged", prefName: key, value: value}); + var message = {method: "prefChanged", prefName: key, value: value}; + notifyAllTabs(message); + chrome.extension.sendMessage(message); } }, removePref: function(key) { setPref(key, undefined) }