左键选中复制

左键选中

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name              左键选中复制
// @description       左键选中
// @version           1.3
// @match             *://*/*
// @grant             unsafeWindow
// @run-at            document-body
// @namespace https://greasyfork.org/users/12375
// ==/UserScript==

(function(uw) {
    'use strict';

    const CTRL_A_MASK = 0b10000001; // 复合位掩码
    const KEY_STATES = new Uint8Array(256);
    const CLIP_WRITE = uw.navigator.clipboard?.writeText ?
        t => uw.navigator.clipboard.writeText(t).catch(()=>GM_setClipboard(t)) :
        t => GM_setClipboard(t);

    let textCache = null, tid = 0;

    const memReadSelection = () => {
        const s = document.getSelection();
        return s && s.toString() || '';
    };

    uw.addEventListener('keydown', e => KEY_STATES[e.keyCode] = e.ctrlKey | (e.keyCode === 65) << 7);
    uw.addEventListener('keyup', () => KEY_STATES.fill(0));

    document.addEventListener('selectionchange', () => {
        if (KEY_STATES[65] & CTRL_A_MASK) return;
        const txt = memReadSelection();
        if (!txt || txt === textCache) return;
        textCache = txt;
        clearTimeout(tid);
        tid = setTimeout(() => CLIP_WRITE(txt), 8);
    }, 0x7FFFFFFF);

})(unsafeWindow);