拷贝网页内容时自动添加版权链接的操作方法
注意:如果你的网站经常需要访客拷贝代码,推荐无需开启本功能,否则在拷贝代码时也会自动添加版权链接造成不便,暂无解决方案!
function addCopyright() {
var Original = "本文来源:" + location.href; //修改你的网站名称
if ("function" == typeof window.getSelection) {
var c = window.getSelection();
if ("Microsoft Internet Explorer" == navigator.appName && navigator.appVersion.match(/MSIE ([\d.]+)/)[1] >= 10 || "Opera" == navigator.appName) {
var g = c.getRangeAt(0),
h = document.createElement("span");
h.appendChild(g.cloneContents()),
g.insertNode(h);
var i = h.innerHTML.replace(/(?:\n|\r\n|\r)/gi, "").replace(/<\s*script[^>]*>[\s\S]*?<\/script>/gim, "").replace(/<\s*style[^>]*>[\s\S]*?<\/style>/gim, "").replace(/<!--.*?-->/gim, "").replace(/<!DOCTYPE.*?>/gi, "");
try {
document.getElementsByTagName("body")[0].removeChild(h)
} catch(f) {
h.style.display = "none",
h.innerHTML = ""
}
} else var d = "" + c;
var e = document.getElementsByTagName("body")[0],
f = document.createElement("div");
f.style.position = "absolute",
f.style.left = "-99999px",
e.appendChild(f),
f.innerHTML = d.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, "$1<br />$2") + "<br />" + Original,
c.selectAllChildren(f),
setTimeout(function() {
e.removeChild(f)
},
0)
} else if ("object" == typeof document.selection.createRange) {
event.returnValue = !1;
var c = document.selection.createRange().text;
window.clipboardData.setData("Text", c + "\n" + Original)
}
};
document.body.oncopy = addCopyright;
发表评论