-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtwitter-reader.userscript.js
More file actions
98 lines (86 loc) · 2.33 KB
/
twitter-reader.userscript.js
File metadata and controls
98 lines (86 loc) · 2.33 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// ==UserScript==
// @name Twitter Reader Buttons
// @namespace Violentmonkey Scripts
// @match https://x.com/*
// @noframes
// @grant none
// @version 1.5
// ==/UserScript==
const BUTTONS = [
{ id: 'quick25', text: 'Middle (25 req)', limit: 25 },
{ id: 'quick5', text: 'Quick (5 req)', limit: 5 },
{ id: 'thread', text: 'Thread', limit: null },
];
const LOCALHOST = 'http://localhost:3412';
const container = document.createElement('div');
container.setAttribute('style', `
position:fixed;
top:16px;
right:16px;
z-index:9999;
display:flex;
gap:20px;
`);
document.body.appendChild(container);
function createButton(cfg) {
const a = document.createElement('a');
a.id = cfg.id;
a.textContent = cfg.text;
a.href = '#';
a.setAttribute('style', `
background:#fff;
color:#111;
border:1px solid #ccc;
border-radius:4px;
padding:6px 12px;
font-size:14px;
text-decoration:none;
box-shadow:0 2px 8px rgba(0,0,0,0.07);
display:none;
`);
container.appendChild(a);
return a;
}
const btnMap = {};
BUTTONS.forEach(cfg => { btnMap[cfg.id] = createButton(cfg); });
const isStatusPage = () => /\/status\/\d+/.test(location.pathname);
function updateButtons() {
const visible = isStatusPage();
BUTTONS.forEach(cfg => {
const el = btnMap[cfg.id];
if (visible) {
const url = new URL(LOCALHOST);
url.searchParams.set('url', location.href);
if (cfg.limit) url.searchParams.set('limit_requests', cfg.limit);
el.href = url.toString();
el.style.display = 'inline-block';
} else {
el.style.display = 'none';
}
});
}
function dispatchLocationChange() {
window.dispatchEvent(new Event('vm-locationchange'));
}
const origPush = history.pushState;
history.pushState = function (...args) {
const ret = origPush.apply(this, args);
dispatchLocationChange();
return ret;
};
const origReplace = history.replaceState;
history.replaceState = function (...args) {
const ret = origReplace.apply(this, args);
dispatchLocationChange();
return ret;
};
window.addEventListener('popstate', dispatchLocationChange);
window.addEventListener('vm-locationchange', updateButtons);
let lastHref = location.href;
setInterval(() => {
if (location.href !== lastHref) {
lastHref = location.href;
updateButtons();
}
}, 500);
updateButtons();