Linux简化版超级链接获取器(支持拖拽和折叠)
// ==UserScript==
// @name Linux简化版超级链接获取器(支持拖拽和折叠)
// @namespace http://tampermonkey.net/
// @version 3.4
// @description 右键点击时智能获取链接并显示在可拖拽的框中,可编辑链接并跳转,支持折叠展开
// @match https://linux.do/*
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
GM_addStyle(`
#super-link-status {
position: fixed;
top: 10px;
right: 10px;
z-index: 10000;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 5px;
border-radius: 50%;
font-size: 12px;
display: flex;
align-items: center;
cursor: move;
transition: all 0.3s ease;
}
#super-link-status.expanded {
border-radius: 4px;
padding: 5px 10px;
}
#super-link-status input {
width: 0;
padding: 0;
border: none;
background: transparent;
color: white;
transition: all 0.3s ease;
}
#super-link-status.expanded input {
width: 300px;
margin-right: 5px;
padding: 2px 5px;
border: 1px solid #ccc;
background: white;
color: black;
}
#super-link-status button {
display: none;
padding: 2px 5px;
background: #28a745;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
#super-link-status.expanded button {
display: inline-block;
}
#toggle-button {
width: 30px;
height: 30px;
background: #28a745;
border: none;
border-radius: 50%;
color: white;
font-size: 20px;
cursor: pointer;
outline: none;
}
`);
// 创建状态显示框
const statusBox = document.createElement('div');
statusBox.id = 'super-link-status';
statusBox.innerHTML = `
<button id="toggle-button">+</button>
<input type="text" id="super-link-edit" placeholder="右键点击获取链接...">
<button id="super-jump-button">跳转</button>
`;
document.body.appendChild(statusBox);
const toggleButton = document.getElementById('toggle-button');
const linkEdit = document.getElementById('super-link-edit');
const jumpButton = document.getElementById('super-jump-button');
toggleButton.addEventListener('click', function() {
statusBox.classList.toggle('expanded');
toggleButton.textContent = statusBox.classList.contains('expanded') ? '-' : '+';
});
// 添加拖拽功能
let isDragging = false;
let dragOffsetX, dragOffsetY;
statusBox.addEventListener('mousedown', function(e) {
if (e.target === statusBox || e.target === toggleButton) {
isDragging = true;
dragOffsetX = e.clientX - statusBox.getBoundingClientRect().left;
dragOffsetY = e.clientY - statusBox.getBoundingClientRect().top;
}
});
document.addEventListener('mousemove', function(e) {
if (isDragging) {
statusBox.style.left = (e.clientX - dragOffsetX) + 'px';
statusBox.style.top = (e.clientY - dragOffsetY) + 'px';
statusBox.style.right = 'auto';
}
});
document.addEventListener('mouseup', function() {
isDragging = false;
});
// 超级链接检测函数
function superDetectLink(element) {
// 检查href属性
if (element.href) {
return element.href;
}
// 检查所有可能包含链接的属性
const potentialAttributes = ['src', 'data-href', 'data-url', 'data-link'];
for (let attr of potentialAttributes) {
if (element.hasAttribute(attr)) {
const value = element.getAttribute(attr);
if (value && value.match(/^(https?:\/\/|\/)/)) {
return value;
}
}
}
// 检查所有data-*属性
for (let attr of element.attributes) {
if (attr.name.startsWith('data-') && attr.value.match(/^(https?:\/\/|\/)/)) {
return attr.value;
}
}
// 检查onclick属性
const onclickAttr = element.getAttribute('onclick');
if (onclickAttr) {
const match = onclickAttr.match(/['"]((https?:\/\/|\/)[^'"]+)['"]/);
if (match) {
return match[1];
}
}
// 检查内部文本是否包含URL
const text = element.textContent;
const urlMatch = text.match(/https?:\/\/[^\s]+/);
if (urlMatch) {
return urlMatch[0];
}
return null;
}
// 递归搜索链接
function recursivelySearchForLink(element) {
const link = superDetectLink(element);
if (link) {
return link;
}
for (let child of element.children) {
const childLink = recursivelySearchForLink(child);
if (childLink) {
return childLink;
}
}
return null;
}
// 右键点击事件处理函数
document.addEventListener('contextmenu', function(e) {
e.preventDefault(); // 禁用默认右键菜单
const clickedElement = document.elementFromPoint(e.clientX, e.clientY);
let content = '';
if (clickedElement) {
content = recursivelySearchForLink(clickedElement) || '';
}
if (!content) {
content = `在 (${e.pageX}, ${e.pageY}) 处没有找到链接`;
} else if (!content.endsWith('/activity/topics')) {
content += '/activity/topics';
}
linkEdit.value = content;
if (!statusBox.classList.contains('expanded')) {
statusBox.classList.add('expanded');
toggleButton.textContent = '-';
}
});
// 跳转按钮功能
jumpButton.addEventListener('click', function() {
const url = linkEdit.value.trim();
if (url) {
window.open(url, '_blank');
} else {
alert('请输入有效的链接!');
}
});
})();