在浏览器“开发者工具”控制台中执行如下脚本,可以解除网页禁止复制:
// 获取当前window下所有window对象
function getAllWindows() {
const windows = [];
function collectWindows(win) {
windows.push(win);
// 检查当前窗口对象是否有子iframe
if (win.frames.length > 0) {
for (let i = 0; i < win.frames.length; i++) {
// 递归调用自身,收集所有子窗口
collectWindows(win.frames[i]);
}
}
}
// 从当前窗口开始收集
collectWindows(window);
return windows;
}
// 解除网页禁止复制
function removeClassName(className) {
// 是否存在解除项
let removeCount = 0;
for (let k = 0,ks = allWindows.length;k < ks;k++) {
let currentWindos = allWindows[k];
let eObj = currentWindos.document.getElementsByClassName(className);
for (let i = 0,s = eObj.length;i < s;i++) {
let classList = eObj[i].classList;
if(classList) {
++removeCount;
classList.remove(className);
}
}
}
if (removeCount == 0) {
console.log('%c \n\n\n若仍未解除禁止复制,请选择网页中的相应元素','color: red');
} else{
console.log('%c \n\n\n【禁止复制】已解除','color: green');
}
}
// 使用函数获取所有window对象
const allWindows = getAllWindows();
// 解除禁止复制
removeClassName('unselect')