From 2ed53db7ad2ba598548f155599b44c216a40e4ce Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 24 Mar 2015 17:07:59 +0300 Subject: [PATCH 1/2] Add Chrome shortcut keys; update popups on change * hotkey to open Manage styles * hotkey to enable/disable all styles No default hotkeys are provided, to customize go to "Keyboard shortcuts" on the Extensions page --- background.js | 14 ++++++++++++++ edit.js | 1 + manifest.json | 8 ++++++++ popup.js | 43 +++++++++++++++++++++++++++++++++++-------- storage.js | 4 +++- 5 files changed, 61 insertions(+), 9 deletions(-) diff --git a/background.js b/background.js index 1c386919..980f776a 100644 --- a/background.js +++ b/background.js @@ -40,6 +40,20 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { } }); +chrome.commands.onCommand.addListener(function(command) { + switch (command) { + case "openManage": + 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}); + break; + } +}); + function getStyles(options, callback) { var enabled = fixBoolean(options.enabled); diff --git a/edit.js b/edit.js index 941135a5..e1fbd732 100644 --- a/edit.js +++ b/edit.js @@ -619,6 +619,7 @@ function saveComplete(style) { } else { updateTitle(); } + chrome.extension.sendMessage({method: "updatePopup", reason: "styleUpdated", style: style}); } function showMozillaFormat() { diff --git a/manifest.json b/manifest.json index ef89c118..2a92bce5 100644 --- a/manifest.json +++ b/manifest.json @@ -18,6 +18,14 @@ "background": { "page": "background.html" }, + "commands": { + "openManage": { + "description": "__MSG_openManage__" + }, + "styleDisableAll": { + "description": "__MSG_disableAllStyles__" + } + }, "content_scripts": [ { "matches": ["http://*/*", "https://*/*", "file:///*"], diff --git a/popup.js b/popup.js index 6eee1f49..72bcd745 100644 --- a/popup.js +++ b/popup.js @@ -4,8 +4,10 @@ styleTemplate.innerHTML = "
" + t('noStylesForSite') + "
"; } @@ -173,15 +174,40 @@ function openLink(event) { } function handleUpdate(style) { - var installed = document.getElementById("installed"); - installed.replaceChild(createStyleElement(style), installed.querySelector("[style-id='" + style.id + "']")); + var styleElement = installed.querySelector("[style-id='" + style.id + "']"); + if (styleElement) { + installed.replaceChild(createStyleElement(style), styleElement); + } } function handleDelete(id) { - var installed = document.getElementById("installed"); - installed.removeChild(installed.querySelector("[style-id='" + id + "']")); + var styleElement = installed.querySelector("[style-id='" + id + "']"); + if (styleElement) { + installed.removeChild(styleElement); + } } +function handleDisableAll(disableAll) { + installed.classList.toggle("disabled", disableAll); +} + +chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { + if (request.method == "updatePopup") { + switch (request.reason) { + case "styleUpdated": + handleUpdate(request.style); + break; + case "styleDeleted": + handleDelete(request.id); + break; + case "styleDisableAll": + document.getElementById("disableAll").checked = request.disableAll; + handleDisableAll(request.disableAll); + break; + } + } +}); + tE("open-manage-link", "openManage"); tE("write-style-for", "writeStyleFor"); tE("find-styles-link", "findStylesForSite"); @@ -191,8 +217,9 @@ tE("disableAll-label", "disableAllStyles"); document.getElementById(id).addEventListener("click", openLink, false); }); -loadPrefs({"disableAll": false}) +loadPrefs({"disableAll": false}); +handleDisableAll(prefs.getPref("disableAll")); document.getElementById("disableAll").addEventListener("change", function(event) { - document.getElementById("installed").classList.toggle("disabled", event.target.checked); notifyAllTabs({method: "styleDisableAll", disableAll: event.target.checked}); + chrome.extension.sendMessage({method: "updatePopup", reason: "styleDisableAll", disableAll: event.target.checked}); }); diff --git a/storage.js b/storage.js index c4b75689..e22a5937 100644 --- a/storage.js +++ b/storage.js @@ -80,7 +80,8 @@ function enableStyle(id, enabled) { chrome.extension.sendMessage({method: "styleChanged"}); chrome.extension.sendMessage({method: "getStyles", id: id}, function(styles) { handleUpdate(styles[0]); - notifyAllTabs({method:"styleUpdated", style: styles[0]}); + notifyAllTabs({method: "styleUpdated", style: styles[0]}); + chrome.extension.sendMessage({method: "updatePopup", reason: "styleUpdated", style: styles[0]}); }); }); }); @@ -94,6 +95,7 @@ function deleteStyle(id) { t.executeSql("DELETE FROM styles WHERE id = ?;", [id]); }, reportError, function() { chrome.extension.sendMessage({method: "styleChanged"}); + chrome.extension.sendMessage({method: "updatePopup", reason: "styleDeleted", id: id}); handleDelete(id); notifyAllTabs({method: "styleDeleted", id: id}); }); From bd9a91951f88fdcf4f5fa69002336fa98fffbc12 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 25 Mar 2015 20:59:10 +0300 Subject: [PATCH 2/2] 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) }