0 | |
(function($) {
|
1 | |
|
2 | |
$.fn.tweet = function(o){
|
3 | |
var s = {
|
4 | |
username: ["seaofclouds"], // [string] required, unless you want to display our tweets. :) it can be an array, just do ["username1","username2","etc"]
|
5 | |
list: null, //[string] optional name of list belonging to username
|
6 | |
avatar_size: null, // [integer] height and width of avatar if displayed (48px max)
|
7 | |
count: 3, // [integer] how many tweets to display?
|
8 | |
intro_text: null, // [string] do you want text BEFORE your your tweets?
|
9 | |
outro_text: null, // [string] do you want text AFTER your tweets?
|
10 | |
join_text: null, // [string] optional text in between date and tweet, try setting to "auto"
|
11 | |
auto_join_text_default: "i said,", // [string] auto text for non verb: "i said" bullocks
|
12 | |
auto_join_text_ed: "i", // [string] auto text for past tense: "i" surfed
|
13 | |
auto_join_text_ing: "i am", // [string] auto tense for present tense: "i was" surfing
|
14 | |
auto_join_text_reply: "i replied to", // [string] auto tense for replies: "i replied to" @someone "with"
|
15 | |
auto_join_text_url: "i was looking at", // [string] auto tense for urls: "i was looking at" http:...
|
16 | |
loading_text: null, // [string] optional loading text, displayed while tweets load
|
17 | |
query: null // [string] optional search query
|
18 | |
};
|
19 | |
|
20 | |
if(o) $.extend(s, o);
|
21 | |
|
22 | |
$.fn.extend({
|
23 | |
linkUrl: function() {
|
24 | |
var returning = [];
|
25 | |
var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
|
26 | |
this.each(function() {
|
27 | |
returning.push(this.replace(regexp,"<a href=\"$1\">$1</a>"));
|
28 | |
});
|
29 | |
return $(returning);
|
30 | |
},
|
31 | |
linkUser: function() {
|
32 | |
var returning = [];
|
33 | |
var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
|
34 | |
this.each(function() {
|
35 | |
returning.push(this.replace(regexp,"<a href=\"http://twitter.com/$1\">@$1</a>"));
|
36 | |
});
|
37 | |
return $(returning);
|
38 | |
},
|
39 | |
linkHash: function() {
|
40 | |
var returning = [];
|
41 | |
var regexp = / [\#]+([A-Za-z0-9-_]+)/gi;
|
42 | |
this.each(function() {
|
43 | |
returning.push(this.replace(regexp, ' <a href="http://search.twitter.com/search?q=&tag=$1&lang=all&from='+s.username.join("%2BOR%2B")+'">#$1</a>'));
|
44 | |
});
|
45 | |
return $(returning);
|
46 | |
},
|
47 | |
capAwesome: function() {
|
48 | |
var returning = [];
|
49 | |
this.each(function() {
|
50 | |
returning.push(this.replace(/\b(awesome)\b/gi, '<span class="awesome">$1</span>'));
|
51 | |
});
|
52 | |
return $(returning);
|
53 | |
},
|
54 | |
capEpic: function() {
|
55 | |
var returning = [];
|
56 | |
this.each(function() {
|
57 | |
returning.push(this.replace(/\b(epic)\b/gi, '<span class="epic">$1</span>'));
|
58 | |
});
|
59 | |
return $(returning);
|
60 | |
},
|
61 | |
makeHeart: function() {
|
62 | |
var returning = [];
|
63 | |
this.each(function() {
|
64 | |
returning.push(this.replace(/(<)+[3]/gi, "<tt class='heart'>♥</tt>"));
|
65 | |
});
|
66 | |
return $(returning);
|
67 | |
}
|
68 | |
});
|
69 | |
|
70 | |
function relative_time(time_value) {
|
71 | |
var parsed_date = Date.parse(time_value);
|
72 | |
var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
|
73 | |
var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
|
74 | |
var pluralize = function (singular, n) {
|
75 | |
return '' + n + ' ' + singular + (n == 1 ? '' : 's');
|
76 | |
};
|
77 | |
if(delta < 60) {
|
78 | |
return 'less than a minute ago';
|
79 | |
} else if(delta < (45*60)) {
|
80 | |
return 'about ' + pluralize("minute", parseInt(delta / 60)) + ' ago';
|
81 | |
} else if(delta < (24*60*60)) {
|
82 | |
return 'about ' + pluralize("hour", parseInt(delta / 3600)) + ' ago';
|
83 | |
} else {
|
84 | |
return 'about ' + pluralize("day", parseInt(delta / 86400)) + ' ago';
|
85 | |
}
|
86 | |
}
|
87 | |
|
88 | |
function build_url() {
|
89 | |
var proto = ('https:' == document.location.protocol ? 'https:' : 'http:');
|
90 | |
if (s.list) {
|
91 | |
return proto+"//api.twitter.com/1/"+s.username[0]+"/lists/"+s.list+"/statuses.json?per_page="+s.count+"&callback=?";
|
92 | |
} else if (s.query == null && s.username.length == 1) {
|
93 | |
return proto+'//twitter.com/status/user_timeline/'+s.username[0]+'.json?count='+s.count+'&callback=?';
|
94 | |
} else {
|
95 | |
var query = (s.query || 'from:'+s.username.join('%20OR%20from:'));
|
96 | |
return proto+'//search.twitter.com/search.json?&q='+query+'&rpp='+s.count+'&callback=?';
|
97 | |
}
|
98 | |
}
|
99 | |
|
100 | |
return this.each(function(){
|
101 | |
var list = $('<ul class="tweet_list">').appendTo(this);
|
102 | |
var intro = '<p class="tweet_intro">'+s.intro_text+'</p>';
|
103 | |
var outro = '<p class="tweet_outro">'+s.outro_text+'</p>';
|
104 | |
var loading = $('<p class="loading">'+s.loading_text+'</p>');
|
105 | |
|
106 | |
if(typeof(s.username) == "string"){
|
107 | |
s.username = [s.username];
|
108 | |
}
|
109 | |
|
110 | |
if (s.loading_text) $(this).append(loading);
|
111 | |
$.getJSON(build_url(), function(data){
|
112 | |
if (s.loading_text) loading.remove();
|
113 | |
if (s.intro_text) list.before(intro);
|
114 | |
$.each((data.results || data), function(i,item){
|
115 | |
// auto join text based on verb tense and content
|
116 | |
if (s.join_text == "auto") {
|
117 | |
if (item.text.match(/^(@([A-Za-z0-9-_]+)) .*/i)) {
|
118 | |
var join_text = s.auto_join_text_reply;
|
119 | |
} else if (item.text.match(/(^\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+) .*/i)) {
|
120 | |
var join_text = s.auto_join_text_url;
|
121 | |
} else if (item.text.match(/^((\w+ed)|just) .*/im)) {
|
122 | |
var join_text = s.auto_join_text_ed;
|
123 | |
} else if (item.text.match(/^(\w*ing) .*/i)) {
|
124 | |
var join_text = s.auto_join_text_ing;
|
125 | |
} else {
|
126 | |
var join_text = s.auto_join_text_default;
|
127 | |
}
|
128 | |
} else {
|
129 | |
var join_text = s.join_text;
|
130 | |
};
|
131 | |
|
132 | |
var from_user = item.from_user || item.user.screen_name;
|
133 | |
var profile_image_url = item.profile_image_url || item.user.profile_image_url;
|
134 | |
var join_template = '<span class="tweet_join"> '+join_text+' </span>';
|
135 | |
var join = ((s.join_text) ? join_template : ' ');
|
136 | |
var avatar_template = '<a class="tweet_avatar" href="http://twitter.com/'+from_user+'"><img src="'+profile_image_url+'" height="'+s.avatar_size+'" width="'+s.avatar_size+'" alt="'+from_user+'\'s avatar" title="'+from_user+'\'s avatar" border="0"/></a>';
|
137 | |
var avatar = (s.avatar_size ? avatar_template : '');
|
138 | |
var date = '<a href="http://twitter.com/'+from_user+'/statuses/'+item.id+'" title="view tweet on twitter">'+relative_time(item.created_at)+'</a>';
|
139 | |
var text = '<span class="tweet_text">' +$([item.text]).linkUrl().linkUser().linkHash().makeHeart().capAwesome().capEpic()[0]+ '</span>';
|
140 | |
|
141 | |
// until we create a template option, arrange the items below to alter a tweet's display.
|
142 | |
list.append('<li>' + avatar + date + join + text + '</li>');
|
143 | |
|
144 | |
list.children('li:first').addClass('tweet_first');
|
145 | |
list.children('li:odd').addClass('tweet_even');
|
146 | |
list.children('li:even').addClass('tweet_odd');
|
147 | |
});
|
148 | |
if (s.outro_text) list.after(outro);
|
149 | |
});
|
150 | |
|
151 | |
});
|
152 | |
};
|
153 | |
})(jQuery);⏎
|