Created
September 20, 2012 09:47
-
-
Save GerjanOnline/3754974 to your computer and use it in GitHub Desktop.
Userscript: Twitter Open Link Shortcut
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 Twitter Open Link Shortcut | |
// @namespace https://gist.github.com/GerjanOnline | |
// @version 1.0 | |
// @description Open any link in the selected tweet by pressing `o' | |
// @include http://twitter.com/* | |
// @include https://twitter.com/* | |
// @match http://twitter.com/* | |
// @match https://twitter.com/* | |
// ==/UserScript== | |
(function() { | |
window.addEventListener("keydown", function(e){ | |
var tag = e.target.tagName.toLowerCase(); | |
// do not execute when in an input field or keyCode is not 'o' | |
if (tag == 'input' || tag == 'textarea' || e.keyCode != 79){ | |
return true; | |
} | |
// get the links | |
var linksToOpen = getLinks(); | |
// open all links (so can be 0-more) | |
for(i = 0; i < linksToOpen.length; i++) { | |
var url = linksToOpen[i].getAttribute("href") | |
window.open(url); | |
} | |
}, false); | |
function getLinks(){ | |
// first try website links | |
var links = document.querySelectorAll('#stream-items-id .hovered-stream-item .twitter-timeline-link'); | |
if(links.length){ return links; } | |
// second users / mentions | |
var users = document.querySelectorAll('#stream-items-id .hovered-stream-item .twitter-atreply'); | |
if (users.length) { return users; } | |
// third hashtags | |
var hashtags = document.querySelectorAll('#stream-items-id .hovered-stream-item .twitter-hashtag'); | |
if (hashtags.length) { return hashtags; } | |
// if above resulted in nothing, try pretty-links (in fact all links in a tweet will have this class) | |
var others = document.querySelectorAll('#stream-items-id .hovered-stream-item .pretty-link'); | |
if (others.length) { return others; } | |
// no links | |
return []; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi