Greasy Fork is available in English.
Detect specific text patterns on mouse selection and prompt with minimal delay.
当前为
// ==UserScript==
// @name 检测网址跳转by小楠
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Detect specific text patterns on mouse selection and prompt with minimal delay.
// @match *://*/*
// @grant none
// ==/UserScript==
let isSelecting = false;
let selectedText = '';
let hasPattern = false;
let showedPopup = false;
document.addEventListener('mousedown', function() {
isSelecting = true;
});
document.addEventListener('mouseup', function() {
isSelecting = false;
if (window.getSelection().toString()) {
selectedText = window.getSelection().toString();
hasPattern = selectedText.includes('http://') || selectedText.includes('https://') || selectedText.includes('www.');
} else {
selectedText = '';
hasPattern = false;
}
});
let checkAndPrompt = function() {
if (hasPattern &&!showedPopup) {
let urlRegex = /(https?:\/\/[^\s]+)/g;
let urls = selectedText.match(urlRegex);
if (urls) {
let urlToCopy = urls[0];
navigator.clipboard.writeText(urlToCopy).then(() => {
let confirmMessage = `是否要跳转 ${urlToCopy}?`;
if (confirm(confirmMessage)) {
window.location.href = urlToCopy;
} else {
showedPopup = true;
}
});
}
}
};
document.addEventListener('selectionchange', checkAndPrompt);
// Also check immediately after mouseup in case selection didn't change but needs to be checked.
document.addEventListener('mouseup', checkAndPrompt);