Add 2 global behavior toggles to the toolbar button context menu

This commit is contained in:
tophf 2015-03-25 20:59:10 +03:00
parent 2ed53db7ad
commit bd9a91951f
4 changed files with 43 additions and 5 deletions

View File

@ -181,6 +181,10 @@
"message": "Stylish", "message": "Stylish",
"description": "Title for the manage page" "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": { "noStylesForSite": {
"message": "No styles installed for this site.", "message": "No styles installed for this site.",
"description": "Text displayed when no styles are installed for the current site" "description": "Text displayed when no styles are installed for the current site"

View File

@ -37,6 +37,14 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
case "openURL": case "openURL":
openURL(request); openURL(request);
break; 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")}); openURL({url: chrome.extension.getURL("manage.html")});
break; break;
case "styleDisableAll": case "styleDisableAll":
var newState = !prefs.getPref("disableAll"); disableAllStylesToggle();
prefs.setPref("disableAll", newState); chrome.contextMenus.update("disableAll", {checked: prefs.getPref("disableAll")});
notifyAllTabs({method: "styleDisableAll", disableAll: newState});
chrome.extension.sendMessage({method: "updatePopup", reason: "styleDisableAll", disableAll: newState});
break; 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) { function getStyles(options, callback) {
var enabled = fixBoolean(options.enabled); var enabled = fixBoolean(options.enabled);

View File

@ -12,6 +12,7 @@
"permissions": [ "permissions": [
"tabs", "tabs",
"webNavigation", "webNavigation",
"contextMenus",
"http://userstyles.org/", "http://userstyles.org/",
"https://userstyles.org/" "https://userstyles.org/"
], ],

View File

@ -217,7 +217,9 @@ var prefs = {
var newValue = this.getPref(key); var newValue = this.getPref(key);
if (newValue !== oldValue) { 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) } removePref: function(key) { setPref(key, undefined) }