-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter-link-converter.js
53 lines (52 loc) · 1.66 KB
/
twitter-link-converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// ==UserScript==
// @name Twitter t.co to Direct Link Converter
// @name:zh-CN 推特 t.co 转直接链接
// @namespace http://tampermonkey.net/
// @version 1.6.0
// @description Automatically converts Twitter's indirect t.co links into direct, original URLs for a more transparent and streamlined browsing experience.
// @description:zh-CN 把推特的 t.co 中转跳转链接改为直接跳转
// @author CLDXiang
// @website https://github.com/CLDXiang/tampermonkey
// @license MIT
// @match *://*twitter.com/*
// @match *://*x.com/*
// @grant none
// @run-at document-end
// ==/UserScript==
"use strict";
(() => {
// src/twitter-link-converter/main.mts
function modifyLink(link) {
if (link.href.includes("t.co")) {
let urlText = link.innerText;
if (urlText.endsWith("\u2026"))
urlText = urlText.slice(0, -1);
if (urlText.startsWith("http"))
link.href = urlText;
else if (!urlText.startsWith("/"))
link.href = `https://${urlText}`;
}
}
function observeDOM() {
try {
new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === "childList") {
mutation.addedNodes.forEach((node) => {
if (node instanceof HTMLElement) {
if (node instanceof HTMLAnchorElement)
modifyLink(node);
else
node.querySelectorAll("a").forEach(modifyLink);
}
});
}
}
}).observe(document.body, { childList: true, subtree: true });
} catch (e) {
console.log(e);
}
}
document.querySelectorAll("a").forEach(modifyLink);
observeDOM();
})();