双击右键粘贴

双击右键粘贴,Chrome 浏览器测试通过

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         双击右键粘贴
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  双击右键粘贴,Chrome 浏览器测试通过
// @author       hostloc @大师兄
// @match        http*://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var CONFIG = {
        threshold: 500,
        _storageKey: 'rightClickTime'
    };
    document.addEventListener('mouseup', function (e) {
        if (e.button === 2) {
            if (!window.localStorage.getItem(CONFIG._storageKey)) {
                window.localStorage.setItem(CONFIG._storageKey, String((new Date()).valueOf()));
            } else {
                var lastTime = Number(window.localStorage.getItem(CONFIG._storageKey));
                var thisTime = (new Date()).valueOf();
                window.localStorage.setItem(CONFIG._storageKey, String(thisTime));
                if (thisTime - lastTime <= CONFIG.threshold) {
                    if ((e.target.localName.toLowerCase() === 'input' || e.target.localName.toLowerCase() === 'textarea') && e.target.disabled === false) {
                        var inp = e.target;
                        console.log('pasted')
                        window.navigator.clipboard.readText().then(function (d) {
                            var cursurPosition = (inp.selectionStart) ? inp.selectionStart : null;
                            if (!cursurPosition && document.selection) {
                                var range = document.selection.createRange();
                                range.moveStart("character", -inp.value.length);
                                cursurPosition = range.text.length;
                            }
                            var oldVal = inp.value;
                            var newVal = oldVal.slice(0, cursurPosition) + d + oldVal.slice(cursurPosition);
                            inp.value = newVal;
                            window.localStorage.removeItem(CONFIG._storageKey);
                        })
                    }
                }
            }
        }
    });
})();