diff --git a/core_js/context_menu.js b/core_js/context_menu.js index d6e8ea0..24fe231 100644 --- a/core_js/context_menu.js +++ b/core_js/context_menu.js @@ -18,7 +18,8 @@ /*jshint esversion: 6 */ /* -* This script is responsible for context menu cleaning functions. +* This script is responsible for context menu cleaning functions +* and based on: https://github.com/mdn/webextensions-examples/tree/master/context-menu-copy-link-with-types */ browser.contextMenus.create({ @@ -30,8 +31,23 @@ browser.contextMenus.create({ browser.contextMenus.onClicked.addListener((info, tab) => { if (info.menuItemId === "copy-link-to-clipboard") { const url = contextCleaning(info.linkUrl); - navigator.clipboard.writeText(url).then(() => { - console.log("[ClearURLs] Copied cleaned url to clipboard."); + const code = "copyToClipboard(" + + JSON.stringify(url)+");"; + + browser.tabs.executeScript({ + code: "typeof copyToClipboard === 'function';", + }).then((results) => { + if (!results || results[0] !== true) { + return browser.tabs.executeScript(tab.id, { + file: "external_js/clipboard-helper.js", + }); + } + }).then(() => { + return browser.tabs.executeScript(tab.id, { + code, + }); + }).catch((error) => { + console.error("Failed to copy text: " + error); }); } }); diff --git a/external_js/clipboard-helper.js b/external_js/clipboard-helper.js new file mode 100644 index 0000000..49aa673 --- /dev/null +++ b/external_js/clipboard-helper.js @@ -0,0 +1,16 @@ +/* + * Source: https://github.com/mdn/webextensions-examples/tree/master/context-menu-copy-link-with-types + */ +function copyToClipboard(text) { + function oncopy(event) { + document.removeEventListener("copy", oncopy, true); + + event.stopImmediatePropagation(); + + event.preventDefault(); + event.clipboardData.setData("text/plain", text); + } + document.addEventListener("copy", oncopy, true); + + document.execCommand("copy"); +} diff --git a/manifest.json b/manifest.json index 745e899..270f78a 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "ClearURLs", - "version": "1.5.7.1", + "version": "1.5.8", "author": "Kevin R.", "description": "Remove tracking elements form URLs.", "homepage_url": "https://gitlab.com/KevinRoebert/ClearUrls",