diff options
author | Simon Parri <simonparri@ganzeria.com> | 2025-09-23 09:10:45 -0500 |
---|---|---|
committer | Simon Parri <simonparri@ganzeria.com> | 2025-09-23 09:12:10 -0500 |
commit | 0bbe831bcd186bd36b5f7b6b1c239a90c7dca5dd (patch) | |
tree | 9a8e8f51158a538d0e3ed882641ed6d1ded51655 /extension | |
parent | cdf0f2e2dc7dba28c8e82ddb6842a4efc63547b2 (diff) | |
download | TabFS-0bbe831bcd186bd36b5f7b6b1c239a90c7dca5dd.tar.gz TabFS-0bbe831bcd186bd36b5f7b6b1c239a90c7dca5dd.zip |
Add tabs/by-id/*/capture-url.txt for current URL+text fragment
Diffstat (limited to 'extension')
-rw-r--r-- | extension/background.js | 10 | ||||
-rw-r--r-- | extension/currentURL.js | 11 |
2 files changed, 21 insertions, 0 deletions
diff --git a/extension/background.js b/extension/background.js index c6cf5b8..1b8960a 100644 --- a/extension/background.js +++ b/extension/background.js @@ -326,6 +326,10 @@ Routes["/tabs/by-id"] = { return (await browser.tabs.executeScript(tabId, {code}))[0]; }); + const routeFromScriptFile = file => makeRouteWithContents(async ({tabId}) => { + return (await browser.tabs.executeScript(tabId, {file}))[0]; + }); + Routes["/tabs/by-id/#TAB_ID/url.txt"] = { description: `Text file containing the current URL of this tab.`, usage: ['cat $0', @@ -349,6 +353,12 @@ Routes["/tabs/by-id"] = { ...routeFromScript(`document.body.innerHTML`) }; + Routes["/tabs/by-id/#TAB_ID/capture-url.txt"] = { + description: `Text file containing the current URL + text fragment of this tab.`, + usage: 'cat $0', + ...routeFromScriptFile("/currentURL.js") + }; + Routes["/tabs/by-id/#TAB_ID/active"] = { description: 'Text file containing `true` or `false` depending on whether this tab is active in its window.', usage: ['cat $0', diff --git a/extension/currentURL.js b/extension/currentURL.js new file mode 100644 index 0000000..0aeab92 --- /dev/null +++ b/extension/currentURL.js @@ -0,0 +1,11 @@ +function encode(str) { + return encodeURIComponent(str).replace("-", "%2D") +} + +let sel = window.document.getSelection(), + ctx = sel?.anchorNode?.parentElement?.textContent?.split(sel), + anc = sel.toString() ? `:~:text=${ctx[0] ? encode(ctx[0])+"-," : ""}${sel}${ctx[1] ? ",-"+encode(ctx[1]) : ""}` : "" + +let url = new URL(window.location) +if (anc) url.hash = anc +url.toString() |