Open links in the correct window context

This commit is contained in:
Nils Maier 2019-09-10 12:43:47 +02:00
parent 76992bd4f4
commit bf725ece72
4 changed files with 57 additions and 23 deletions

View File

@ -19,6 +19,7 @@ export interface MessageSender {
export interface Tab { export interface Tab {
id?: number; id?: number;
incognito?: boolean;
} }
export interface MenuClickInfo { export interface MenuClickInfo {

View File

@ -187,8 +187,8 @@ export async function select(links: BaseItem[], media: BaseItem[]) {
openPrefs(); openPrefs();
}); });
port.on("openUrls", ({urls}) => { port.on("openUrls", ({urls, incognito}) => {
openUrls(urls); openUrls(urls, incognito);
}); });
try { try {

View File

@ -8,13 +8,16 @@ import DEFAULT_ICONS from "../data/icons.json";
const DONATE_URL = "https://www.downthemall.org/howto/donate/"; const DONATE_URL = "https://www.downthemall.org/howto/donate/";
const MANAGER_URL = "/windows/manager.html"; const MANAGER_URL = "/windows/manager.html";
export async function mostRecentBrowser(): Promise<any> { export async function mostRecentBrowser(incognito: boolean): Promise<any> {
let window; let window;
try { try {
window = await windows.getCurrent({windowTypes: ["normal"]}); window = await windows.getCurrent({windowTypes: ["normal"]});
if (window.type !== "normal") { if (window.type !== "normal") {
throw new Error("not a normal window"); throw new Error("not a normal window");
} }
if (incognito && !window.incognito) {
throw new Error("Not incognito");
}
} }
catch { catch {
try { try {
@ -22,23 +25,28 @@ export async function mostRecentBrowser(): Promise<any> {
if (window.type !== "normal") { if (window.type !== "normal") {
throw new Error("not a normal window"); throw new Error("not a normal window");
} }
if (incognito && !window.incognito) {
throw new Error("Not incognito");
}
} }
catch { catch {
window = Array.from(await windows.getAll({windowTypes: ["normal"]})). 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) { if (!window) {
window = await windows.create({ window = await windows.create({
url: DONATE_URL, incognito: !!incognito,
type: "normal", type: "normal",
}); });
} }
return window; return window;
} }
export async function openInTab(url: string) { export async function openInTab(url: string, incognito: boolean) {
const window = await mostRecentBrowser(); const window = await mostRecentBrowser(incognito);
await tabs.create({ await tabs.create({
active: true, active: true,
url, url,
@ -47,7 +55,7 @@ export async function openInTab(url: string) {
await windows.update(window.id, {focused: true}); 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({ const etabs = await tabs.query({
url url
}); });
@ -57,21 +65,21 @@ export async function openInTabOrFocus(url: string) {
await windows.update(tab.windowId, {focused: true}); await windows.update(tab.windowId, {focused: true});
return; 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({ const etabs = await tabs.query({
url url
}); });
if (etabs.length) { if (etabs.length) {
return; return;
} }
await openInTab(url); await openInTab(url, incognito);
} }
export async function donate() { export async function donate() {
await openInTab(DONATE_URL); await openInTab(DONATE_URL, false);
} }
export async function openPrefs() { export async function openPrefs() {
@ -86,15 +94,15 @@ export async function openManager(focus = true) {
console.error(ex.toString(), ex); console.error(ex.toString(), ex);
} }
if (focus) { if (focus) {
await openInTabOrFocus(await runtime.getURL(MANAGER_URL)); await openInTabOrFocus(await runtime.getURL(MANAGER_URL), false);
} }
else { else {
await maybeOpenInTab(await runtime.getURL(MANAGER_URL)); await maybeOpenInTab(await runtime.getURL(MANAGER_URL), false);
} }
} }
export async function openUrls(urls: string) { export async function openUrls(urls: string, incognito: boolean) {
const window = await mostRecentBrowser(); const window = await mostRecentBrowser(incognito);
for (const url of urls) { for (const url of urls) {
try { try {
await tabs.create({ await tabs.create({

View File

@ -428,17 +428,42 @@ class SelectionTable extends VirtualTable {
} }
openSelection() { openSelection() {
const items = this.items.filter((i, idx) => this.selection.contains(idx)); const privates: BaseMatchedItem[] = [];
if (!items.length) { 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) { if (this.focusRow < 0) {
return; 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) { applyDeltaTo(delta: ItemDelta[], items: ItemCollection) {