-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path124227.user.js
107 lines (86 loc) · 3.51 KB
/
124227.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
// ==UserScript==
// @name twitter-url
// @namespace http://naonie.com/projects/twitter_expand_url.html
// @description expand shorten twitter short links in tweet body
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// @version 0.2
// @include https://twitter.com/*
// ==/UserScript==
var TwitterUrl = {
set_cache: function(short_url, full_url) {
$.data(document.body, short_url, full_url);
},
get_cache: function(short_url) {
return $.data(document.body, short_url);
},
search_links: function($tweet_text) {
/* used only in conversion */
var i, short_url, short_url_in_cache, full_url, $link,
links = $tweet_text.find(".twitter-timeline-link"),
len = links.length;
for (i=0; i<len; i++) {
$link = $(links[i]);
/* link has no protocol name */
short_url = $link.html();
/* cache used for pane out short url lookup */
short_url_in_cache = this.get_cache(short_url);
if (short_url_in_cache) {
/* dealt with attr event, which twitter try to resolve all
shortened url
*/
full_url = short_url_in_cache;
} else {
/* on tweet text inserted event, dealt with some extreme case,
where innerHTML haven't been expanded.
*/
full_url = $link.attr("data-expanded-url");
}
this.expand_link($link, short_url, full_url);
}
},
expand_link: function($target, short_url, full_url) {
/* twitter triggered two times modify event, second time retrieve the
full url
*/
if (short_url !== full_url) {
if (full_url.indexOf(short_url) === -1) {
/* skip not unshortened urls, save only short_url->full_url
in cache*/
this.set_cache(short_url, full_url);
}
$target.html(full_url);
if (full_url.indexOf("http") === 0) {
$target.prop("href", full_url);
$target.prop("title", full_url);
/* in case some tweets has non-expanded url on this attribute */
$target.attr("data-expanded-url", full_url);
}
}
},
shortened: function(e) {
var $target = $(e.target);
if (!$target.hasClass("twitter-timeline-link")) {
return;
}
$(window).trigger("ShortenUrlResolve", {"target": $target});
},
poppedout: function(e, data) {
var $target = data.tweet_popped_out;
TwitterUrl.search_links($target.find(".tweet-text"));
},
shorten_resolve: function(e, data) {
var $target = data.target,
full_url = $target.attr("data-ultimate-url"),
short_url = $target.html();
TwitterUrl.expand_link($target, short_url, full_url);
}
}
/* url is shortended , .main-content use to filter DOMAttrModified events comes
out of pane out event, which is interfere changes of attributes(auto set
value back) */
$(window).on("DOMAttrModified", "",
TwitterUrl, TwitterUrl.shortened);
/* on page load up, twitter try to resolve shortened url automatically */
$(window).on("ShortenUrlResolve", TwitterUrl.shorten_resolve);
/* click on tweet-body, side pane pop out */
$(window).on("SidepanePopout", TwitterUrl.poppedout);