Greasy Fork

Greasy Fork is available in English.

双击右键粘贴

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
                        })
                    }
                }
            }
        }
    });
})();