Last active
April 10, 2024 18:43
-
-
Save aleclarson/b72cf150125875a2c61c32cbf595dec1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name X.com ~ Shift+Click to Mute | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-04-10 | |
// @description Mute the tweet under the cursor using the Shift key and click. | |
// @author Alec Larson | |
// @match https://twitter.com/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com | |
// @grant none | |
// ==/UserScript== | |
;(function () { | |
'use strict' | |
function log(...args) { | |
console.log('[shift-mute]', ...args) | |
} | |
/** @type {HTMLDivElement} */ | |
let tweet | |
document.addEventListener( | |
'mouseover', | |
(e) => { | |
const current = tweet | |
tweet = e.target.closest('[data-testid="tweet"]') | |
if (current !== tweet) { | |
if (tweet) { | |
log('mouseover tweet by', tweet.querySelectorAll('a')[2]?.textContent) | |
} else { | |
log('mouseout tweet') | |
} | |
} | |
}, | |
{ capture: true }, | |
) | |
document.addEventListener( | |
'mouseout', | |
(e) => { | |
if (tweet && !e.target.closest('[data-testid="tweet"]')) { | |
log('mouseout tweet') | |
tweet = null | |
} | |
}, | |
{ capture: true }, | |
) | |
// Capture click events on the document. Check if the hyper key was used (all modifiers are active). If so, trigger a click on the "More" button within the current tweet. | |
document.addEventListener( | |
'click', | |
(e) => { | |
if (!tweet) return | |
if ( | |
e.button === 0 && | |
e.shiftKey && | |
!e.ctrlKey && | |
!e.altKey && | |
!e.metaKey | |
) { | |
e.preventDefault() | |
e.stopPropagation() | |
const moreBtn = tweet.querySelector( | |
'[aria-haspopup="menu"][aria-label="More"]', | |
) | |
log('shift+click detected') | |
moreBtn.click() | |
// Clear the text selection caused by shift-clicking. | |
window.getSelection().removeAllRanges() | |
setTimeout(() => { | |
const muteBtn = Array.from( | |
document.querySelectorAll('[role="menu"] [role="menuitem"]'), | |
).find((e) => e.textContent.includes('Mute')) | |
if (muteBtn) { | |
muteBtn.click() | |
log('muted %s', tweet.querySelectorAll('a')[2]?.textContent) | |
} else { | |
log('mute button not found') | |
} | |
}, 50) | |
} | |
}, | |
{ capture: true }, | |
) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment