Js文章内容监听复制代码
若别人在你的网站复制内容将会提示,请把监听提示添加到文章内容div或者直接将代码复制粘贴到所需要的文件中!

<script> //监听ctrl+v 复制 document.addEventListener("copy", function(e) { alert('复制成功,请遵循本文的转载规则'); }); //把监听提示添加到文章内容中 //文章内容div的ID $('#Inside_content').on('copy', function(e) { // IE8 or earlier browser is 'undefined' if (typeof window.getSelection === 'undefined') return; var selection = window.getSelection(); // if the selection is short let's not annoy our users. if (('' + selection).length < Number.parseInt('120')) { return; } //在内容末尾追加提示内容 var bodyElement = document.getElementsByTagName('body')[0]; var newdiv = document.createElement('div'); newdiv.style.position = 'absolute'; newdiv.style.left = '-99999px'; bodyElement.appendChild(newdiv); newdiv.appendChild(selection.getRangeAt(0).cloneContents()); //格式化 if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE') { newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>"; } var url = document.location.href; //追加的内容 newdiv.innerHTML += '<br />' + '来源: 神经娃博客<br />' + '文章作者: 神经娃博客<br />' + '文章链接: <a href="' + url + '">' + url + '</a><br />' + '本文章著作权归作者所有,任何形式的转载都请注明出处。'; selection.selectAllChildren(newdiv); window.setTimeout(function() { bodyElement.removeChild(newdiv); }, 200); }); </script>
作者:Alone°李道长ご
联系:WeChat(QQ):1969170027
-------------------------------------------------
如果觉得这篇文章对你有小小的帮助的话,点点左下角“👍”博主在此感谢!

浙公网安备 33010602011771号