1

I want to copy some content from a website that has copy disabled by this part

$(document).bind('copy', function(e){
      return false;
});

I tried to unbind the function with $(document).off('copy'), and it works flawlessly in the console, but when in userscript it does not work.

I used Tampermonkey userscript to create a button to copy all content from a class with these lines

// Everything in this code block works in the console...
$(document).off('copy');
range.selectNode(document.getElementsByClassName('bbWrapper')[0]);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();

Already have this in header // @require https://code.jquery.com/jquery-3.6.0.min.js

Also done this var $ = window.jQuery;

PhanLong
  • 43
  • 6
  • 1
    Remove `@require` and add `// @grant none` so the script will use jquery of the site. – wOxxOm Dec 12 '22 at 12:52
  • @wOxxOm `// @grant none` has been there the whole time. I didn't delete it. I managed to get it right by using `https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js` instead of `https://code.jquery.com/jquery-3.6.0.min.js`. Thanks to this answer [here](https://stackoverflow.com/a/11852406/20301238) – PhanLong Dec 13 '22 at 11:54
  • Note that `@grant none` is ignored if you have other `@grant` values. – wOxxOm Dec 13 '22 at 12:18
  • There are no other grant values. I didn't add anything other than the require line. – PhanLong Dec 13 '22 at 12:45
  • In that case there should be no need to require jquery. – wOxxOm Dec 13 '22 at 13:06
  • @wOxxOm It said `$ is not defined` (or something like that, I don't remember). When using `window.jQuery`, it said `window.jQuery is not a function`. It only work if I `@require` a jQuery.js from somewhere else. – PhanLong Dec 14 '22 at 14:14
  • It means the site doesn't have jQuery or it uses `jQuery` not `$` or you have another `@grant` or there's a typo. An alternative method of accessing site's jquery is `unsafeWindow.jQuery`. – wOxxOm Dec 14 '22 at 14:27

1 Answers1

0

For some reason I can't use https://code.jquery.com/jquery-3.6.0.min.js. But thanks to this answer here, I changed the link to https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js. And it worked.

double-beep
  • 5,031
  • 17
  • 33
  • 41
PhanLong
  • 43
  • 6