-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter-delete-hotkey.user.js
More file actions
72 lines (59 loc) · 2.1 KB
/
twitter-delete-hotkey.user.js
File metadata and controls
72 lines (59 loc) · 2.1 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
// ==UserScript==
// @name Twitter - Delete Hotkey
// @namespace https://github.com/digitalby
// @version 1.1.1
// @author digitalby
// @description Press d on a focused tweet to delete it (via the ... menu)
// @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('d', 'Delete focused tweet');
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;
}
async function deleteTweet() {
const article = getFocusedTweet();
if (!article) return;
const caret = article.querySelector('[data-testid="caret"]');
if (!caret) return;
caret.click();
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('Delete')) {
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;
if (e.key === 'd') {
e.preventDefault();
e.stopPropagation();
deleteTweet();
}
});
console.log('[DeleteHotkey] Loaded: d=delete tweet');
})();