油猴:有道云笔记网页复制功能

此文章发布于 6 个月前,部分信息可能已经过时,请自行斟酌确认。

某云笔记分享页禁用了复制功能,拖动内容可以看到选中状态,但实际是假的选中,所以点右键的时候不会出现[复制]菜单,真是变态至极~

20231222_1335.png

解决方案

研究结论

经过 F12 分析网页元素及不段尝试,发现屏蔽复制功能主要用到以下两点:

1、网页元素 body 和 div 的样式上添加了 user-select = none。
2、通过 js 注册了鼠标选择、复制、右键等一系列事件用于屏蔽。

验证过程

1.F12 找到正文元素,然后将其它无关元素全部删除,包括将元素所有的 style、class 都删除,仍不能复制。
2.通过 F12 - 设置 - 禁用 Javascript 后可以复制了。
3.单独禁用 Javascript 也是不能复制的,根本无法选择内容。

手动解除

1.打开网页。
2.打开 F12 并在正文上点一下选择元素(因为里面是一个框架页)。
3.运行命令设置样式将 user-select 设置为 auto。

var bodyElement = document.body;
bodyElement.style.userSelect = "auto";
document.getElementsByClassName('bulb-editor')[0].style.userSelect = "auto";

4.通过 F12 - 设置 - 禁用 Javascript。
5.可以复制了。

油猴脚本

// ==UserScript==
// @name         有道云笔记复制
// @namespace    http://tampermonkey.net/
// @version      2023-12-16
// @description  try to take over the world!
// @author       xinggang
// @match        https://note.youdao.com/newEditorV1*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youdao.com
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    //延迟1.5s后设置网页 user-select = 'auto'
    setTimeout(function(){
        var bodyElement = document.body;
        bodyElement.style.userSelect = "auto";
        document.getElementsByClassName('bulb-editor')[0].style.userSelect = "auto";
    },1500);

    //替换 body 元素用于移除所有已注册的事件(发现网页有未加载的图片滚动后图片加载失败,屏蔽掉,下面用按钮手动开启)
    //setTimeout(function(){
        //var body = document.body;
        //var newBody = body.cloneNode(true);
        //document.body.parentNode.replaceChild(newBody,body);
        //console.log('已开启复制~')
    //},2500);

    //页面中增加一个按钮用于开启复制
    var button = document.createElement('button');
    button.innerHTML = '开启复制';

    // 设置按钮样式(可选)
    button.style.position = 'fixed';
    button.style.top = '10px';
    button.style.left = '10px';
    button.style.zIndex = '1000';

    // 将按钮添加到 body 元素中
    document.body.appendChild(button);

    // 添加点击事件处理程序
    button.addEventListener('click', function() {
        var body = document.body;
        var newBody = body.cloneNode(true);
        document.body.parentNode.replaceChild(newBody,body);
        console.log('已开启复制~')
        alert('已开启复制~')
    });

})();
最后修改:2024 年 01 月 05 日 09 : 02 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论