Change to old clipboard copy technique, to support chrome and older firefox versions

This commit is contained in:
Kevin Röbert 2019-04-10 19:08:07 +02:00
parent cfd3bf5f43
commit 1b6cc37bdd
3 changed files with 36 additions and 4 deletions

View File

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

View File

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

View File

@ -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",