现在有很多的网站关闭了拷贝功能,下面介绍一种方法,将拷贝功能打开。
禁止拷贝的原来实际上是将是给文本元素添加了disabled属性,将该属性祛除即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| javascript: (function () { function allowTextSelection() { window.console && console.log("allowTextSelection"); var style = document.createElement("style"); style.type = "text/css"; style.innerHTML = "*,p,div{user-select:text !important;-moz-user-select:text !important;-webkit-user-select:text !important;}"; document.head.appendChild(style); var elArray = document.body.getElementsByTagName("*"); for (var i = 0; i < elArray.length; i++) { var el = elArray[i]; el.onselectstart = el.ondragstart = el.ondrag = el.oncontextmenu = el.onmousedown = el.onmouseup = function () { return true; }; if ( el instanceof HTMLInputElement && ["text", "password", "email", "number", "tel", "url"].indexOf( el.type.toLowerCase() ) > -1 ) { el.removeAttribute("disabled"); el.onkeydown = el.onkeyup = function () { return true; }; } } } allowTextSelection(); })();
|
上面的代码将其压缩成一行代码如下,只要打开console(Ctrl+Shift+I), 将下面的代码执行一下就ok了。
1 2 3
| javascript:(function(){function allowTextSelection(){window.console&&console.log('allowTextSelection');var style=document.createElement('style');style.type='text/css';style.innerHTML='*,p,div{user-select:text !important;-moz-user-select:text !important;-webkit-user-select:text !important;}';document.head.appendChild(style);var elArray=document.body.getElementsByTagName('*');for(var i=0;i<elArray.length;i++){var el=elArray[i];el.onselectstart=el.ondragstart=el.ondrag=el.oncontextmenu=el.onmousedown=el.onmouseup=function(){return true};if(el instanceof HTMLInputElement&&['text','password','email','number','tel','url'].indexOf(el.type.toLowerCase())>-1){el.removeAttribute('disabled');el.onkeydown=el.onkeyup=function(){return true};}}}allowTextSelection();})();
|
参考文档
Enabling blocked text selection using JavaScript