使用 JavaScript 启用被阻止的文本选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7101982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:58:49  来源:igfitidea点击:

Enabling blocked text selection using JavaScript

javascriptcss

提问by Patrick

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.

我最近遇到了一个禁用文本选择网站,防止任何人轻松复制和粘贴文本。我有一个书签,它禁止使用 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"
}

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

在其他地方,该函数用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 StackOverflowthat CSS could also be used:

最后,我在 StackOverflow 的其他地方看到也可以使用 CSS:

-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/SelectStartfor all elements and set the CSS properties?

有没有一种方法可以同时打击所有这些并结束对我的浏览器的这种暴政?我将如何为所有元素启用MozUserSelect/SelectStart并设置 CSS 属性?

回答by ccpizza

Ran into the same issue on a nasty web app that I have to use daily at work.

在我每天必须在工作中使用的讨厌的网络应用程序上遇到了同样的问题。

Tried writing my own bookmarklet which would overwride onselectstartevent handler, but it did not work, apparently because of the additional CSS rules that had to be modified.

尝试编写我自己的书签来覆盖onselectstart事件处理程序,但它不起作用,显然是因为必须修改额外的 CSS 规则。

Then I have found the Enable All Text Selection bookmarklet by Alan Hoganthat solved the problem for me. The only issue with the bookmarklet is that it does not handle frames/iframes.

然后我找到了Alan HoganEnable All Text Selection 书签,它为我解决了这个问题。bookmarklet 的唯一问题是它不处理框架/iframe。

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:

创建书签(例如,将任何页面的 URL 左侧的图标拖到书签栏),右键单击并选择Edit,重命名为有意义的名称,然后在 URL 字段中插入以下代码:

javascript:(function(){function ats(){var styles='*,p,div{user-select:text !important;-moz-user-select:text !important;-webkit-user-select:text !important;}';jQuery('head').append(jQuery('<style />').html(styles));var allowNormal=function(){return true;};jQuery('*[onselectstart], *[ondragstart], *[oncontextmenu], #songLyricsDiv').unbind('contextmenu').unbind('selectstart').unbind('dragstart').unbind('mousedown').unbind('mouseup').unbind('click').attr('onselectstart',allowNormal).attr('oncontextmenu',allowNormal).attr('ondragstart',allowNormal);}function atswp(){if(window.jQuery){ats();}else{window.setTimeout(atswp,100);}}if(window.jQuery){ats();}else{var s=document.createElement('script');s.setAttribute('src','http://code.jquery.com/jquery-1.9.1.min.js');document.getElementsByTagName('body')[0].appendChild(s);atswp();}})();

It has limitations though in that it is not going to work inside nested frames.

它有局限性,因为它不能在嵌套框架内工作。



..btw, to make the bookmarklet code readable, I have used the Bookmarkelt Builder at http://subsimple.com/bookmarklets/jsbuilder.htm- just paste the minified bookmarklet text and click the Format button; this tool saved me a lot of time.

..btw,为了使书签代码可读,我使用了http://subsimple.com/bookmarklets/jsbuilder.htm 上的 Bookmarkelt Builder - 只需粘贴缩小的书签文本并单击“格式”按钮;这个工具为我节省了很多时间。

回答by Kanhailal Murmu

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.

CSS 无法解决这个问题,因为每当您尝试选择文本时,Javascript 就会发挥作用。

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.htm2) Open the javascript console. I am using chrome (click shift+command+C on Mac, f12 on Ubuntu and Windows)

有两种方法可以解决这个问题 1) 在您的网络浏览器上禁用 Javascript。检查这个以供参考。 http://browsers.about.com/od/googlechrome/ss/disable-javascript-chrome-windows.htm2) 打开 javascript 控制台。我正在使用 chrome(在 Mac 上单击 shift+command+C,在 Ubuntu 和 Windows 上单击 f12)

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

复制此代码document.body.onselectstart = function() {return true;};并将其粘贴到控制台中,然后按 Enter。

回答by Cloe Chen

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:

同意jfriend00。最好避开那些有问题的网站。除非真的有必要在网站上存货……那么你可以考虑试试这个:

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

为问题网站禁用浏览器 Java 脚本也可能有效。 http://browsers.about.com/od/googlechrome/ss/disable-javascript-chrome-windows.htm

回答by jfriend00

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.

放弃担心吧。Firefox 4+ 让您再次控制右键单击,因此应用程序无法将其从您手中夺走。如果您认为某个站点如此令人讨厌,请停止使用它并支持其他一些不那么令人讨厌的站点,或者停止尝试接受他们不希望您接受的内容。除非页面都是由 JS 动态构建的,否则您始终可以从页面源中获取文本。我说你应该停止使用惹恼你的网站。如果每个人都这样做,他们将不得不改变他们的方式。

回答by qix

Seen the same annoying js and more on another site:

在另一个网站上看到了同样烦人的 js 和更多内容:

<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 documentmanually 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};)

请注意,所有事件处理程序都附加到document手动和disableSelection(document.body). 所以@alan-h 引用的允许鼠标事件的脚本也需要更新以在 document 和 document.body 上运行。(重:el.onselectstart = el.ondragstart = el.ondrag = el.oncontextmenu = el.onmousedown = el.onmouseup = function(){return true};