
























在浏览页面的时候,页面上使用了禁用选择文字,禁用了右键等功能,找豆包给了一个解决方案,如下:
// jQuery 解绑document所有selectstart绑定
$(document).unbind('selectstart');
// 顺带把右键、拖拽一并清掉
$(document).unbind('contextmenu dragstart mousedown');
// 再强制修复CSS不能选中问题
$('body, *').css({
'user-select': 'auto',
'-webkit-user-select': 'auto'
});
如果页面其他代码也绑定了 selectstart,不想全部清空,只能保存原函数引用再解绑,但匿名函数无法直接获取,只能用 jQuery 内部清理:
// 清空document上jQuery托管的所有事件(慎用,会清掉所有jQuery绑定事件)
$(document).unbind();
不管它绑了多少层 jQuery 事件、原生 addEventListener,直接覆盖:
// 原生事件置空
document.onselectstart = null;
document.oncontextmenu = null;
// 阻止jQuery再次绑定生效
Object.defineProperty(document, 'onselectstart', {
set: function() {},
get: function() { return null; }
});
// 全局CSS放开选中
document.querySelectorAll('*').forEach(el => {
el.style.userSelect = 'auto';
el.style.webkitUserSelect = 'auto';
});
.bind() 对应解绑 .unbind();.on() 对应解绑 .off();addEventListener 只能通过相同函数引用 removeEventListener,匿名函数无法单个移除;user-select: none 才是大头,只清事件不改 CSS 依然选不动。======================================================================================
主要是移除页面上的jQuery的绑定的事件

// ==UserScript== // @name moveSelect // @namespace http://tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author You // @match http*://shyahs.chneic.sh.cn/* // @icon https://www.google.com/s2/favicons?sz=64&domain=doubao.com // @grant none // ==/UserScript== (function (){ 'use strict'; // Your code here... $(document).ready(function(){ // 在此处执行希望在页面加载完成后执行的操作 console.log("moveSelect==========页面加载完成") //setTimeout(unbindSelect,1000); unbindSelect(); }); })(); function unbindSelect() { // 主要解决延安中学网站无法选择的问题,jQuery 解绑document所有selectstart绑定, 顺带把右键、拖拽一并清掉 $(document).unbind('selectstart contextmenu dragstart mousedown'); // 再强制修复CSS不能选中问题 $('body, *').css({ 'user-select': 'auto', '-webkit-user-select': 'auto' }); console.log("moveSelect==========解绑document所有selectstart绑定") // GM_info 内置对象,不需要额外权限 console.log('当前脚本名称:', GM_info.script.name); console.log('脚本信息:', GM_info.script); console.log('脚本完整信息:', GM_info); }
View Code
增加统一日志打印的功能

// ==UserScript== // @name moveSelect // @namespace http://tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author You // @match http*://* // @match http*://shyahs.chneic.sh.cn/* // @icon https://www.google.com/s2/favicons?sz=64&domain=baidu.com // @grant none // ==/UserScript== (function (){ 'use strict'; // Your code here... $(document).ready(function(){ // 在此处执行希望在页面加载完成后执行的操作 log("==========页面加载完成事件") //setTimeout(unbindSelect,1000); unbindSelect(); }); })(); //匿名函数调用日志 (()=>{ log('匿名函数内容'); })(); //* 统一日志打印 //* @param {...any} args 要打印的内容 function log(...args) { const fnName = new Error().stack.split('\n')[2].match(/at (\w+)/)[1]; console.log(`[${GM_info.script.name}]-[${fnName}]`, ...args); } function unbindSelect() { // 主要解决延安中学网站无法选择的问题,jQuery 解绑document所有selectstart绑定, 顺带把右键、拖拽一并清掉 $(document).unbind('selectstart contextmenu dragstart mousedown'); // 再强制修复CSS不能选中问题 $('body, *').css({ 'user-select': 'auto', '-webkit-user-select': 'auto' }); log("==========解绑document所有selectstart绑定") }
View Code
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。