32

I recently came across a website that disabled text selection, preventing anyone from easily copying and pasting text. I have a bookmarklet that disables similar attempts to block context menus using JavaScript, and I'm wondering if it would be possible to do something similar for text selection.

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //For IE
    target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //For Firefox
    target.style.MozUserSelect="none"
else //All other route (For Opera)
    target.onmousedown=function(){return false}
target.style.cursor = "default"
}

Elsewhere the function is called with disableSelection(document.body).

The solution from my context menu bookmarklet is also probably necessary:

javascript:void(document.onmousedown=null);
void(document.onclick=null);
void(document.oncontextmenu=null)

Finally, I had seen elsewhere on StackOverflow that CSS could also be used:

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;

Is there a method to fight all of these at once and end this tyranny over my browser? How would I both enable MozUserSelect/SelectStart for all elements and set the CSS properties?

0

8 Answers 8

41

Check out the Enable All Text Selection bookmarklet by Alan Hogan. The only issue with the bookmarklet is that it does not handle frames/iframes (that's a browser security thing so it's unlikely something can be done about it).

As an added bonus, it also enables the mouse right-click event on pages that block it.

Create a bookmark (e.g. by dragging the icon to the left of the URL for any page to your bookmarks bar), right-click and select Edit, rename to something meaningful, and insert the following code in the URL field:

javascript:(function(){function%20allowTextSelection(){window.console&&console.log('allowTextSelection');var%20style=document.createElement('style');style.type='text/css';style.innerHTML='*,p,div{user-select:text%20!important;-moz-user-select:text%20!important;-webkit-user-select:text%20!important;}';document.head.appendChild(style);var%20elArray=document.body.getElementsByTagName('*');for(var%20i=0;i<elArray.length;i++){var%20el=elArray[i];el.onselectstart=el.ondragstart=el.ondrag=el.oncontextmenu=el.onmousedown=el.onmouseup=function(){return%20true};if(el%20instanceof%20HTMLInputElement&&['text','password','email','number','tel','url'].indexOf(el.type.toLowerCase())>-1){el.removeAttribute('disabled');el.onkeydown=el.onkeyup=function(){return%20true};}}}allowTextSelection();})();

To make the bookmarklet code readable you can use the Bookmarkelt Builder at http://subsimple.com/bookmarklets/jsbuilder.htm - just paste the minified bookmarklet text and click the Format button.

3
  • Glad it was helpful — and thanks for sharing the bookmarklet builder link!
    – Alan H.
    Commented Mar 30, 2013 at 4:09
  • 1
    if add also document.oncontextmenu=null; will complete that.
    – albanx
    Commented Feb 15, 2015 at 21:42
  • 1
    Update: This bookmarklet source code is on GitHub. It has been improved and updated. Pull requests are welcome! github.com/alanhogan/bookmarklets
    – Alan H.
    Commented Sep 14, 2016 at 17:42
10

Had the same issue with a website.

CSS can't solve this problem as Javascript comes into play whenever you try to select the text.

There are two ways to solve this 1) Disable Javascript on your web browser. Check this out for reference. http://browsers.about.com/od/googlechrome/ss/disable-javascript-chrome-windows.htm 2) Open the javascript console. I am using chrome (click shift+command+C on Mac, f12 on Ubuntu and Windows)

copy this code document.body.onselectstart = function() {return true;}; and paste it in the console, and hit enter.

2
  • 1
    The accepted answer didn't work on chrome for me. But document.body.onselectstart did the trick. Thanks!
    – dtroy
    Commented Oct 8, 2017 at 1:44
  • Just love that, simple and elegant! :)
    – mitkosoft
    Commented Mar 22, 2018 at 19:32
4

if you are on google chrome you can use this extension

https://chrome.google.com/webstore/detail/enable-selection/jehoagbopeaefibnihnfgenfcilmcikj/

It enables text selection and the right click if it's blocked

0
2

Agree with jfriend00. It's good to avoid those problem website. Unless if it's really necessarily to stock to the site... Then you might consider trying this:

Disable browser Java Script for the problem website might work too. http://browsers.about.com/od/googlechrome/ss/disable-javascript-chrome-windows.htm

1

Web sites can always find ways to be obnoxious. All they have to do is put transparent images over things or put text in images and you'll be slowed down a bit.

Give up worrying about it. Firefox 4+ gives you control over right-click again so apps can't wrest it away from you. If you think a site is being that obnoxious, then stop using it and support some other site that isn't being that obnoxious or stop trying to take things they don't want you to take. Unless the page is all dynamically built from JS, you can always get text from the source of the page. I say you should stop using sites that annoy you. If everyone did that, they'd have to change their ways.

3
  • I'm aware that I can always pull text from the source. I was hoping to write a bookmarklet for myself that would solve these problems when they came up, much like the context button bookmarklet I mentioned.
    – Patrick
    Commented Aug 18, 2011 at 3:24
  • I'm telling you that you can probably identify a specific technique used to annoy you and find a work-around for it, but there are so many ways to annoy that it is not feasible to try to proactively work-around all of them in advance. I'm really not interested in tracking down a solution to the page you referenced. It's just too obnoxious. I wouldn't patronize them.
    – jfriend00
    Commented Aug 18, 2011 at 3:46
  • Things like transparent images, are easily solved by disabling the page CSS; I have made a bookmarklet that "humanizes" a page by setting standard colors and text sizes, as well as disabling most of the css. It will allow for example to save an image behind a transparent gif on flickr: gist.github.com/1485798
    – ccpizza
    Commented Dec 16, 2011 at 12:04
0

Seen the same annoying js and more on another site:

<script type="text/javascript" language="JavaScript">
function disableText(e){
  return false
}
function reEnable(){
  return true
}
//For browser IE4+
document.onselectstart = new Function ("return false")

//For browser NS6
if (window.sidebar){
  document.onmousdown = disableText
  document.onclick = reEnable
}
</script>
<script language="JavaScript1.2">
var msgpopup="";
function pmb(){
      if(alertVis == "1") alert(message);
          if(closeWin == "1") self.close();
          return false;
}
function IE() {
     if (event.button == "2" || event.button == "3"){pmb();}
}
function NS(e) {
     if (document.layers || (document.getElementById && !document.all)){
          if (e.which == "2" || e.which == "3"){ pmb();}
     }
}
document.onmousedown=IE;document.onmouseup=NS;document.oncontextmenu=new Function("alert(msgpopup);return false")

</script>
<script type="text/javascript">
function disableSelection(target){
if (typeof target.onselectstart!="undefined") //For IE
    target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //For Firefox
    target.style.MozUserSelect="none"
else //All other route (For Opera)
    target.onmousedown=function(){return false}
target.style.cursor = "default"
}

</script>

Note that all the event handlers are either attached to document manually and disableSelection(document.body). So the script referenced by @alan-h that allows mouse events also needs to be updated to run on document and document.body. (re: el.onselectstart = el.ondragstart = el.ondrag = el.oncontextmenu = el.onmousedown = el.onmouseup = function(){return true};)

0

So I tried all above answers but none worked for me.

Later I opened the same link in the Microsoft Edge browser.

That did the trick. Of course, you must be on a computer and Edge should be installed. Please try above answers first.

0

Only for Chrome: I tried several chrome extension, but only this one worked and enable selection of text on the website that blocks selection.

https://chrome.google.com/webstore/detail/allow-select-and-copy/lamaakaemgdclpnfbofmhpkanfnojjch/related

Don't forget to press Alt+Shift+A to activate ultra mode. You can see message for activation on Developers tool > console (i.e ultra true).

Note: Each website employs different method to block text-selection. That's why you need to try some of the extension available that can tackle the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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