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.