From bf725ece72cb43257da554d5d74eac57c29346a0 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Tue, 10 Sep 2019 12:43:47 +0200 Subject: [PATCH] Open links in the correct window context --- lib/browser.ts | 1 + lib/select.ts | 4 ++-- lib/windowutils.ts | 36 ++++++++++++++++++++++-------------- windows/select.ts | 39 ++++++++++++++++++++++++++++++++------- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/lib/browser.ts b/lib/browser.ts index 3052f7e..ea3204b 100644 --- a/lib/browser.ts +++ b/lib/browser.ts @@ -19,6 +19,7 @@ export interface MessageSender { export interface Tab { id?: number; + incognito?: boolean; } export interface MenuClickInfo { diff --git a/lib/select.ts b/lib/select.ts index cfc9458..de6cac3 100644 --- a/lib/select.ts +++ b/lib/select.ts @@ -187,8 +187,8 @@ export async function select(links: BaseItem[], media: BaseItem[]) { openPrefs(); }); - port.on("openUrls", ({urls}) => { - openUrls(urls); + port.on("openUrls", ({urls, incognito}) => { + openUrls(urls, incognito); }); try { diff --git a/lib/windowutils.ts b/lib/windowutils.ts index f53bdf4..ad919a0 100644 --- a/lib/windowutils.ts +++ b/lib/windowutils.ts @@ -8,13 +8,16 @@ import DEFAULT_ICONS from "../data/icons.json"; const DONATE_URL = "https://www.downthemall.org/howto/donate/"; const MANAGER_URL = "/windows/manager.html"; -export async function mostRecentBrowser(): Promise { +export async function mostRecentBrowser(incognito: boolean): Promise { let window; try { window = await windows.getCurrent({windowTypes: ["normal"]}); if (window.type !== "normal") { throw new Error("not a normal window"); } + if (incognito && !window.incognito) { + throw new Error("Not incognito"); + } } catch { try { @@ -22,23 +25,28 @@ export async function mostRecentBrowser(): Promise { if (window.type !== "normal") { throw new Error("not a normal window"); } + if (incognito && !window.incognito) { + throw new Error("Not incognito"); + } } catch { window = Array.from(await windows.getAll({windowTypes: ["normal"]})). - filter((w: any) => w.type === "normal").pop(); + filter( + (w: any) => w.type === "normal" && !!w.incognito === !!incognito). + pop(); } } if (!window) { window = await windows.create({ - url: DONATE_URL, + incognito: !!incognito, type: "normal", }); } return window; } -export async function openInTab(url: string) { - const window = await mostRecentBrowser(); +export async function openInTab(url: string, incognito: boolean) { + const window = await mostRecentBrowser(incognito); await tabs.create({ active: true, url, @@ -47,7 +55,7 @@ export async function openInTab(url: string) { await windows.update(window.id, {focused: true}); } -export async function openInTabOrFocus(url: string) { +export async function openInTabOrFocus(url: string, incognito: boolean) { const etabs = await tabs.query({ url }); @@ -57,21 +65,21 @@ export async function openInTabOrFocus(url: string) { await windows.update(tab.windowId, {focused: true}); return; } - await openInTab(url); + await openInTab(url, incognito); } -export async function maybeOpenInTab(url: string) { +export async function maybeOpenInTab(url: string, incognito: boolean) { const etabs = await tabs.query({ url }); if (etabs.length) { return; } - await openInTab(url); + await openInTab(url, incognito); } export async function donate() { - await openInTab(DONATE_URL); + await openInTab(DONATE_URL, false); } export async function openPrefs() { @@ -86,15 +94,15 @@ export async function openManager(focus = true) { console.error(ex.toString(), ex); } if (focus) { - await openInTabOrFocus(await runtime.getURL(MANAGER_URL)); + await openInTabOrFocus(await runtime.getURL(MANAGER_URL), false); } else { - await maybeOpenInTab(await runtime.getURL(MANAGER_URL)); + await maybeOpenInTab(await runtime.getURL(MANAGER_URL), false); } } -export async function openUrls(urls: string) { - const window = await mostRecentBrowser(); +export async function openUrls(urls: string, incognito: boolean) { + const window = await mostRecentBrowser(incognito); for (const url of urls) { try { await tabs.create({ diff --git a/windows/select.ts b/windows/select.ts index 34d681d..9cf89cc 100644 --- a/windows/select.ts +++ b/windows/select.ts @@ -428,17 +428,42 @@ class SelectionTable extends VirtualTable { } openSelection() { - const items = this.items.filter((i, idx) => this.selection.contains(idx)); - if (!items.length) { + const privates: BaseMatchedItem[] = []; + const items = this.items.filter((i, idx) => this.selection.contains(idx)). + filter(i => { + if (i.private) { + privates.push(i); + return false; + } + return true; + }); + if (!items.length && !privates.length) { if (this.focusRow < 0) { return; } - items.push(this.items.at(this.focusRow)); + const item = this.items.at(this.focusRow); + if (item.private) { + privates.push(item); + } + else { + items.push(item); + } + } + + if (items.length) { + PORT.postMessage({ + msg: "openUrls", + urls: items.map(e => e.url), + incognito: false, + }); + } + if (privates.length) { + PORT.postMessage({ + msg: "openUrls", + urls: privates.map(e => e.url), + incognito: true, + }); } - PORT.postMessage({ - msg: "openUrls", - urls: items.map(e => e.url) - }); } applyDeltaTo(delta: ItemDelta[], items: ItemCollection) {