Skip to content

Instantly share code, notes, and snippets.

@mikeyk
Created December 29, 2008 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeyk/41419 to your computer and use it in GitHub Desktop.
Save mikeyk/41419 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name RetweetHelper
// @namespace retweethelper
// @include http://twitter.com/home
// @exclude http://twitter.com/*/status/*
// ==/UserScript==
/*
by Mike Krieger (@mikeyk)
original suggestion from Sanjay Kairam (@skairam), machine tag suggestion
from Chris Messina (@factoryjoe)
Adds machine-tag hashtag support to Twitter's Web interface.
Adds a "retweet this" link to each tweet, which will add
#twitter:status=tweet_id to the new tweet entry box.
Also, when reading tweets, will automatically link up
#twitter:status=tweet_id text, and when it's clicked,
will fetch the correct link to that tweet's individual
page and then go to that tweet page.
Current limitations:
-- Doesn't show "retweet this" or highlight links
for tweets that have just been posted / loaded
into page using AJAX (anyone know what the
callback for that AJAX post function is?)
*/
(function(){
var tweetEntries = document.getElementsByClassName('entry-content');
function linkUpHashTags(){
/* replace links in existing tweets */
for(var entry = 0; entry < tweetEntries.length; entry++){
var textEntry = tweetEntries[entry].innerHTML;
if(textEntry.search("#twitter\:status") != -1){
tweetEntries[entry].innerHTML = textEntry.replace(/\#twitter\:status\=(\d+)/, "<a href='#'>#twitter:status=$1</a>");
tweetEntries[entry].addEventListener('click', function(e){
// make sure it was an actual #twitter:status link
if(e.target.href && e.target.innerHTML.search("#twitter\:status") != -1){
var tweetId = e.target.innerHTML.match(/\#twitter\:status\=(\d+)/)[1];
if(tweetId){
GM_xmlhttpRequest({
method: 'GET',
url: 'http://twitter.com/statuses/show/'+tweetId+'.json',
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'application/json, text/json',
},
onload: function(responseDetails) {
var json = eval('(' + responseDetails.responseText + ')');
if(json){
var screenName = json['user']['screen_name'];
if(screenName) window.location.href = "http://twitter.com/" + screenName + "/status/" + tweetId;
}
}
});
}
}
}, true);
}
}
}
linkUpHashTags();
/* set up retweet if you're on a page with a status box */
if(document.getElementById('status')){
var tweetLinks = document.getElementsByClassName('entry-date');
var tweetBoxes = document.getElementsByClassName('status-body');
var startIndex = 0;
if(tweetLinks.length - tweetBoxes.length == 1){
startIndex = 1;
};
for(var tweet = startIndex; tweet < tweetLinks.length; tweet++){
var linkElems = tweetLinks[tweet].href.split('/');
var tweetId = linkElems[linkElems.length-1];
var newLink = document.createElement("a");
newLink.href = "#";
newLink.innerHTML = " " + "retweet this";
tweetLinks[tweet].parentNode.appendChild(newLink);
newLink.id = "retweet-" + tweetId;
newLink.addEventListener('click', function(event){
// this is a closure, so we can't rely on variables above
// or we'll end up just taking the last element in the for
// loop's parameters; make sure you're using the event, instead
document.getElementById('status').value += ("#twitter:status=" + event.target.id.split('-')[1] + " ");
document.getElementById('status').focus();
}, true);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment