Greasy Fork

Greasy Fork is available in English.

防止未经授权的自动复制

在非选词复制时显示小红点提示用户以防止未经授权的自动复制。

当前为 2024-03-03 提交的版本,查看 最新版本

// ==UserScript==
// @name         防止未经授权的自动复制
// @version      22
// @description  在非选词复制时显示小红点提示用户以防止未经授权的自动复制。
// @grant        GM_setClipboard
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-start
// @match        *://*/*
// @namespace    http://greasyfork.icu/users/452911
// ==/UserScript==

(function() {
  'use strict';

  const domain = window.location.hostname;
  let isEnabled = GM_getValue(domain, true); // 默认启用

  function toggleEnabled() {
    isEnabled = !isEnabled;
    GM_setValue(domain, isEnabled);
    alert(`脚本现在${isEnabled ? "已启用" : "已禁用"}于 ${domain}`);
    if (isEnabled) {
      registerEventListeners();
    } else {
      unregisterEventListeners();
    }
    updateMenuCommand();
  }

  function updateMenuCommand() {
    const commandName = isEnabled ? `禁用复制监听` : `启用复制监听`;
    if (typeof GM_registerMenuCommand === "function") {
      GM_registerMenuCommand(commandName, toggleEnabled);
    }
  }

  updateMenuCommand();

  if (!isEnabled) return; // 如果脚本被禁用,则不执行以下代码

  let hasCopied = false;
  let timeoutId = null;
  let dot = null;

  const handleCopy = function(event) {
    if (!isEnabled) return; // 检查是否启用
    event.preventDefault();
    const selection = window.getSelection().toString();
    if (!hasCopied && selection.trim().length > 0) {
      hasCopied = true;
      createDot(selection);
    }
  };

  const createDot = function(selection) {
    if (!isEnabled) return; // 检查是否启用
    dot = document.createElement('div');
    dot.style.width = '20px';
    dot.style.height = '20px';
    dot.style.zIndex = '9999';
    dot.style.background = 'rgba(255, 0, 0, 0.2)';
    dot.style.borderRadius = '50%';
    dot.style.position = 'fixed';
    dot.style.top = '50%';
    dot.style.right = '10px';
    dot.style.transform = 'translateY(-50%)';
    dot.style.cursor = 'pointer';
    dot.addEventListener('click', function() {
      const shouldCopy = confirm(selection);
      if (shouldCopy) {
        if (typeof GM_setClipboard === "function") {
          GM_setClipboard(selection);
        } else {
          copyToClipboard(selection);
        }
      }
      document.body.removeChild(dot);
      hasCopied = false;
    });
    document.body.appendChild(dot);

    timeoutId = setTimeout(function() {
      if (dot && dot.parentNode) {
        document.body.removeChild(dot);
      }
      hasCopied = false;
      timeoutId = null;
    }, 4000);
  };

  function handleSelectionChange() {
    if (!isEnabled) return; // 检查是否启用
    if (window.getSelection().toString().trim().length === 0) {
      document.addEventListener('copy', handleCopy, { capture: true });
    } else {
      document.removeEventListener('copy', handleCopy, { capture: true });
    }
  }

  function registerEventListeners() {
    document.addEventListener('selectionchange', handleSelectionChange);
    document.addEventListener('copy', handleCopy, { capture: true });
  }

  function unregisterEventListeners() {
    document.removeEventListener('selectionchange', handleSelectionChange);
    document.removeEventListener('copy', handleCopy, { capture: true });
    if (dot && dot.parentNode) {
      document.body.removeChild(dot);
    }
    hasCopied = false;
    clearTimeout(timeoutId);
  }

  registerEventListeners();
})();