跳转至

使用油猴脚本解除NOJ题目右键文本选中复制限制

约 210 个字 • 67 行代码

对话记录

You:

noj

DevForge学编程社区.mhtml

i can't copy the content and Use the right-click menu with the mouse, can u help me to write a tempermonkey javascript file to unlock this things?

 

ChatGPT:

To unlock right-click menu functionality and enable content copying on a webpage, you can create a Tampermonkey script that disables the JavaScript restrictions. Here's a simple script that you can use to unlock right-click and text selection:

  1. Install Tampermonkey on your browser (if not already installed).
  2. Create a new script and paste the following code:
// ==UserScript==
// @name         Unlock Right-Click and Text Selection
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Enables right-click and text selection on pages that block it
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Remove event listeners that block right-click and selection
    document.addEventListener('contextmenu', event => event.stopPropagation(), true);
    document.addEventListener('selectstart', event => event.stopPropagation(), true);
    document.addEventListener('mousedown', event => event.stopPropagation(), true);
    document.addEventListener('copy', event => event.stopPropagation(), true);
    document.addEventListener('cut', event => event.stopPropagation(), true);
    document.addEventListener('dragstart', event => event.stopPropagation(), true);

    // Remove specific style restrictions on user-select
    const styles = document.createElement('style');
    styles.innerHTML = `
        * {
            -webkit-user-select: text !important;
            -moz-user-select: text !important;
            -ms-user-select: text !important;
            user-select: text !important;
        }
    `;
    document.head.appendChild(styles);
})();

Explanation:

  • This script removes event listeners that prevent right-click, text selection, and copying.
  • It also overrides any CSS rules that prevent text selection by enforcing user-select: text.

After installing and running the script, try accessing the page again, and you should be able to right-click and copy content freely.

在浏览器tempermonkey油猴插件中,新建脚本,再将chatgpt提供的代码复制进去保存即可

修改后的脚本代码
// ==UserScript==
// @name         Unlock Right-Click and Text Selection on NOJ
// @version      1.0
// @description  Enables right-click and text selection on pages that block it
// @author       Ronald Luo
// @copyright    2024, Ronald Luo (https://github.com/RonaldLN)
// @license      MIT
// @match        *://10.12.13.248/cpbox/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Remove event listeners that block right-click and selection
    document.addEventListener('contextmenu', event => event.stopPropagation(), true);
    document.addEventListener('selectstart', event => event.stopPropagation(), true);
    document.addEventListener('mousedown', event => event.stopPropagation(), true);
    document.addEventListener('copy', event => event.stopPropagation(), true);
    document.addEventListener('cut', event => event.stopPropagation(), true);
    document.addEventListener('dragstart', event => event.stopPropagation(), true);

    // Remove specific style restrictions on user-select
    const styles = document.createElement('style');
    styles.innerHTML = `
        * {
            -webkit-user-select: text !important;
            -moz-user-select: text !important;
            -ms-user-select: text !important;
            user-select: text !important;
        }
    `;
    document.head.appendChild(styles);
})();

最后更新: 2024-10-15
创建日期: 2024-10-15

评论