diff --git a/copyurl.js b/copyurl.js index 4260200..c57919e 100644 --- a/copyurl.js +++ b/copyurl.js @@ -1,13 +1,17 @@ -const url = window.location.href; -chrome.commands.onCommand.addListener(function (command) { - if (command === "Copy URL") { - navigator.clipboard.writeText(url).then( - function () { - console.log(`Copied URL: ${url}`); - }, - function (err) { - console.error(`Something went wrong..`, err); - }, - ); - } +function copyUrl() { + document.navigator.clipboard.writeText(tab.url).then( + function () { + console.log(`Copied URL: ${tab.url}`); + }, + function (err) { + console.error(`Something went wrong..`, err); + }, + ); +} + +chrome.action.onClicked.addListener((tab) => { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + func: copyUrl, + }); }); diff --git a/manifest.json b/manifest.json index c421bca..b2a0f86 100644 --- a/manifest.json +++ b/manifest.json @@ -17,5 +17,6 @@ }, "description": "Copy the URL of the current tab" } - } + }, + "permissions": ["clipboardWrite", "clipboardRead", "activeTab", "scripting"] }