Greasy Fork is available in English.
在链接上禁用拖拽行为,左键滑动时选择文本而不是打开链接。
当前为
// ==UserScript==
// @name 链接选中
// @version 0.1
// @namespace http://greasyfork.icu/users/1171320
// @description 在链接上禁用拖拽行为,左键滑动时选择文本而不是打开链接。
// @author yzcjd
// @author2 ChatGPT4 辅助
// @match *://*/*
// @run-at document-start
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// 阻止链接的拖拽事件
document.addEventListener('mousedown', function(event) {
if (event.target.tagName === 'A') {
event.preventDefault(); // 禁止默认的拖拽行为
}
});
// 监听鼠标移动事件,当鼠标左键按下并滑动时,我们就允许选择链接内容
document.addEventListener('mousemove', function(event) {
if (event.buttons === 1 && event.target.tagName === 'A') {
const link = event.target;
if (link && link.textContent) {
// 确保选中文本而不是拖拽链接
window.getSelection().selectAllChildren(link);
}
}
});
// 鼠标松开时,恢复正常的点击行为
document.addEventListener('mouseup', function(event) {
if (event.target.tagName === 'A') {
// 如果需要,可以在此处添加打开链接的逻辑
}
});
})();