Mercurial > addons > firefox-addons > open-incognito
comparison background.js @ 0:3353f3c48b6b
Initial revision
author | Guido Berhoerster <guido+open-incognito@berhoerster.name> |
---|---|
date | Fri, 14 Dec 2018 15:59:37 +0100 |
parents | |
children | 89239e60d9e1 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3353f3c48b6b |
---|---|
1 /* | |
2 * Copyright (C) 2018 Guido Berhoerster <guido+open-incognito@berhoerster.name> | |
3 * | |
4 * This Source Code Form is subject to the terms of the Mozilla Public | |
5 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 */ | |
8 | |
9 'use strict'; | |
10 | |
11 function onMenuShown(info, tab) { | |
12 let enabled = !tab.incognito && | |
13 typeof info.linkUrl !== 'undefined' && | |
14 (info.linkUrl.startsWith('http:') || | |
15 info.linkUrl.startsWith('https:') || | |
16 info.linkUrl.startsWith('ftp:')); | |
17 browser.menus.update('open-link-in-private-mode', {enabled}); | |
18 browser.menus.refresh(); | |
19 } | |
20 | |
21 async function onClicked(info, tab) { | |
22 let activeTabs = await browser.tabs.query({ | |
23 active: true, | |
24 currentWindow: false, | |
25 windowType: 'normal' | |
26 }); | |
27 console.log('active tabs:', activeTabs); | |
28 let [windowId, ] = activeTabs.reduce((accumulator, currentTab) => | |
29 (currentTab.incognito && accumulator[1] < currentTab.lastAccessed) ? | |
30 [currentTab.windowId, currentTab.lastAccessed] : accumulator, | |
31 [-1, 0]); | |
32 if (windowId < 0) { | |
33 browser.windows.create({ | |
34 incognito: true, | |
35 url: info.linkUrl | |
36 }); | |
37 } else { | |
38 browser.tabs.create({ | |
39 url: info.linkUrl, | |
40 windowId | |
41 }); | |
42 } | |
43 } | |
44 | |
45 browser.menus.create({ | |
46 id: 'open-link-in-private-mode', | |
47 title: browser.i18n.getMessage('openLinkInPrivateWindow'), | |
48 enabled: false, | |
49 contexts: ['link'] | |
50 }, | |
51 () => { | |
52 if (browser.runtime.lastError) { | |
53 console.error('Failed to create menu item:', browser.runtime.lastError); | |
54 return; | |
55 } | |
56 browser.menus.onShown.addListener(onMenuShown); | |
57 browser.menus.onClicked.addListener(onClicked); | |
58 }); |