From 02a575a9d68386fa0923b65a726be7588aef118d Mon Sep 17 00:00:00 2001 From: narcolepticinsomniac Date: Wed, 12 Feb 2020 07:47:24 -0500 Subject: [PATCH] Add reload context menu item (#848) * Add reload context menu item * Recheck on initial install * convert createContextMenus to function * reload command --- _locales/en/messages.json | 4 ++++ background/background.js | 50 +++++++++++++++++++++++---------------- manifest.json | 3 +++ 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index bacb8089..55098a94 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1145,6 +1145,10 @@ "message": "Temporarily applies the changes without saving.\nSave the style to make the changes permanent.", "description": "Tooltip for the checkbox in style editor to enable live preview while editing." }, + "reload": { + "message": "Reload Stylus extension", + "description": "Context menu reload" + }, "replace": { "message": "Replace", "description": "Label before the replace input field in the editor shown on Ctrl-H" diff --git a/background/background.js b/background/background.js index 5b117fcc..556c370b 100644 --- a/background/background.js +++ b/background/background.js @@ -168,6 +168,9 @@ chrome.runtime.onInstalled.addListener(({reason}) => { // "normal" = addon installed from webstore chrome.management.getSelf(info => { localStorage.installType = info.installType; + if (reason === 'install' && info.installType === 'development' && chrome.contextMenus) { + createContextMenus(['reload']); + } }); if (reason !== 'update') return; @@ -187,6 +190,7 @@ browserCommands = { styleDisableAll(info) { prefs.set('disableAll', info ? info.checked : !prefs.get('disableAll')); }, + reload: () => chrome.runtime.reload(), }; // ************************************************************************* @@ -208,6 +212,11 @@ contextMenus = { title: 'openOptions', click: browserCommands.openOptions, }, + 'reload': { + presentIf: () => localStorage.installType === 'development', + title: 'reload', + click: browserCommands.reload, + }, 'editor.contextDelete': { presentIf: () => !FIREFOX && prefs.get('editor.contextDelete'), title: 'editDeleteText', @@ -220,28 +229,28 @@ contextMenus = { } }; -if (chrome.contextMenus) { - const createContextMenus = ids => { - for (const id of ids) { - let item = contextMenus[id]; - if (item.presentIf && !item.presentIf()) { - continue; - } - item = Object.assign({id}, item); - delete item.presentIf; - item.title = chrome.i18n.getMessage(item.title); - if (!item.type && typeof prefs.defaults[id] === 'boolean') { - item.type = 'checkbox'; - item.checked = prefs.get(id); - } - if (!item.contexts) { - item.contexts = ['browser_action']; - } - delete item.click; - chrome.contextMenus.create(item, ignoreChromeError); +function createContextMenus(ids) { + for (const id of ids) { + let item = contextMenus[id]; + if (item.presentIf && !item.presentIf()) { + continue; } - }; + item = Object.assign({id}, item); + delete item.presentIf; + item.title = chrome.i18n.getMessage(item.title); + if (!item.type && typeof prefs.defaults[id] === 'boolean') { + item.type = 'checkbox'; + item.checked = prefs.get(id); + } + if (!item.contexts) { + item.contexts = ['browser_action']; + } + delete item.click; + chrome.contextMenus.create(item, ignoreChromeError); + } +} +if (chrome.contextMenus) { // circumvent the bug with disabling check marks in Chrome 62-64 const toggleCheckmark = CHROME >= 3172 && CHROME <= 3288 ? (id => chrome.contextMenus.remove(id, () => createContextMenus([id]) + ignoreChromeError())) : @@ -299,7 +308,6 @@ function webNavUsercssInstallerFF(data) { }); } - function webNavIframeHelperFF({tabId, frameId}) { if (!frameId) return; msg.sendTab(tabId, {method: 'ping'}, {frameId}) diff --git a/manifest.json b/manifest.json index fc7e94a6..81aec076 100644 --- a/manifest.json +++ b/manifest.json @@ -59,6 +59,9 @@ "openManage": { "description": "__MSG_openManage__" }, + "reload": { + "description": "__MSG_reload__" + }, "styleDisableAll": { "description": "__MSG_disableAllStyles__" }