使用油候脚本,移除页面无法选择文字

在浏览页面的时候,页面上使用了禁用选择文字,禁用了右键等功能,找豆包给了一个解决方案,如下:

 

方案 1:解绑 document 上所有 selectstart 事件(最简单)

控制台直接运行:
javascript
// jQuery 解绑document所有selectstart绑定
$(document).unbind('selectstart');

// 顺带把右键、拖拽一并清掉
$(document).unbind('contextmenu dragstart mousedown');

// 再强制修复CSS不能选中问题
$('body, *').css({
    'user-select': 'auto',
    '-webkit-user-select': 'auto'
});
 

方案 2:精准只解绑那一个匿名函数(严谨版)

如果页面其他代码也绑定了 selectstart,不想全部清空,只能保存原函数引用再解绑,但匿名函数无法直接获取,只能用 jQuery 内部清理:
javascript
// 清空document上jQuery托管的所有事件(慎用,会清掉所有jQuery绑定事件)
$(document).unbind();
 

方案 3:不用管解绑,直接强行重写事件拦截(终极覆盖)

不管它绑了多少层 jQuery 事件、原生 addEventListener,直接覆盖:
javascript
// 原生事件置空
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';
});
 

方案 4:彻底根治:禁用 JavaScript(推荐一劳永逸)

  1. F12 打开开发者工具 → 右上角设置图标
  2. Settings → Debugger → 取消 Enable JavaScript
  3. 刷新页面,所有 jQuery 绑定、原生事件全部失效,CSS 限制也可手动修改,百分百可以选中文字。

补充小知识

  1. .bind() 对应解绑 .unbind()
  2. 新版 jQuery .on() 对应解绑 .off()
  3. 原生 addEventListener 只能通过相同函数引用 removeEventListener,匿名函数无法单个移除;
  4. 绝大多数不能选文字,CSS user-select: none 才是大头,只清事件不改 CSS 依然选不动。

 

======================================================================================

个人使用

版本1

主要是移除页面上的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

 

版本2

增加统一日志打印的功能

// ==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

 

posted on 2026-07-25 10:01  jack_Meng  阅读(2)  评论(0)    收藏  举报

导航