Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface ContainerInfo {
name: string | null;
icon?: string;
color?: string;
replace: boolean;
}

function toContainerInfo(value: any): ContainerInfo | undefined {
Expand All @@ -43,21 +44,22 @@ 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);
if (!info) {
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) {
Expand All @@ -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 };
}
Expand Down Expand Up @@ -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 };
}
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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": [
"<all_urls>",
Expand Down
35 changes: 31 additions & 4 deletions ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,44 @@ <h1>Container Script</h1>
<div class="instructions-content">
<p>
Write JavaScript code to define container rules.
The code runs each time you open a URL, with the <code>url</code> variable (an instance of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/URL">URL</a> class) available in scope.
The code runs each time you navigate to a URL. Two variables are available:
</p>
<ul style="color: #34495e; line-height: 1.8; margin-bottom: 15px;">
<li><code>url</code> &mdash; the <a href="https://developer.mozilla.org/en-US/docs/Web/API/URL">URL</a> being navigated to.</li>
<li><code>sourceUrl</code> &mdash; the URL of the page that initiated the navigation (may be <code>undefined</code> for new tabs).</li>
</ul>
<p>
The code is executed using <a href="https://github.com/Siubaak/sval">Sval</a> due to <a href="https://www.w3.org/TR/CSP/">CSP</a> restrictions.
</p>
<h3>Basic Usage</h3>
<p>Return a string to open the URL in that container:</p>
<p>Return a string to open the URL in that container (replaces the current tab):</p>
<p><code>if (url.hostname === "www.amazon.com") return "shopping";</code></p>
<p>Return <code>null</code> to open the URL in the default container (no container):</p>
<p><code>if (url.hostname === "github.com") return null;</code></p>
<h3>Advanced Configuration</h3>
<p>Return an object for more control over the container:</p>
<p><code>if (url.toString().includes("bank.com")) return { name: "finance", icon: "fingerprint", color: "blue" }; </code></p>
<p>Return an object for more control. Use <code>name</code> (or <code>profile</code>) for the container name, and <code>replace</code> to control whether the original tab is closed:</p>
<p><code>if (url.toString().includes("bank.com")) return { name: "finance", icon: "fingerprint", color: "blue" };</code></p>
<p>Keep the original tab open by setting <code>replace</code> to <code>false</code>:</p>
<p><code>if (url.hostname === "example.com") return { name: "work", replace: false };</code></p>
<p>Use <code>sourceUrl</code> to keep the original tab open when navigating from a specific site (e.g. Google):</p>
<pre style="background-color: #f8f9fa; padding: 12px 16px; border-radius: 3px; font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace; font-size: 0.9em; color: #e83e8c; line-height: 1.6; overflow-x: auto;"><code>
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;
</code></pre>
</div>
</details>
</div>
Expand Down
8 changes: 6 additions & 2 deletions ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down