addons/firefox-addons/open-incognito

changeset 3:89239e60d9e1 tip

Enable on privileged pages

There are contexts in Firefox (for example, the default new tab page) that
contain links to http(s) resources but are considered privileged. In these
contexts `menus.onShown` does not provide the link URL to the extension and
Open Incognito disables the context menu entry.
However, if the entry is not disabled, the `onClicked` listener will actually
know the link URL because a user's click on the extension's menu item is
considered to be an explicit permission, so Open Incognito could work.

Keep the menu item enabled if the URL is not available. There is a drawback
that the item will be enabled but do nothing if the actual link has an
unknown/unsupported protocol, but this is probably a more rare situation.
author Denis Lisov <dennis.lissov@gmail.com>
date Sat Feb 09 03:38:05 2019 +0300 (15 months ago)
parents 0e8c61f3373d
children
files background.js
line diff
     1.1 --- a/background.js	Fri Dec 14 22:18:02 2018 +0100
     1.2 +++ b/background.js	Sat Feb 09 03:38:05 2019 +0300
     1.3 @@ -8,17 +8,23 @@
     1.4  
     1.5  'use strict';
     1.6  
     1.7 +function hasKnownProtocol(link) {
     1.8 +    return (link.startsWith('http:') ||
     1.9 +            link.startsWith('https:') ||
    1.10 +            link.startsWith('ftp:'));
    1.11 +}
    1.12 +
    1.13  function onMenuShown(info, tab) {
    1.14      let enabled = !tab.incognito &&
    1.15 -            typeof info.linkUrl !== 'undefined' &&
    1.16 -            (info.linkUrl.startsWith('http:') ||
    1.17 -            info.linkUrl.startsWith('https:') ||
    1.18 -            info.linkUrl.startsWith('ftp:'));
    1.19 +            (typeof info.linkUrl === 'undefined' || hasKnownProtocol(info.linkUrl));
    1.20      browser.menus.update('open-link-in-private-mode', {enabled});
    1.21      browser.menus.refresh();
    1.22  }
    1.23  
    1.24  async function onClicked(info, tab) {
    1.25 +    if(!hasKnownProtocol(info.linkUrl)) {
    1.26 +        return;
    1.27 +    }
    1.28      let activeTabs = await browser.tabs.query({
    1.29          active: true,
    1.30          currentWindow: false,