commit 876a5b5928dae7252244793c60478b9e79f26947 Author: setop Date: Thu Oct 24 14:16:52 2024 +0200 basic impl diff --git a/README.md b/README.md new file mode 100644 index 0000000..b1fab02 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +This is a Firefox Addon that allows to create a ["Text Fragment"](https://developer.mozilla.org/en-US/docs/Web/URI/Fragment/Text_fragments#:%7E:text=Text%20fragments%20allow%20linking%20directly%20to%20a%20specific%20portion%20of%20text%20in%20a%20web%20document) link for the context menu. + + +# installation + +* git clone or export this repo +* go to page "about:debugging#/runtime/this-firefox" (or "about:debugging" then click "This Firefox") +* click on "Load Temporary Add-on…" +* select "manifest.json" file + +# usage + +* select a text in a document, +* right clic to have contextual menu, +* choose "text fragment" entry +* paste the link in an address bar or in a mail or in a chat + +# limitation + +* not packaged, only debug mode yet + * on the plus side, the code is so simple that you can see it won't stab you in the back +* tested only on Firefox + * the other, most popular, web browser is on the evil side for a long time now + diff --git a/background.js b/background.js new file mode 100644 index 0000000..5b0da0c --- /dev/null +++ b/background.js @@ -0,0 +1,20 @@ +browser.contextMenus.create({ + id: "text-fragment", + title: "Text Fragment", + contexts: ["selection"] +}); + +browser.contextMenus.onClicked.addListener((info) => { + if (info.menuItemId === "text-fragment") { + const selectedText = encodeURIComponent(info.selectionText); + const pageUrl = info.pageUrl.split('#')[0]; // Remove existing fragment, if any + const textToCopy = `${pageUrl}#:~:text=${selectedText}`; + + navigator.clipboard.writeText(textToCopy).then(() => { + console.log("Text copied to clipboard"); + }).catch(err => { + console.error("Could not copy text: ", err); + }); + } +}); + diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..d26746a --- /dev/null +++ b/icon.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..7a97b05 --- /dev/null +++ b/manifest.json @@ -0,0 +1,20 @@ +{ + "manifest_version": 2, + "name": "Text Fragment Copier", + "version": "1.0", + "permissions": [ + "contextMenus", + "clipboardWrite", + "activeTab" + ], + "background": { + "scripts": ["background.js"] + }, + "icons": { + "32": "icon.svg", + "48": "icon.svg", + "64": "icon.svg", + "96": "icon.svg", + "128": "icon.svg" + } +}