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 {
id?: number;
incognito?: boolean;
}
export interface MenuClickInfo {

View File

@ -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 {

View File

@ -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<any> {
export async function mostRecentBrowser(incognito: boolean): Promise<any> {
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<any> {
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({

View File

@ -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) {