This question is different from the other questions in SO concerning copying to the clipboard, as I want to run a function in a bookmarklet and then copy that result to the clipboard. And, is this possible without external libraries and do it within the bookmarklet Javascript?
The bookmarklet below creates a HTML link from the current page, and then opens a new browser tab and puts the HTML link into a text area that can be copied with the keyboard.
But how would I skip that step of opening the window and copying, but go directly to the clipboard? By using document.execCommand('copy')
? Other method? Is it possible to do this without external libraries and do it with bookmarklet Javascript?
Bookmarklet that creates the page link in a new tab:
javascript:function htmlEscape(s){s=s.replace(/&/g,'&');s=s.replace(/>/g,'>');
s=s.replace(/</g,'<');return s;} function linkEscape(s){s=s.replace(/&/g,'&');
s=s.replace(/"/,'"');return s} h = '<a href="' + linkEscape(location.href) + '" target="_blank">
<span class="tooltip" title="' + htmlEscape(document.title) + '">' + htmlEscape(document.title)
+ '</span></a>'; with(window.open().document){write(h+'<form name=f>
<textarea name=a rows=5 cols=80 wrap=hard>'+htmlEscape(h)+'</textarea></form>');
close(); f.a.select(); } void 0
I can see the part that generates the HTML as, it appears, in a variable called h
:
javascript:function htmlEscape(s){s=s.replace(/&/g,'&');s=s.replace(/>/g,'>');
s=s.replace(/</g,'<');return s;} function linkEscape(s){s=s.replace(/&/g,'&');
s=s.replace(/"/,'"');return s} h =
I can see the part of the function that opens the new window:
with(window.open().document){write(h+'<form name=f><textarea name=a rows=5
cols=80 wrap=hard>'+htmlEscape(h)+'</textarea></form>'); close(); f.a.select(); } void 0
How do I write the contents of function - the HTML link - directly to the clipboard?