mikeyk (owner)

Revisions

gist: 41419 Download_button fork
public
Public Clone URL: git://gist.github.com/41419.git
Embed All Files: show embed
retweethelper.user.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// ==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);
    }
    
}
 
 
 
})();