diff --git a/extension/extension-purpose.js b/extension/extension-purpose.js --- a/extension/extension-purpose.js +++ b/extension/extension-purpose.js @@ -15,7 +15,8 @@ along with this program. If not, see . */ -let purposeShareMenuId = "purpose_share"; +const purposeShareMenuId = "purpose_share"; +let hasPurposeMenu = false; // Stores so that when you click the finished // notification it will open the URL @@ -61,6 +62,54 @@ }); } +function checkPurposeEnabled() { + return Promise.all([ + sendPortMessageWithReply("settings", "getSubsystemStatus"), + SettingsUtils.get() + ]).then((result) => { + + const subsystemStatus = result[0]; + const settings = result[1]; + + // HACK Unfortunately I removed the loaded/unloaded signals for plugins + // so we can't reliably know on settings change whether a module is enabled + // sending settings is also legacy done without a reply we could wait for. + // Instead, check whether the module is known and enabled in settings, + // which should be close enough, since purpose plugin also has no additional + // dependencies that could make it fail to load. + return subsystemStatus.hasOwnProperty("purpose") + && settings.purpose && settings.purpose.enabled; + }); +} + +function updatePurposeMenu() { + checkPurposeEnabled().then((enabled) => { + if (enabled && !hasPurposeMenu) { + chrome.contextMenus.create({ + id: purposeShareMenuId, + contexts: ["link", "page", "image", "audio", "video", "selection"], + title: chrome.i18n.getMessage("purpose_share") + }, () => { + const error = chrome.runtime.lastError; + if (error) { + console.warn("Error creating purpose context menu", error.message); + return; + } + hasPurposeMenu = true; + }); + } else if (!enabled && hasPurposeMenu) { + chrome.contextMenus.remove(purposeShareMenuId, () => { + const error = chrome.runtime.lastError; + if (error) { + console.warn("Error removing purpose context menu", error.message); + return; + } + hasPurposeMenu = false; + }); + } + }); +} + chrome.contextMenus.onClicked.addListener((info) => { if (info.menuItemId !== purposeShareMenuId) { return; @@ -107,12 +156,11 @@ }); }); -// FIXME only add context menu if purpose is enabled and supported -/*chrome.contextMenus.create({ - id: purposeShareMenuId, - contexts: ["link", "page", "image", "audio", "video", "selection"], - title: chrome.i18n.getMessage("purpose_share") -});*/ +SettingsUtils.onChanged().addListener((delta) => { + if (delta.purpose) { + updatePurposeMenu(); + } +}); addRuntimeCallback("purpose", "share", (message, sender, action) => { return purposeShare(message); diff --git a/extension/extension.js b/extension/extension.js --- a/extension/extension.js +++ b/extension/extension.js @@ -197,6 +197,8 @@ sendEnvironment(); sendSettings(); sendDownloads(); + + updatePurposeMenu(); } SettingsUtils.onChanged().addListener(() => {