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