Here's my code.

javascript:x=document.createElement("script");x.src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"; document.getElementsByTagName("head")[0].appendChild(x);copy(1)

Is this the correct way? Are there any flaws in it?



doesn't work by itself so that's not a possibility, and

javascript:x=document.createElement("script");x.src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"; document.getElementsByTagName("head")[0].appendChild(x);copy(1)

appears to be the best so far.

  • \$\begingroup\$ Is the expected result for data to be copied to the clipboard without user action? \$\endgroup\$ – guest271314 Jan 29 '19 at 8:24
  • \$\begingroup\$ Welcome to Code Review! Can you confirm that the code functions correctly? If so, I recommend that you edit to add a summary of the testing (ideally as reproducible unit-test code). If it's not working, it isn't ready for review (see help center) and the question may be deleted. \$\endgroup\$ – Toby Speight Jan 29 '19 at 8:54
  • \$\begingroup\$ @guest271314 yes, after clicking the bookmarklet \$\endgroup\$ – not legit Jan 29 '19 at 16:16
  • \$\begingroup\$ @TobySpeight It works, how would I give proof? \$\endgroup\$ – not legit Jan 29 '19 at 16:17
  • \$\begingroup\$ Just edit to say how you've tested it, so that it's clear that it's not an off-topic question. BTW, does the code need to be all on a single line? It might be more readable if split across lines. \$\endgroup\$ – Toby Speight Jan 29 '19 at 16:22

Looks fine, except for polluting the namespace, you could consider using a self executing anonymous function.

javascript:(function(){x=document.createElement("script");x.src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"; document.getElementsByTagName("head")[0].appendChild(x);copy(1);})();
  • \$\begingroup\$ Have you tried the code at the answer? Uncaught ReferenceError: copy is not defined \$\endgroup\$ – guest271314 Jan 30 '19 at 2:12
  • \$\begingroup\$ I had, it seemed to work just fine for me. His form, and the self executing form did not throw an error for me. But I now see that my browser has a copy function [Command Line API] even before running the bookmarklet. That does not take away my feedback on polluting the namespace though. \$\endgroup\$ – konijn Jan 30 '19 at 11:41
  • \$\begingroup\$ @konijn it seems to work only in the console. have your tried in the bookmarklets? \$\endgroup\$ – not legit Jan 31 '19 at 20:32
  • \$\begingroup\$ There is this: stackoverflow.com/a/106438/7602 \$\endgroup\$ – konijn Feb 1 '19 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.