1

I am trying to write a userscript that will remove the effects of onselectstart from an id on a page.

So far I am assuming that I can rebind the original function using something like this:

document.getElementById('nodrag').onselectstart = function() { /* code original action */ };

JSFiddle

Any ideas would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
onmylemon
  • 724
  • 8
  • 25
  • You want to do something before/after the original selectStart function? It's not totally clear to me. If so, you can store the old selectStart event handler and call it wherever you want inside the new selectStart handler. – Niccolò Campolungo Jul 04 '13 at 12:00
  • Hi LightStyle, this would be after. The script I am writing will be a userscript (greasemonkey) and will allow the selection of text on a page where the creator of the page has tried to not allow it. Thanks. – onmylemon Jul 04 '13 at 12:02
  • Wrap your function inside `window.onload`, it should work – Niccolò Campolungo Jul 04 '13 at 12:03
  • Thanks, it appears that isn't doing it either. What I am looking for is some code that will bring back the ability to select text. http://jsfiddle.net/onmylemon/V3SAt/2/ – onmylemon Jul 04 '13 at 12:06
  • 1
    Have you any possibilities to load the script at the end of the page? If you can, you can also don't wrap the code in window.onload and leave the line as it is(try this just in JSFiddle, in "framework and extensions" on the left select "no wrap - in ``" instead of "onload" – Niccolò Campolungo Jul 04 '13 at 12:24

1 Answers1

4

Looks like I managed to work out the answer. Thank you LightStyle for giving me the hints.

So I create an script tag with document.getElementById('nodrag').onselectstart = function() { }; inside it and append this to the body.

// Create the script to remove the nodrag from chrome
var script = document.createElement('script');
script.type='text/javascript';
script.innerHTML = "document.getElementById('nodrag').onselectstart = function() {  };";
var body = document.getElementsByTagName('body')[0];
body.appendChild(script);

This might not work in traditional contexts however works a charm on a userscript. Here is the final script:

https://gist.github.com/lgoldstien/5928021

onmylemon
  • 724
  • 8
  • 25
  • I asked how to make it to work with `body` tag too `` , and I was able to disable it now with this `script.innerHTML = "document.getElementsBytagName('body')[0].onselectstart = function() { };";` – Salem Mar 14 '22 at 00:13