// ==UserScript== // @name 网页复制解锁 // @namespace https://cxbank.icu/copyfree // @version 1.1.0 // @description 解除网页的复制、右键、选中限制;新增图片原图下载(可打包 ZIP)。一键开关,不影响页面其他功能。 // @author ZZ // @match *://*/* // @grant none // @run-at document-start // @noframes false // ==/UserScript== (function () { 'use strict'; var KEY = 'copyfree_on'; var S = { on: true, removedAttrs: 0, removedClasses: 0, }; try { S.on = localStorage.getItem(KEY) !== '0'; } catch (e) {} /* 需要"放行"的事件类型 */ var BLOCKED = { copy: 1, cut: 1, contextmenu: 1, selectstart: 1, dragstart: 1 }; /* ---------- 1) 拦截 preventDefault ---------- */ var origPD = Event.prototype.preventDefault; Event.prototype.preventDefault = function () { try { if (S.on && this && BLOCKED[this.type]) { /* 记录一下,面板上能看到"本站拦了几个" */ S.blockedCalls = (S.blockedCalls || 0) + 1; return; } } catch (e) {} return origPD.apply(this, arguments); }; /* ---------- 2) 清掉内联事件 ---------- */ function stripInline(root) { try { var attrs = ['oncopy', 'oncut', 'oncontextmenu', 'onselectstart', 'ondragstart', 'onmousedown', 'onmouseup']; var all = (root || document).querySelectorAll('*'); for (var i = 0; i < all.length; i++) { var el = all[i]; for (var k = 0; k < attrs.length; k++) { if (el.getAttribute && el.getAttribute(attrs[k])) { el.removeAttribute(attrs[k]); S.removedAttrs++; } } } /* 文档级 */ ['oncopy', 'oncut', 'oncontextmenu', 'onselectstart', 'ondragstart'].forEach(function (a) { try { document[a] = null; } catch (e) {} try { document.body && (document.body[a] = null); } catch (e) {} }); } catch (e) {} } /* ---------- 3) 去掉 user-select: none ---------- */ var style = null; function applyStyle() { if (!style) { style = document.createElement('style'); style.id = 'copyfree-style'; style.textContent = 'html,body,*{-webkit-user-select:text !important;user-select:text !important;' + '-webkit-touch-callout:default !important}'; (document.head || document.documentElement).appendChild(style); } style.disabled = !S.on; } /* ---------- 4) 清理常见"禁止选中"的 class ---------- */ var BAD_CLASS = /(noselect|no-select|noselection|unselectable|disable-select|user-select-none)/i; function stripClasses(root) { try { var all = (root || document).querySelectorAll('[class]'); for (var i = 0; i < all.length; i++) { var el = all[i]; var c = String(el.className || ''); if (!BAD_CLASS.test(c)) continue; el.className = c.split(/\s+/).filter(function (x) { return !BAD_CLASS.test(x); }).join(' '); S.removedClasses++; } } catch (e) {} } /* ---------- 5) 兜底:Alt+C 强制复制选中内容 ---------- */ function forceCopy() { try { var sel = String(window.getSelection ? window.getSelection() : ''); if (!sel) return false; if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(sel); return true; } var ta = document.createElement('textarea'); ta.value = sel; ta.style.cssText = 'position:fixed;left:-9999px;top:0'; document.body.appendChild(ta); ta.select(); document.execCommand('copy'); ta.remove(); return true; } catch (e) { return false; } } document.addEventListener('keydown', function (e) { if (e.altKey && (e.key === 'c' || e.key === 'C')) { if (forceCopy()) { e.preventDefault(); toast('已复制选中内容'); } } }, true); /* ---------- 6) 图片原图下载 ---------- */ /* 常见 CDN 缩略图规则 → 还原成原图 */ function origUrl(u) { if (!u) return ''; var s = String(u).trim(); if (!s || /^data:/i.test(s)) return ''; /* 微信公众平台:末尾 /640 或 /300 之类 */ s = s.replace(/\/(?:640|300|200|132|0|w)\/?$/i, '/0'); s = s.replace(/([?&])(?:wxfrom|wx_lazy|wx_co|tp|wx_fmt)=[^&]*/gi, '$1').replace(/[?&]$/, ''); /* 通用 ?x-oss-process / ?imageView2 / ?imageMogr2 / resize 参数 */ s = s.replace(/[?&](?:x-oss-process|imageView2|imageMogr2|resize|thumbnail|thumb|w|h|quality|q|width|height)=[^&]*/gi, ''); s = s.replace(/[?&]$/, ''); /* 知乎 _b / _hd → 原图 */ s = s.replace(/_(?:b|hd|720w|540w)\.(jpg|jpeg|png|webp|gif)/i, '.$1'); /* 后缀式缩略:@320w_200h、@!q70、!w400 */ s = s.replace(/@[^/?#]*$/, ''); s = s.replace(/![a-z]\d+[^/?#]*$/i, ''); /* 淘宝/1688:_sum.jpg / _400x400.jpg / _q75.jpg */ s = s.replace(/_(?:sum|q\d{2}|\d+x\d+)(\.(?:jpg|jpeg|png|webp|gif))/i, '$1'); /* WordPress:-300x200.jpg */ s = s.replace(/-\d{2,4}x\d{2,4}(\.(?:jpg|jpeg|png|webp|gif))/i, '$1'); /* 简书/CSDN 等:/thumb/、/small/、_small */ s = s.replace(/\/(?:thumb|thumbs|small|medium|preview)\//i, '/'); s = s.replace(/_(?:small|medium|thumb)(\.(?:jpg|jpeg|png|webp|gif))/i, '$1'); /* bilibili:@320w_200h.webp */ s = s.replace(/@\d+w_\d+h\.(?:webp|jpg|png)/i, ''); return s; } function absUrl(u) { try { return new URL(u, location.href).href; } catch (e) { return ''; } } function collectImages() { var out = [], seen = {}; function push(u, w, h, note) { var a = absUrl(u); if (!a || /^data:/i.test(a)) return; if (seen[a]) return; seen[a] = 1; out.push({ url: a, orig: origUrl(a), w: w || 0, h: h || 0, note: note || '' }); } /* :原图优先取 data-original / data-src / data-actualsrc */ var imgs = document.querySelectorAll('img'); for (var i = 0; i < imgs.length && out.length < 200; i++) { var im = imgs[i]; var real = im.getAttribute('data-original') || im.getAttribute('data-src') || im.getAttribute('data-actualsrc') || im.getAttribute('data-lazy-src') || ''; var cand = real || im.currentSrc || im.src || ''; if (!cand) continue; if (im.naturalWidth && im.naturalWidth < 60 && im.naturalHeight < 60) continue; /* 跳过图标 */ push(cand, im.naturalWidth || im.width, im.naturalHeight || im.height, real ? 'lazy' : ''); /* srcset 里挑最大的 */ var ss = im.getAttribute('srcset'); if (ss) { var best = '', bw = 0; ss.split(',').forEach(function (part) { var p = part.trim().split(/\s+/); var w = parseInt(p[1], 10) || 0; if (w > bw) { bw = w; best = p[0]; } }); if (best) push(best, bw, 0, 'srcset'); } } /* */ document.querySelectorAll('picture source[srcset]').forEach(function (sc) { var first = (sc.getAttribute('srcset') || '').split(',')[0]; if (first) push(first.trim().split(/\s+/)[0], 0, 0, 'picture'); }); /* CSS 背景图(限前 120 个元素,避免卡) */ var all = document.querySelectorAll('*'); for (var k = 0; k < all.length && k < 1200 && out.length < 200; k++) { var bg = ''; try { bg = getComputedStyle(all[k]).backgroundImage || ''; } catch (e) { bg = ''; } var m = /url\(["']?([^"')]+)["']?\)/.exec(bg); if (m && m[1]) push(m[1], 0, 0, 'bg'); } return out; } /* --- 极简 ZIP(仅存储不压缩)--- */ var CRC_TABLE = (function () { var t = new Uint32Array(256); for (var n = 0; n < 256; n++) { var c = n; for (var k = 0; k < 8; k++) c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1); t[n] = c >>> 0; } return t; })(); function crc32(buf) { var c = 0xFFFFFFFF; for (var i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xFF] ^ (c >>> 8); return (c ^ 0xFFFFFFFF) >>> 0; } function makeZip(files) { var enc = new TextEncoder(); var parts = [], central = [], offset = 0; files.forEach(function (f) { var name = enc.encode(f.name); var data = f.data; var crc = crc32(data); var local = new Uint8Array(30 + name.length); var dv = new DataView(local.buffer); dv.setUint32(0, 0x04034b50, true); dv.setUint16(4, 20, true); dv.setUint16(6, 0x0800, true); dv.setUint16(8, 0, true); dv.setUint16(10, 0, true); dv.setUint16(12, 0, true); dv.setUint32(14, crc, true); dv.setUint32(18, data.length, true); dv.setUint32(22, data.length, true); dv.setUint16(26, name.length, true); dv.setUint16(28, 0, true); local.set(name, 30); parts.push(local, data); var cd = new Uint8Array(46 + name.length); var cv = new DataView(cd.buffer); cv.setUint32(0, 0x02014b50, true); cv.setUint16(4, 20, true); cv.setUint16(6, 20, true); cv.setUint16(8, 0x0800, true); cv.setUint16(10, 0, true); cv.setUint16(12, 0, true); cv.setUint16(14, 0, true); cv.setUint32(16, crc, true); cv.setUint32(20, data.length, true); cv.setUint32(24, data.length, true); cv.setUint16(28, name.length, true); cv.setUint16(30, 0, true); cv.setUint16(32, 0, true); cv.setUint16(34, 0, true); cv.setUint16(36, 0, true); cv.setUint32(38, 0, true); cv.setUint32(42, offset, true); cd.set(name, 46); central.push(cd); offset += local.length + data.length; }); var cdSize = central.reduce(function (a, b) { return a + b.length; }, 0); var end = new Uint8Array(22); var ev = new DataView(end.buffer); ev.setUint32(0, 0x06054b50, true); ev.setUint16(8, files.length, true); ev.setUint16(10, files.length, true); ev.setUint32(12, cdSize, true); ev.setUint32(16, offset, true); return new Blob(parts.concat(central, [end]), { type: 'application/zip' }); } function saveBlob(blob, name) { var a = document.createElement('a'); var u = URL.createObjectURL(blob); a.href = u; a.download = name; document.body.appendChild(a); a.click(); setTimeout(function () { URL.revokeObjectURL(u); a.remove(); }, 4000); } function fileName(u, i) { var n = 'image_' + (i + 1); try { var p = new URL(u).pathname.split('/').pop(); if (p && p.length < 80) n = decodeURIComponent(p).replace(/[\\/:*?"<>|]/g, '_'); } catch (e) {} if (!/\.(jpg|jpeg|png|webp|gif|bmp|avif)$/i.test(n)) n += '.jpg'; return n; } /* 逐个抓取;跨域失败就退回新窗口打开 */ async function grab(url) { try { var r = await fetch(url, { credentials: 'omit', mode: 'cors' }); if (!r.ok) throw new Error('HTTP ' + r.status); return new Uint8Array(await r.arrayBuffer()); } catch (e) { return null; } } var IMG = { list: [], sel: {}, panel: null, busy: false }; function imgPanel() { if (IMG.panel) { IMG.panel.remove(); IMG.panel = null; return; } IMG.list = collectImages(); IMG.sel = {}; if (!IMG.list.length) { toast('本页没找到图片'); return; } var box = document.createElement('div'); box.style.cssText = 'position:fixed;inset:4% 6%;z-index:2147483003;background:#fff;border-radius:16px;' + 'box-shadow:0 20px 60px rgba(0,0,0,.35);display:flex;flex-direction:column;overflow:hidden;' + 'font:13px/1.6 -apple-system,"Segoe UI",sans-serif;color:#1c1c1e'; box.innerHTML = '
' + '图片原图下载' + '' + '' + '' + '' + '' + '' + '
'; var list = box.querySelector('#cf-list'); IMG.list.forEach(function (it, i) { var card = document.createElement('div'); card.style.cssText = 'border:2px solid ' + (it.orig !== it.url ? '#34c759' : '#e5e5ea') + ';border-radius:10px;overflow:hidden;cursor:pointer;background:#fafafa'; var thumb = document.createElement('img'); thumb.src = it.url; thumb.style.cssText = 'width:100%;height:96px;object-fit:cover;display:block;background:#eee'; var meta = document.createElement('div'); meta.style.cssText = 'padding:6px 8px;font-size:11px;color:#666;white-space:nowrap;overflow:hidden;text-overflow:ellipsis'; meta.textContent = (it.w ? it.w + '×' + it.h : '尺寸未知') + (it.orig !== it.url ? ' · 可还原原图' : '') + (it.note ? ' · ' + it.note : ''); card.appendChild(thumb); card.appendChild(meta); card.onclick = function () { if (IMG.sel[i]) { delete IMG.sel[i]; card.style.outline = ''; } else { IMG.sel[i] = 1; card.style.outline = '3px solid #0a84ff'; } updateCnt(); }; list.appendChild(card); }); function updateCnt() { var n = Object.keys(IMG.sel).length; box.querySelector('#cf-cnt').textContent = '共 ' + IMG.list.length + ' 张 · 已选 ' + n; } updateCnt(); box.querySelector('#cf-x').onclick = function () { box.remove(); IMG.panel = null; }; box.querySelector('#cf-all').onclick = function () { var all = Object.keys(IMG.sel).length !== IMG.list.length; IMG.sel = {}; if (all) IMG.list.forEach(function (_, i) { IMG.sel[i] = 1; }); Array.prototype.forEach.call(list.children, function (c, i) { c.style.outline = IMG.sel[i] ? '3px solid #0a84ff' : ''; }); updateCnt(); }; box.querySelector('#cf-copy').onclick = function () { var urls = Object.keys(IMG.sel).map(function (i) { return IMG.list[i].orig; }); if (!urls.length) return toast('先选几张'); var txt = urls.join('\n'); if (navigator.clipboard) navigator.clipboard.writeText(txt).then(function () { toast('已复制 ' + urls.length + ' 条'); }); else toast('复制失败'); }; box.querySelector('#cf-zip').onclick = async function () { var idx = Object.keys(IMG.sel).map(Number); if (!idx.length) return toast('先选择图片'); if (IMG.busy) return; IMG.busy = true; toast('正在抓取 0/' + idx.length + '…'); var files = [], fail = 0; for (var n = 0; n < idx.length; n++) { var it = IMG.list[idx[n]]; var data = await grab(it.orig); if (!data) { fail++; continue; } files.push({ name: fileName(it.orig, n), data: data }); toast('正在抓取 ' + (n + 1) + '/' + idx.length + '…'); } IMG.busy = false; if (!files.length) return toast('全部抓取失败(跨域限制),可改用「复制链接」'); saveBlob(makeZip(files), 'images_' + Date.now() + '.zip'); toast('已打包 ' + files.length + ' 张' + (fail ? '(' + fail + ' 张跨域失败)' : '')); }; document.body.appendChild(box); IMG.panel = box; } /* ---------- 面板 ---------- */ var host = null, root = null, dot = null, tip = null; var CSS = [ '.dot{position:fixed;right:14px;bottom:56px;width:38px;height:38px;border-radius:50%;', 'background:linear-gradient(145deg,#34c759,#28a745);color:#fff;border:none;cursor:pointer;', 'font:600 13px/1 -apple-system,"Segoe UI",sans-serif;z-index:2147483000;', 'box-shadow:0 8px 20px rgba(52,199,89,.4);display:flex;align-items:center;justify-content:center}', '.dot.off{background:linear-gradient(145deg,#8e8e93,#6c6c70);box-shadow:0 8px 20px rgba(0,0,0,.2)}', '.card{position:fixed;right:14px;bottom:102px;width:236px;z-index:2147483001;', 'background:rgba(255,255,255,.97);border:.5px solid rgba(0,0,0,.1);border-radius:14px;', 'padding:12px 14px;font:13px/1.7 -apple-system,"Segoe UI",sans-serif;color:#1c1c1e;', 'box-shadow:0 12px 34px rgba(0,0,0,.22);display:none}', '.card h4{margin:0 0 8px;font-size:13px}', '.row{display:flex;justify-content:space-between;color:#8e8e93;font-size:11.5px;margin-bottom:6px}', '.row b{color:#1c1c1e}', '.sw{display:flex;align-items:center;justify-content:space-between;margin:10px 0 4px;font-size:12.5px}', '.tg{width:44px;height:24px;border-radius:99px;background:#e5e5ea;position:relative;cursor:pointer;transition:.2s}', '.tg.on{background:#34c759}', '.tg i{position:absolute;left:2px;top:2px;width:20px;height:20px;border-radius:50%;background:#fff;transition:.2s}', '.tg.on i{left:22px}', '.hint{color:#8e8e93;font-size:11px;margin-top:8px;line-height:1.6}', ].join(''); function toast(t) { if (tip) { tip.textContent = t; tip.style.opacity = '1'; return; } tip = document.createElement('div'); tip.textContent = t; tip.style.cssText = 'position:fixed;left:50%;top:18px;transform:translateX(-50%);z-index:2147483002;' + 'background:rgba(28,28,30,.92);color:#fff;padding:8px 16px;border-radius:99px;' + 'font:600 12.5px/1.4 -apple-system,sans-serif;transition:opacity .3s'; document.body.appendChild(tip); setTimeout(function () { if (tip) { tip.style.opacity = '0'; setTimeout(function () { if (tip) { tip.remove(); tip = null; } }, 400); } }, 1400); } function render() { if (!dot) return; dot.classList.toggle('off', !S.on); dot.title = S.on ? '复制解锁:已开启(点击设置)' : '复制解锁:已关闭'; try { if (root) { root.getElementById('st').textContent = S.on ? '已开启' : '已关闭'; var sw = root.getElementById('sw'); if (sw) sw.classList.toggle('on', S.on); root.getElementById('a1').textContent = S.removedAttrs; root.getElementById('a2').textContent = S.removedClasses; root.getElementById('a3').textContent = S.blockedCalls || 0; } } catch (e) {} } function build() { if (!document.body || document.getElementById('copyfree-host')) return; host = document.createElement('div'); host.id = 'copyfree-host'; root = host.attachShadow({ mode: 'open' }); var st = document.createElement('style'); st.textContent = CSS; root.appendChild(st); dot = document.createElement('button'); dot.className = 'dot'; dot.textContent = '解开'; root.appendChild(dot); var card = document.createElement('div'); card.className = 'card'; card.innerHTML = '

网页复制解锁

' + '
状态已开启
' + '
解除限制
' + '
清理内联事件0
' + '
清理禁选样式0
' + '
拦截 preventDefault0
' + '
图片原图下载' + '
' + '
Alt + C 可强制复制选中文字
Shift + Alt + I 打开图片面板
'; root.appendChild(card); document.body.appendChild(host); dot.onclick = function () { card.style.display = card.style.display === 'block' ? 'none' : 'block'; }; var imgBtn = root.getElementById('imgbtn'); if (imgBtn) imgBtn.onclick = function () { imgPanel(); }; var sw = root.getElementById('sw'); sw.onclick = function () { S.on = !S.on; try { localStorage.setItem(KEY, S.on ? '1' : '0'); } catch (e) {} applyStyle(); render(); }; render(); } function boot() { applyStyle(); stripInline(document); stripClasses(document); build(); /* 动态内容:每 2 秒补扫一次(低成本) */ setInterval(function () { if (!S.on) return; stripClasses(document); }, 2000); window.addEventListener('load', function () { stripInline(document); stripClasses(document); }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', boot); } else { boot(); } document.addEventListener('keydown', function (e) { if (e.altKey && e.shiftKey && (e.key === 'I' || e.key === 'i' || e.code === 'KeyI')) { e.preventDefault(); try { imgPanel(); } catch (err) {} } }, true); window.__copyfree = { state: S, forceCopy: forceCopy, imgPanel: imgPanel, collectImages: collectImages, origUrl: origUrl }; })();