diff --git a/background.ts b/background.ts index 718b2a49..df8d1b2c 100644 --- a/background.ts +++ b/background.ts @@ -22,6 +22,7 @@ interface ContainerInfo { name: string | null; icon?: string; color?: string; + replace: boolean; } function toContainerInfo(value: any): ContainerInfo | undefined { @@ -43,12 +44,15 @@ async function onBeforeRequest( return {}; } + const tab = await browser.tabs.get(request.tabId); + const sourceUrl = tab.url ? new URL(tab.url) : undefined; + const interpreter = new Sval({ ecmaVer: "latest", sourceType: "script", sandBox: true, }); - interpreter.import({ url: new URL(request.url) }); + interpreter.import({ url: new URL(request.url), sourceUrl }); interpreter.run(program); const info = toContainerInfo(interpreter.exports.end); @@ -56,8 +60,6 @@ async function onBeforeRequest( return {}; } - const tab = await browser.tabs.get(request.tabId); - // Open in the default container (no container) if name is null. if (info.name === null) { if (!tab.cookieStoreId || tab.cookieStoreId === DEFAULT_COOKIE_STORE_ID) { @@ -69,7 +71,9 @@ async function onBeforeRequest( cookieStoreId: DEFAULT_COOKIE_STORE_ID, }); - await browser.tabs.remove(request.tabId); + if (info.replace != false) { + await browser.tabs.remove(request.tabId); + } return { cancel: true }; } @@ -97,7 +101,9 @@ async function onBeforeRequest( }); // Close the old tab - await browser.tabs.remove(request.tabId); + if (info.replace !== false) { + await browser.tabs.remove(request.tabId); + } return { cancel: true }; } diff --git a/manifest.json b/manifest.json index 7e241f7a..03c78c0b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "ContainerScript", - "version": "1.2", + "version": "1.3", "description": "User defined scripts for assining URLs to containers", "permissions": [ "", diff --git a/ui.html b/ui.html index 3e089f53..31862682 100644 --- a/ui.html +++ b/ui.html @@ -101,17 +101,44 @@

Container Script

Write JavaScript code to define container rules. - The code runs each time you open a URL, with the url variable (an instance of the URL class) available in scope. + The code runs each time you navigate to a URL. Two variables are available: +

+ +

The code is executed using Sval due to CSP restrictions.

Basic Usage

-

Return a string to open the URL in that container:

+

Return a string to open the URL in that container (replaces the current tab):

if (url.hostname === "www.amazon.com") return "shopping";

Return null to open the URL in the default container (no container):

if (url.hostname === "github.com") return null;

Advanced Configuration

-

Return an object for more control over the container:

-

if (url.toString().includes("bank.com")) return { name: "finance", icon: "fingerprint", color: "blue" };

+

Return an object for more control. Use name (or profile) for the container name, and replace to control whether the original tab is closed:

+

if (url.toString().includes("bank.com")) return { name: "finance", icon: "fingerprint", color: "blue" };

+

Keep the original tab open by setting replace to false:

+

if (url.hostname === "example.com") return { name: "work", replace: false };

+

Use sourceUrl to keep the original tab open when navigating from a specific site (e.g. Google):

+

+let replace = true;
+switch (sourceUrl?.hostname) {
+    case "www.google.com":
+    case "duckduckgo.com":
+    case "www.bing.com":
+        replace = false;
+}
+
+switch (url.hostname) {
+    case "www.youtube.com":
+    case "chatgpt.com":
+    case "www.reddit.com":
+        return { name: "Personal", replace };
+}
+
+return null;
+
diff --git a/ui.ts b/ui.ts index 92e1b2ed..de742a49 100644 --- a/ui.ts +++ b/ui.ts @@ -11,12 +11,16 @@ async function main() { }, }; - // setup the url parameter for auto-complete + // setup auto-complete for available variables const libSource = [ "/**", - " * The URL we're finding a container for", + " * The URL being navigated to", " */", "declare const url: URL", + "/**", + " * The URL of the page that initiated the navigation (may be undefined for new tabs)", + " */", + "declare const sourceUrl: URL | undefined", ].join("\n"); const libUri = "ts:filename/ContainerScript.d.ts"; monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, libUri);