Skip to content

Instantly share code, notes, and snippets.

@mcornella
Last active October 12, 2019 09:41
Show Gist options
  • Save mcornella/9166edb5701aa11a1e68281a749da94f to your computer and use it in GitHub Desktop.
Save mcornella/9166edb5701aa11a1e68281a749da94f to your computer and use it in GitHub Desktop.
Allow use of middle click (wheel click) to open quoted tweets
function openquotedtweet(e) {
// Taken from https://stackoverflow.com/a/39165137/5798232
function FindReact(dom) {
let key = Object.keys(dom).find(key=>key.startsWith("__reactInternalInstance$"));
let internalInstance = dom[key];
if (internalInstance === null) return null;
if (internalInstance.return) { // react 16+
return internalInstance._debugOwner
? internalInstance._debugOwner.stateNode
: internalInstance.return.stateNode;
} else { // react <16
return internalInstance._currentElement._owner._instance;
}
}
if (e.button === 1 && e.which === 2) {
try {
let quotedtweet = e.target.closest('[role="blockquote"]');
if (quotedtweet === null) return;
let reactElem = FindReact(quotedtweet);
if (reactElem === null) return;
let pathname = reactElem.props.link.pathname;
e.preventDefault();
window.open(`${window.location.origin}${pathname}`);
} catch {}
}
}
document.querySelector('[data-reactroot]').onmousedown = openquotedtweet;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment