diff --git a/background.js b/background.js new file mode 100644 --- /dev/null +++ b/background.js @@ -0,0 +1,32 @@ +const templates = { + "youtube.com": ["v", "t", "start", "list", "index"], + "google.com": ["q"], + "bing.com": ["q"], + "duckduckgo.com": ["q"], + "wikipedia.org": ["title", "oldid", "diff"], +}; + +chrome.action.onClicked.addListener(async (tab) => { + if (!tab.url || !/^https?:/.test(tab.url)) return; + + const url = new URL(tab.url); + const template = + Object.entries(templates).find( + ([domain]) => + url.hostname === domain || url.hostname.endsWith(`.${domain}`), + )?.[1] || []; + const parameters = [...url.searchParams]; + url.search = ""; + for (const [name, value] of parameters) { + if (template.includes(name)) url.searchParams.append(name, value); + } + + await chrome.offscreen + .createDocument({ + url: "offscreen.html", + reasons: ["CLIPBOARD"], + justification: "Copy detracked URL when button is clicked", + }) + .catch(() => {}); + await chrome.runtime.sendMessage({ type: "copy", text: url.href }); +}); diff --git a/icon-128.png b/icon-128.png new file mode 100644 --- /dev/null +++ b/icon-128.png diff --git a/icon-16.png b/icon-16.png new file mode 100644 --- /dev/null +++ b/icon-16.png diff --git a/icon.svg b/icon.svg new file mode 100644 --- /dev/null +++ b/icon.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/manifest.json b/manifest.json new file mode 100644 --- /dev/null +++ b/manifest.json @@ -0,0 +1,18 @@ +{ + "manifest_version": 3, + "name": "Detrack", + "version": "1.0.0", + "permissions": ["activeTab", "offscreen", "clipboardWrite"], + "background": { "service_worker": "background.js" }, + "action": { + "default_title": "Copy detracked URL", + "default_icon": { + "16": "icon-16.png", + "128": "icon-128.png" + } + }, + "icons": { + "16": "icon-16.png", + "128": "icon-128.png" + } +} diff --git a/offscreen.html b/offscreen.html new file mode 100644 --- /dev/null +++ b/offscreen.html @@ -0,0 +1,4 @@ + + + + diff --git a/offscreen.js b/offscreen.js new file mode 100644 --- /dev/null +++ b/offscreen.js @@ -0,0 +1,12 @@ +chrome.runtime.onMessage.addListener( + ({ type, text }, _sender, sendResponse) => { + if (type !== "copy") return; + const textarea = document.createElement("textarea"); + textarea.value = text; + document.body.append(textarea); + textarea.select(); + document.execCommand("copy"); + textarea.remove(); + sendResponse(); + }, +);