Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript启用阻止的文本选择_Javascript_Css - Fatal编程技术网

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

使用JavaScript启用阻止的文本选择,javascript,css,Javascript,Css,我最近遇到了一个问题,防止任何人轻松复制和粘贴文本。我有一个bookmarklet,它可以禁止使用JavaScript阻止上下文菜单的类似尝试,我想知道是否有可能为文本选择做类似的事情 function disableSelection(target){ if (typeof target.onselectstart!="undefined") //For IE target.onselectstart=function(){return false} else if (typeof t

我最近遇到了一个问题,防止任何人轻松复制和粘贴文本。我有一个bookmarklet,它可以禁止使用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"
}
在其他地方,使用
disableSelection(document.body)
调用该函数

我的关联菜单bookmarklet中的解决方案可能也是必要的:

javascript:void(document.onmousedown=null);
void(document.onclick=null);
void(document.oncontextmenu=null)
最后,我发现CSS也可以使用:

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

有没有一种方法可以一次解决所有这些问题,结束对我的浏览器的暴政?如何为所有元素启用
MozUserSelect
/
SelectStart
,并设置CSS属性?

网站总能找到令人讨厌的方法。他们所要做的就是把透明的图像放在物体上,或者把文本放在图像上,这样你的速度就会慢一点

别再担心了。Firefox4+让你可以控制再次右键点击,这样应用程序就不会从你手中夺走它。如果你认为一个网站是那么令人讨厌,那么停止使用它,支持其他一些不是那么令人讨厌的网站,或者停止尝试拿走他们不想让你拿走的东西。除非页面都是从JS动态构建的,否则您始终可以从页面的源代码获取文本。我说你应该停止使用那些让你烦恼的网站。如果每个人都这样做了,他们就必须改变自己的方式。

请查看。bookmarklet的唯一问题是它不处理frames/iframe(这是一个浏览器安全问题,因此不太可能对此采取措施)

作为额外的好处,它还可以在阻止它的页面上启用鼠标右键单击事件

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

javascript:


要使bookmarklet代码可读,您可以使用bookmarklet生成器-只需粘贴缩小的bookmarklet文本并单击格式按钮。

同意jfriend00。这是很好的,以避免这些问题的网站。除非真的有必要在网站上进货。。。那么你可以考虑尝试一下:

为有问题的网站禁用浏览器Java脚本也可能有效。
一个网站也有同样的问题

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

有两种方法可以解决这个问题 1) 在web浏览器上禁用Javascript。 查看此文件以供参考。 2) 打开javascript控制台。我正在使用chrome(在Mac上单击shift+command+C,在Ubuntu和Windows上单击f12)

复制此代码
document.body.onselectstart=function(){return true;}

并将其粘贴到控制台中,然后按enter键

在另一个网站上看到了同样令人讨厌的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>

函数禁用文本(e){
返回错误
}
函数可重入(){
返回真值
}
//用于浏览器IE4+
document.onselectstart=新函数(“返回false”)
//用于浏览器NS6
if(窗口侧栏){
document.onmousdown=disableText
document.onclick=可重入
}
var msgpopup=“”;
函数pmb(){
如果(alertVis==“1”)警报(消息);
如果(closeWin==“1”)self.close();
返回false;
}
函数IE(){
如果(event.button==“2”| | event.button==“3”){pmb();}
}
函数NS(e){
if(document.layers | |(document.getElementById&&!document.all)){
如果(e.which==“2”| e.which==“3”){pmb();}
}
}
document.onmousedown=IE;document.onmouseup=NS;document.oncontextmenu=新函数(“警报(msgpopup);返回false”)
功能禁用选择(目标){
if(typeof target.onselectstart!=“undefined”)//用于IE
target.onselectstart=function(){return false}
else if(typeof target.style.mozzuserselect!=“undefined”)//用于Firefox
target.style.MozUserSelect=“无”
else//所有其他路线(用于歌剧院)
target.onmousedown=function(){return false}
target.style.cursor=“默认”
}

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

如果你在谷歌浏览器上,你可以使用这个扩展


它启用文本选择,如果被阻止,则右键单击

,因此我尝试了上述所有答案,但没有一个对我有效

后来,我在Microsoft Edge浏览器中打开了相同的链接

这就成功了。当然,您必须在计算机上,并且应该安装Edge。
请先尝试以上答案。

我知道我总能从源代码中提取文本。我希望为自己编写一个bookmarklet来解决出现的这些问题,就像我提到的上下文按钮bookmarklet一样。我告诉你,你可能会发现一种特定的技术让你烦恼,并找到解决方法,但是有太多的方法让人恼火,所以事先尝试主动解决这些问题是不可行的。我真的不想找到你引用的页面的解决方案。这太令人讨厌了。我不会光顾他们的,比如交通运输