10

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.

pgr
  • 898
  • 11
  • 27
  • As there's no answer yet, I can advise to use the free 'iMacros for Firefox' extension. It would let you get the clipboard content by means of the built-in `!CLIPBOARD` variable. A helpful feature to take advantage of. – Shugar Dec 26 '18 at 18:50
  • What steps should I take in your _demo site_ to test this? Also, can you provide more of your code as an example? – Castro Roy Dec 27 '18 at 00:54
  • After logging in to the demo site, any bookmarklet that can produce a navigation to this URL should work: `index.php?module=Accounts&action=DetailView&record=84f1bb99-7017-e8dc-94f9-5c179da9f102`. It should show you a specific detail view of an Account. Of course, the whole point is that `84f1bb99-7017-e8dc-94f9-5c179da9f102` should be coming from the Clipboard. About my code, I tried messing with a few samples I picked up from the other answers, but I probably broke them all beyond help. I am not a Javascript guy. – pgr Dec 27 '18 at 16:27
  • Let me correct myself, sorry. It seems that public demo (it's not mine) refreshes periodically, and the ids get reassigned, so the ids I've been giving here get deprecated. To pick up a fresh id that is valid please go to `https://demo.suiteondemand.com/index.php?module=Accounts&action=ListView` and get one from the URL's into the specific records listed there. – pgr Dec 27 '18 at 16:29
  • Can you clarify why you want to use clipboard data for the value of id? – Josh KG Dec 27 '18 at 18:37
  • @JoshKG it's simple: "manual integration". A value coming from one system, and going into the other via copy-paste. This is never a good technical option, but it is sometimes is the best option available when office politics are sc$#"wing up poor users. – pgr Dec 27 '18 at 19:50

3 Answers3

1

Sadly bookmarklets nowadays usually run in the same context as active page, so it shares same restrictions. Currently JavaScript in page can only have access to clipboard when several conditions are met:

  • page is served from secure origin,
  • (when implemented in browser) explicit clipboard-read CSP policy was in HTTP headers,
  • handler accessing clipboard can be tracked to event raised by user interaction (click, keypress).

Currently the last point can never be satisfied, since clicking the bookmarklet happens "outside" the page and there is no event to check.


Workaround

As workaround I'd suggest using old stupid prompt('Enter ID') in place where you need your clipboard data, so it would only add CtrlV, Enter to the process.

myf
  • 9,874
  • 2
  • 37
  • 49
0

Clipboard copy cannot works from scripts. It must comes from an user action.

Similary, in the same way, you can't call a fullscreen from a bookmarklet.

From the Firefox console:

document.execCommand(‘cut’/‘copy’) was denied because it was not
called from inside a short running user-generated event handler.
NVRM
  • 11,480
  • 1
  • 88
  • 87
  • The bookmarklet is activated by a user action. The user clicks on a button on the browser's chrome. Is there an event handler for that? – pgr Dec 28 '18 at 09:38
0

I try this method and it works:

SAME WINDOWS:

javascript:location.href='https://www.ricerca.com?search%27+escape(location.href)

NEW WINDOWS: (window.location.href and window.open () methods in JavaScript) window.open()

javascript:window.open("https://www.ricerca.com?search="+window.getSelection());
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • How is this referring to the clipboard? The clipboard is not the same thing as the selected text in the browser. The clipboard contents can come from a different app... – pgr Jan 11 '23 at 12:21