basic impl

This commit is contained in:
setop 2024-10-24 14:16:52 +02:00
commit ddd6e235d8
4 changed files with 60 additions and 0 deletions

24
README.md Normal file
View File

@ -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 "/runtime/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

20
background.js Normal file
View File

@ -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);
});
}
});

BIN
icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

16
manifest.json Normal file
View File

@ -0,0 +1,16 @@
{
"manifest_version": 2,
"name": "Text Fragment Copier",
"version": "1.0",
"permissions": [
"contextMenus",
"clipboardWrite",
"activeTab"
],
"background": {
"scripts": ["background.js"]
},
"icons": {
"48": "icon.png"
}
}