Last active
August 9, 2018 09:46
-
-
Save monoblaine/7a22a4b8098acbb20c2f0289a5bcb69d to your computer and use it in GitHub Desktop.
Open image or link embedded within a tweet
This file contains hidden or 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 Open image or link embedded within a tweet | |
// @namespace https://twitter.com/ | |
// @version 1 | |
// @include https://twitter.com/* | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
document.addEventListener('keydown', function (e) { | |
const keyName = e.key; | |
if (keyName === 'Control' || !e.ctrlKey) { | |
return; | |
} | |
let elToFocusSelector; | |
switch (keyName) { | |
case 'i': | |
openImage(); | |
break; | |
case 'l': | |
openInaccessibleLink(); | |
break; | |
default: | |
return; | |
} | |
e.preventDefault(); | |
}); | |
function openImage () { | |
const activeEl = document.activeElement; | |
if (activeEl == null) { | |
return; | |
} | |
const tweet = activeEl.closest('.tweet'); | |
const maybeIFrame = tweet.querySelector('iframe'); | |
let imgElList; | |
if (maybeIFrame != null) { | |
imgElList = maybeIFrame.contentWindow.document.body.querySelectorAll('.SummaryCard-image img'); | |
} | |
else { | |
imgElList = tweet.querySelectorAll('.AdaptiveMedia img'); | |
} | |
if (imgElList.length === 0) { | |
return; | |
} | |
Array.prototype.slice.call(imgElList) | |
.filter(imgEl => imgEl.src != null) | |
.forEach(imgEl => setTimeout(() => window.open(imgEl.src), 10)); | |
} | |
function openInaccessibleLink () { | |
const activeEl = document.activeElement; | |
if (activeEl == null) { | |
return; | |
} | |
const tweet = activeEl.closest('.tweet'); | |
const maybeIFrame = tweet.querySelector('iframe'); | |
if (maybeIFrame == null) { | |
return; | |
} | |
const linkEl = maybeIFrame.contentWindow.document.body.querySelector('a.TwitterCard-container--clickable'); | |
if (linkEl != null && linkEl.href != null) { | |
setTimeout(() => window.open(linkEl.href), 10); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment