Sunday, 23 December 2018

Bookmarklet to make URL from Clipboard content

I'm trying to make a Bookmarklet to grab an id value from the Clipboard, and navigate to a URL that is built with that id.

javascript:(function(){  
 window.location="index.php?module=Accounts&action=DetailView&record=" 
                 + clipboardData.getData('Text');
})()

(this is only supposed to work when clicked on a specific site that is expecting that URL form)

The basics of the Bookmarklet are working fine, the tricky part is getting the Clipboard value, because clipboardData is not working.

I am using Firefox v64 (although I would like this to be generic across more browsers, at least modern ones).

Now, upon searching about this issue I realize what I'm trying to do is not as simple as it seems - clipboard API's in browsers are a tricky issue. I found several answers about this, the best one seems to be this:

JavaScript get clipboard data on paste event (Cross browser)

I also tried this one but couldn't get it to work either: https://stackoverflow.com/a/27908501/1189711

My question here is: are any of those techniques applicable in a Bookmarklet? If so, I would appreciate some help with this. My skills in Javascript are too low to understand how to translate these answers to my case - namely the asynchronous stuff.

PS - if someone wants a place to test this, just put 84f1bb99-7017-e8dc-94f9-5c179da9f102 in your clipboard and try it on this demo site, credentials will/will.



from Bookmarklet to make URL from Clipboard content

No comments:

Post a Comment