Skip to content

Commit

Permalink
OpenNewTab: Support Vivaldi (#89)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Lydell <[email protected]>
  • Loading branch information
sctice and lydell authored Aug 17, 2024
1 parent cfc03d9 commit 8ea1b53
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
7 changes: 7 additions & 0 deletions @types/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ interface Navigator {
} | null;
}

// This is used to identify the Vivaldi browser.
declare namespace browser.tabs {
export interface Tab {
vivExtData?: unknown;
}
}

interface ShadowRoot {
elementFromPoint: Document["elementFromPoint"];
elementsFromPoint: Document["elementsFromPoint"];
Expand Down
30 changes: 23 additions & 7 deletions src/background/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
splitEnteredText,
} from "../shared/main";
import type {
ChromiumVariant,
FromBackground,
FromOptions,
FromPopup,
Expand Down Expand Up @@ -1214,13 +1215,19 @@ export default class BackgroundProgram {
url
);
} else if (BROWSER === "chrome") {
this.sendWorkerMessage(
{
type: "OpenNewTab",
url,
foreground,
},
{ tabId, frameId: TOP_FRAME_ID }
fireAndForget(
getChromiumVariant().then((chromiumVariant) => {
this.sendWorkerMessage(
{
type: "OpenNewTab",
url,
foreground,
chromiumVariant,
},
{ tabId, frameId: TOP_FRAME_ID }
);
}),
"BackgroundProgram#openNewTab->getChromiumVariant"
);
} else {
fireAndForget(
Expand Down Expand Up @@ -2315,6 +2322,15 @@ export default class BackgroundProgram {
}
}

// Copied from: https://stackoverflow.com/a/77047611
async function getChromiumVariant(): Promise<ChromiumVariant> {
const tabs = await browser.tabs.query({
active: true,
currentWindow: true,
});
return tabs[0]?.vivExtData !== undefined ? "vivaldi" : "chrome";
}

function makeEmptyTabState(tabId: number | undefined): TabState {
const tabState: TabState = {
hintsState: {
Expand Down
3 changes: 3 additions & 0 deletions src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export type FromWorker =
type: "WorkerScriptAdded";
};

export type ChromiumVariant = "chrome" | "vivaldi";

export type ToWorker =
| {
type: "ClickElement";
Expand All @@ -129,6 +131,7 @@ export type ToWorker =
type: "OpenNewTab";
url: string;
foreground: boolean;
chromiumVariant: ChromiumVariant;
}
| {
type: "ReverseSelection";
Expand Down
33 changes: 26 additions & 7 deletions src/worker/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
walkTextNodes,
} from "../shared/main";
import type {
ChromiumVariant,
FromBackground,
FromWorker,
ToBackground,
Expand Down Expand Up @@ -458,15 +459,10 @@ export default class WorkerProgram {
// This technique does not seem to work in Firefox, but it's not needed
// there anyway (see background/Program.ts).
case "OpenNewTab": {
const { url, foreground } = message;
const link = document.createElement("a");
link.href = url;
link.href = message.url;
link.dispatchEvent(
new MouseEvent("click", {
ctrlKey: true,
metaKey: true,
shiftKey: foreground,
})
new MouseEvent("click", getOpenNewTabModifiers(message))
);
break;
}
Expand Down Expand Up @@ -1862,3 +1858,26 @@ function temporarilySetFilter(
},
};
}

function getOpenNewTabModifiers({
chromiumVariant,
foreground,
}: {
chromiumVariant: ChromiumVariant;
foreground: boolean;
}): MouseEventInit {
switch (chromiumVariant) {
case "chrome":
return {
ctrlKey: true,
metaKey: true,
shiftKey: foreground,
};
case "vivaldi":
return {
ctrlKey: !foreground,
metaKey: true,
shiftKey: foreground,
};
}
}

0 comments on commit 8ea1b53

Please sign in to comment.