Skip to content

Instantly share code, notes, and snippets.

@ankit
Created October 3, 2010 13:18
Show Gist options
  • Save ankit/608566 to your computer and use it in GitHub Desktop.
Save ankit/608566 to your computer and use it in GitHub Desktop.
Userscript to add 'o' shortcut to the new Twitter. Opens shared links, @reply usernames, hashtags shared in a tweet, in that order of priority
// ==UserScript==
// @name 'o' shortcut for new Twitter
// @description Adds an 'o' shortcut to open any shared links in selected tweet in a new tab in the background. If no link is found, looks for @usernames. And finally if no usernames, looks for hashtags.
// @include http://twitter.com/*
// @author Ankit Ahuja
// ==/UserScript==
// DISCLAIMER: Only tested on Chrome!
document.addEventListener('keyup', function(e) {
var tag = e.target.tagName.toLowerCase();
// keycode for 'o' is 79/111
if (tag == 'input' || tag == 'textarea' || e.keyCode != 79)
return true;
var h_el = document.getElementsByClassName('hovered-stream-item');
if (h_el.length != 0) {
// find links
var links = h_el[0].getElementsByClassName('twitter-timeline-link');
if (links.length != 0) {
// open the first found link in a background tab. TODO: Add ability to open multiple links
simulateClick(links[0], true);
return true;
}
// otherwise, simulate a click on first found @username in tweet
var user = h_el[0].getElementsByClassName('twitter-atreply');
if (user.length != 0) {
simulateClick(user[0]);
return true;
}
// finally, look for hashtags. if found, open in a background tab
var hashtag = h_el[0].getElementsByClassName('twitter-hashtag');
if (hashtag.length != 0) {
simulateClick(hashtag[0], true);
return true;
}
}
}, true);
function simulateClick(el, target) {
var evt = document.createEvent("MouseEvents");
//on Mac, pass target as e.metaKey
if (navigator.platform.indexOf("Mac") != -1)
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, target, 0, null);
else //otherwise, pass target as e.ctrlKey
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, target, false, false, false, 0, null);
return el.dispatchEvent(evt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment