-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter-post-activity-hotkeys.user.js
More file actions
98 lines (84 loc) · 3.16 KB
/
Copy pathtwitter-post-activity-hotkeys.user.js
File metadata and controls
98 lines (84 loc) · 3.16 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 - Post Activity Hotkeys
// @namespace https://github.com/digitalby
// @version 1.1.1
// @author digitalby
// @description Keyboard shortcuts for post activity: v to view, q/t/l to switch tabs (Quotes/Reposts/Likes)
// @match https://twitter.com/*
// @match https://x.com/*
// @require https://raw.githubusercontent.com/digitalby/twitter-userscripts/main/twitter-custom-keys.lib.js
// @grant none
// ==/UserScript==
(function () {
'use strict';
window.__twitterCustomKeys?.register('v', 'View post activity');
window.__twitterCustomKeys?.register('q', 'Quotes tab');
window.__twitterCustomKeys?.register('t', 'Reposts tab');
window.__twitterCustomKeys?.register('l', 'Likes tab');
function isTyping(event) {
return window.__twitterCustomKeys?.isTyping?.(event) ?? false;
}
function getFocusedTweet() {
let el = document.activeElement;
while (el) {
if (el.matches && el.matches('article[data-testid="tweet"]')) return el;
el = el.parentElement;
}
return null;
}
function clickTab(label) {
const tabs = document.querySelectorAll('[role="tab"]');
for (const tab of tabs) {
if (tab.textContent.toLowerCase().includes(label.toLowerCase())) {
tab.click();
return true;
}
}
return false;
}
async function viewPostActivity() {
const article = getFocusedTweet();
if (!article) return;
const caret = article.querySelector('[data-testid="caret"]');
if (!caret) return;
caret.click();
// Wait for dropdown menu to appear
let menu = null;
for (let i = 0; i < 10; i++) {
await new Promise(r => setTimeout(r, 50));
menu = document.querySelector('[role="menu"]');
if (menu) break;
}
if (!menu) return;
const items = menu.querySelectorAll('[role="menuitem"]');
for (const item of items) {
if (item.textContent.includes('View post activity')) {
item.click();
return;
}
}
// Not found (not tweet author) — close menu
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
}
document.addEventListener('keydown', function (e) {
if (isTyping(e)) return;
if (e.ctrlKey || e.metaKey || e.altKey) return;
switch (e.key) {
case 'v':
e.preventDefault();
e.stopPropagation();
viewPostActivity();
break;
case 'q':
if (clickTab('Quotes')) { e.preventDefault(); e.stopPropagation(); }
break;
case 't':
if (clickTab('Reposts')) { e.preventDefault(); e.stopPropagation(); }
break;
case 'l':
if (clickTab('Likes')) { e.preventDefault(); e.stopPropagation(); }
break;
}
});
console.log('[PostActivityHotkeys] Loaded: v=view activity, q=quotes, t=reposts, l=likes');
})();