url-text-fragment/background.js

21 lines
617 B
JavaScript
Raw Normal View History

2024-10-24 12:16:52 +00:00
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);
});
}
});