|
| 1 | +// ==UserScript== |
| 2 | +// @name Linux.do 链接新标签页打开 |
| 3 | +// @namespace http://tampermonkey.net/ |
| 4 | +// @version 2.0 |
| 5 | +// @description 强制让 linux.do 的主题列表链接在新标签页打开 |
| 6 | +// @author You |
| 7 | +// @match https://linux.do/* |
| 8 | +// @grant none |
| 9 | +// ==/UserScript== |
| 10 | + |
| 11 | +(function () { |
| 12 | + 'use strict' |
| 13 | + |
| 14 | + // 选择器数组 |
| 15 | + const selectors = [ |
| 16 | + '.topic-list-main-link a.title', |
| 17 | + '.topic-list .main-link a.title', |
| 18 | + ] |
| 19 | + |
| 20 | + // 检查元素是否匹配我们的选择器 |
| 21 | + function isTargetLink(element) { |
| 22 | + return selectors.some((selector) => { |
| 23 | + return element.matches(selector) |
| 24 | + }) |
| 25 | + } |
| 26 | + |
| 27 | + // 使用事件委托处理点击事件 |
| 28 | + document.addEventListener('click', (event) => { |
| 29 | + const target = event.target |
| 30 | + |
| 31 | + // 检查点击的是否是我们要处理的链接 |
| 32 | + if (target.tagName === 'A' && isTargetLink(target)) { |
| 33 | + // 阻止默认行为 |
| 34 | + event.preventDefault() |
| 35 | + event.stopPropagation() |
| 36 | + |
| 37 | + // 获取链接地址 |
| 38 | + const href = target.href |
| 39 | + if (href) { |
| 40 | + // 在新标签页打开 |
| 41 | + window.open(href, '_blank', 'noopener,noreferrer') |
| 42 | + } |
| 43 | + } |
| 44 | + }, true) // 使用捕获阶段,确保在 Discourse 的事件处理器之前执行 |
| 45 | + |
| 46 | + // 额外的处理:直接修改链接的 onclick 事件 |
| 47 | + function processLinks() { |
| 48 | + selectors.forEach((selector) => { |
| 49 | + const links = document.querySelectorAll(selector) |
| 50 | + links.forEach((link) => { |
| 51 | + if (!link.hasAttribute('data-processed')) { |
| 52 | + // 移除可能存在的事件监听器 |
| 53 | + link.onclick = function (e) { |
| 54 | + e.preventDefault() |
| 55 | + e.stopPropagation() |
| 56 | + window.open(this.href, '_blank', 'noopener,noreferrer') |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + // 标记为已处理 |
| 61 | + link.setAttribute('data-processed', 'true') |
| 62 | + } |
| 63 | + }) |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + // 初始处理 |
| 68 | + processLinks() |
| 69 | + |
| 70 | + // 使用 MutationObserver 监听新增的链接 |
| 71 | + const observer = new MutationObserver((mutations) => { |
| 72 | + let shouldProcess = false |
| 73 | + |
| 74 | + mutations.forEach((mutation) => { |
| 75 | + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { |
| 76 | + mutation.addedNodes.forEach((node) => { |
| 77 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 78 | + const hasTargetLinks = selectors.some((selector) => { |
| 79 | + return node.matches && node.matches(selector) |
| 80 | + || node.querySelector && node.querySelector(selector) |
| 81 | + }) |
| 82 | + |
| 83 | + if (hasTargetLinks) { |
| 84 | + shouldProcess = true |
| 85 | + } |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + if (shouldProcess) { |
| 92 | + // 延迟处理,确保元素完全加载 |
| 93 | + setTimeout(processLinks, 100) |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + // 开始观察 |
| 98 | + observer.observe(document.body, { |
| 99 | + childList: true, |
| 100 | + subtree: true, |
| 101 | + }) |
| 102 | +})() |
0 commit comments