2011-08-18 54 views
12

我最近遇到了website that disabled text selection,阻止任何人轻松地复制和粘贴文本。我有一个小书签,禁止使用JavaScript阻止上下文菜单的类似尝试,我想知道是否可以为文本选择做类似的事情。使用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)

从我的上下文菜单中选择书签的解决方案也可能是必要的:

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

最后,我看到了elsewhere on StackOverflow是CSS也可用于:

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

有没有打所有的方法这些立即结束这个暴虐通过我的浏览器?我如何为所有元素启用MozUserSelect/SelectStart并设置CSS属性?

+0

这是一个非常快速downvote ...任何解释? – Patrick

回答

15

在我每天必须在工作中使用的恶劣Web应用程序中遇到同样的问题。

尝试写我自己的小书签,它会overwride onselectstart事件处理程序,但它没有工作,显然是因为额外的CSS规则,必须修改。

然后,我发现Enable All Text Selection bookmarklet by Alan Hogan,这解决了我的问题。小书签唯一的问题是它不处理帧/ iframe。

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

创建一个书签(例如,通过将图标拖到URL左侧的任何页面到您的书签栏),右键单击并选择编辑,重命名为有意义的内容,然后在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();}})(); 

它有一些限制,尽管它不会在嵌套帧内工作。


..btw,使书签代码可读性,我已经使用了Bookmarkelt生成器在http://subsimple.com/bookmarklets/jsbuilder.htm - 只是贴上精缩书签文本,然后单击格式按钮;这个工具为我节省了很多时间。

+0

很高兴它有帮助 - 并感谢您分享bookmarklet生成器链接! –

+0

if add also document.oncontextmenu = null;将完成这一点。 – albanx

+0

更新:此书签源代码在GitHub上为**。**已经过改进和更新。拉请求是值得欢迎的! https://github.com/alanhogan/bookmarklets –

1

网站总能找到令人厌恶的方式。他们所要做的就是在图片上放置透明的图片,或者在图片中放置文字,这样会稍微放慢速度。

放弃担心它。 Firefox 4 +可让您再次右键单击,以便应用程序无法将其从您身上夺走。如果你认为一个网站是令人讨厌的,那就停止使用它,并支持一些不太讨厌的站点,或者停止尝试不想让你采取的事情。除非页面全部由JS动态构建,否则您始终可以从页面源获取文本。我说你应该停止使用惹恼你的网站。如果每个人都这样做,他们就不得不改变他们的方式。

+0

我知道我总是可以从源文件中提取文本。我希望为自己编写一个小书签,以便在他们出现时解决这些问题,就像我提到的上下文按钮书签一样。 – Patrick

+0

我告诉你,你大概可以找出一种特定的技术来惹恼你,并找到解决办法,但有很多方法可以惹恼,试图主动解决所有问题是不可行的提前。我真的没有兴趣追踪你引用页面的解决方案。这太令人讨厌了。我不会光顾他们。 – jfriend00

+0

像透明图像的东西,很容易通过禁用页面CSS解决;我制作了一个书签,通过设置标准颜色和文本大小来“人化”页面,并禁用大部分CSS。例如,它将允许在flickr上将图像保存在透明gif后面:https://gist.github.com/1485798 – ccpizza

2

与网站有同样的问题。

当Javascript尝试使用 来选择文本时,CSS无法解决此问题。

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

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

+0

我接受的答案并不适用于Chrome。但document.body.onselectstart做了诡计。谢谢! – dtroy

0

看在其他网站相同的恼人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> 

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