Skip to content

Instantly share code, notes, and snippets.

@iamqiz
Last active February 24, 2022 10:32
Show Gist options
  • Save iamqiz/72985bd929b3853d5f4e638b95f2c2dc to your computer and use it in GitHub Desktop.
Save iamqiz/72985bd929b3853d5f4e638b95f2c2dc to your computer and use it in GitHub Desktop.
选中即复制 强化版
// ==UserScript==
// @name 选中即复制
// @namespace http://tampermonkey.net/
// @version 0.3.1
// @description 鼠标选中页面中的字符,自动复制进剪贴板;插件地址见https://greasyfork.org/zh-CN/scripts/410789-%E9%80%89%E4%B8%AD%E5%8D%B3%E5%A4%8D%E5%88%B6
// @author kakasearch
// @include *://**/*
// @grant unsafeWindow
// @grant GM_setClipboard
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function() {
'use strict';
//设置开关flag,默认关闭
GM_setValue("flag",false)
//备份默认的onclick函数
let default_onclick=document.onclick;
// console.log("document.onclick:",default_onclick)
//function hello2 () {let flag=GM_getValue("flag","none"); console.log(flag); GM_setValue("flag",!flag); }
function Switch () {
let flag=GM_getValue("flag")
// console.log("current flag1:",flag)
flag=!flag
// console.log("current flag2:",flag)
GM_setValue("flag",flag);
if(flag){
console.log("开启:",flag)
document.onclick = function() {
var text=""
if ( unsafeWindow.getSelection) {
text = unsafeWindow.getSelection();
} else if (document.selection) {
text = document.selection.createRange();
}else {
console.log("else");
}
//使用油猴命令将选中文本保存到剪贴板
GM_setClipboard(text.toString());
// 放到粘贴板里,操作浏览器自身的API
console.log(text.toString());
//document.execCommand('Copy'); // 执行浏览器的复制命令
}
}else{
console.log("关闭:",flag)
document.onclick=default_onclick
}
}
// GM_registerMenuCommand("Hello, 2",hello2)
//增加菜单
GM_registerMenuCommand("开关",Switch)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment