0

How can I copy the contents of the browser's address bar to the user's clipboard using javascript / jquery? I'm trying to make an extension for Chrome and Firefox which adds a hotkey for that.

I figured [F6] was an easy target for the hotkey because it already selects the contents of the address bar in both browsers. So I tried:

(function() {
    $(document).keyup(function(e) {
        // [F6] selects and copies address bar content.
        if (e.keyCode == 117){
            document.execCommand('copy');
        }
    });
})();

However this doesn't copy it. Next I tried to get the url trough window.location.href, but I still have to copy it to the clipboard. So far the methods I found for getting a variable into the clipboard involve adding elements to a page. Seeing as it's for an extension and not a website I would prefer it doesn't modify pages.

I'm new to javascript and extension development so I'm in the dark on how I should go about this. Any help would be much appreciated.

  • 1
    You can do it inside an event page which is a hidden background page so you won't modify the web page. Use chrome.commands API to declare a hotkey, chrome.tabs API to get the URL, don't use content scripts. – wOxxOm Mar 16 '18 at 19:45

1 Answers1

0

You can see what is done here : How to copy URL on button click? Maybe you will need an hidden field to put the url before copying it.