// ==UserScript== // @name 我要复制!!! // @namespace http://tampermonkey.net/ // @version 1.1.2 // @description 解除网页复制限制,支持SPA页面切换。支持mooc的页面复制 // @author You // @match http://130.20.186.173/weboffice/office/* // @match https://www.icourse163.org/* // @grant none // @run-at document-start // ==/UserScript== (function() { 'use strict'; const w = window; // 核心解锁函数 function unlockCopy() { try { if (w.APP) { if (w.APP.api && w.APP.api._permissions) { w.APP.api._permissions.copy = true; } if (w.APP.pdfEditPermissions) { w.APP.pdfEditPermissions.copy = true; } if (w.APP.session && w.APP.session._permissions) { w.APP.session._permissions.copy = true; } } if (w.fileInfo && w.fileInfo.user_acl) { w.fileInfo.user_acl.copy = true; } } catch (e) { // 静默处理错误 } } // 持续执行解锁 function startUnlockLoop() { // 立即执行一次 unlockCopy(); // 每秒执行一次 setInterval(unlockCopy, 1000); } // 监听 URL 变化(处理 SPA 页面切换) function observeUrlChanges() { let lastUrl = location.href; // 使用 MutationObserver 监听 URL 变化 const observer = new MutationObserver(() => { const currentUrl = location.href; if (currentUrl !== lastUrl) { lastUrl = currentUrl; // URL 变化后,延迟执行解锁(等待新页面加载完成) setTimeout(unlockCopy, 500); setTimeout(unlockCopy, 1500); } }); observer.observe(document, { subtree: true, childList: true }); // 同时监听 popstate 和 hashchange 事件 w.addEventListener('popstate', () => { setTimeout(unlockCopy, 500); }); w.addEventListener('hashchange', () => { setTimeout(unlockCopy, 500); }); } // 监听 DOM 变化(当目标元素被重新渲染时) function observeDomChanges() { const observer = new MutationObserver((mutations) => { // 当 DOM 发生变化时尝试解锁 unlockCopy(); }); // 等到 document.body 存在后再开始监听 const startObserving = () => { if (document.body) { observer.observe(document.body, { childList: true, subtree: true }); } else { setTimeout(startObserving, 100); } }; startObserving(); } // 拦截并修改可能阻止复制的事件 function interceptCopyEvents() { // 拦截 copy 事件 document.addEventListener('copy', (e) => { e.stopPropagation(); }, true); // 拦截 beforecopy 事件 document.addEventListener('beforecopy', (e) => { e.stopPropagation(); }, true); // 拦截 keydown 事件(Ctrl+C / Cmd+C) document.addEventListener('keydown', (e) => { if ((e.ctrlKey || e.metaKey) && (e.key === 'c' || e.key === 'C')) { e.stopImmediatePropagation(); } }, true); // 拦截 contextmenu 事件(右键菜单) document.addEventListener('contextmenu', (e) => { e.stopImmediatePropagation(); }, true); // 拦截 selectstart 事件(文本选择) document.addEventListener('selectstart', (e) => { e.stopImmediatePropagation(); }, true); } // 初始化 function init() { startUnlockLoop(); observeUrlChanges(); observeDomChanges(); interceptCopyEvents(); } // 页面加载完成后初始化 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();