选中的文本后在浏览器右上角弹出菜单(位置固定),可以进行搜索,复制,识别其中的网站。
当前为
// ==UserScript==
// @name 选中文本
// @author LceAn
// @version 1.0.2
// @namespace https://gf.qytechs.cn/zh-CN/users/858044
// @description 选中的文本后在浏览器右上角弹出菜单(位置固定),可以进行搜索,复制,识别其中的网站。
// @match http://*/*
// @include http://*
// @include https://*
// @grant GM_setClipboard
// @encoding utf-8
// @license GPL-3.0 License
// ==/UserScript==
(function() {
'use strict';
// Create the menu element
const menu = document.createElement('div');
menu.id = 'text-selection-menu';
menu.style.position = 'fixed';
menu.style.top = '20px';
menu.style.right = '20px';
menu.style.border = '1px solid #ccc';
menu.style.background = '#fff';
menu.style.padding = '10px';
menu.style.borderRadius = '5px';
menu.style.boxShadow = '2px 2px 5px #ccc';
menu.style.display = 'none';
document.body.appendChild(menu);
// Create the search option
const searchOption = document.createElement('a');
searchOption.href = '#';
searchOption.textContent = '搜索';
searchOption.style.display = 'block';
searchOption.style.marginBottom = '5px';
menu.appendChild(searchOption);
// Create the copy option
const copyOption = document.createElement('a');
copyOption.href = '#';
copyOption.textContent = '复制';
copyOption.style.display = 'block';
copyOption.style.marginBottom = '5px';
menu.appendChild(copyOption);
// Create the link option
const linkOption = document.createElement('a');
linkOption.href = '#';
linkOption.textContent = '链接';
linkOption.style.display = 'block';
menu.appendChild(linkOption);
// Add click event listeners to the options
searchOption.addEventListener('click', function(event) {
event.preventDefault();
const selectedText = window.getSelection().toString();
if (selectedText) {
window.open('https://www.google.com/search?q=' + selectedText, '_blank');
}
menu.style.display = 'none';
});
copyOption.addEventListener('click', function(event) {
event.preventDefault();
const selectedText = window.getSelection().toString();
if (selectedText) {
GM_setClipboard(selectedText);
}
menu.style.display = 'none';
});
// 链接无法匹配消息对话框样式函数
function showMessage(message, timeout) {
const messageBox = document.createElement('div');
messageBox.textContent = message;
messageBox.style.position = 'fixed';
messageBox.style.top = '0';
messageBox.style.left = '0';
messageBox.style.right = '0';
messageBox.style.backgroundColor = 'rgba(255, 255, 255, 0.8)';
messageBox.style.color = 'black';
messageBox.style.padding = '10px';
messageBox.style.fontSize = '18px';
messageBox.style.fontWeight = 'bold';
messageBox.style.textAlign = 'center';
messageBox.style.zIndex = '9999';
document.body.appendChild(messageBox);
setTimeout(function() {
messageBox.style.display = 'none';
}, timeout);
}
linkOption.addEventListener('click', function(event) {
event.preventDefault();
const selectedText = window.getSelection().toString();
const urlRegex = /((https?:\/\/|www\.)[\x21-\x7e]+[\w\/=]|\w([\w._-])+@\w[\w\._-]+\.(com|cn|org|net|info|tv|cc|gov|edu)|(\w[\w._-]+\.(com|cn|org|net|info|tv|cc|gov|edu))(\/[\x21-\x7e]*[\w\/])?|ed2k:\/\/[\x21-\x7e]+\|\/|thunder:\/\/[\x21-\x7e]+=)/gi;
const selectedUrl = selectedText.match(urlRegex);
if (selectedUrl) {
const url = selectedUrl[0];
if (!/^https?:\/\//i.test(url)) {
window.open(`http://${url}`, '_blank');
} else {
window.open(url, '_blank');
}
}else {
showMessage('未匹配到链接!', 1000);
}
menu.style.display = 'none';
});
// Add a listener for text selection
document.addEventListener('mouseup', function(event) {
const selectedText = window.getSelection().toString();
if (selectedText) {
menu.style.display = 'block';
} else {
menu.style.display = 'none';
}
});
})();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址