forked from aaronpk/IndieWeb-Reply-Browser-Extension
-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_script.js
207 lines (168 loc) · 6.7 KB
/
content_script.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// ==UserScript==
// @name IndieWebReply
// @include http://*
// @include https://*
// @require jquery-1.8.3.min.js
// @require URI.js
// ==/UserScript==
/*
* Copyright (c) 2012 by Aaron Parecki. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
*/
var $ = window.$.noConflict(true);
var IndieWebReplyModule = (function () {
// Private
function openNoteUI(verb, replace) {
kango.invokeAsync('kango.storage.getItem', verb + 'URL', function (postURL) {
if (postURL === undefined) {
alert('You must set a Reply Post URL in Chrome options in order to use IndieWeb Reply');
return false;
}
// Replace template vars
for (var template in replace) {
if (replace[template] == undefined)
continue;
postURL = postURL.split('{' + template + '}').join(encodeURIComponent(replace[template].replace(/^\s+|\s+$/g, '')));
}
// replace any unreplaced templates with nothing
postURL = postURL.replace(/\{[a-zA-Z0-9_-]+\}/gi, '');
window.open(postURL);
});
}
function parseQueryString(url) {
var uri = new URI(url);
return uri.search(true);
}
function parseQueryStringFragment(url) {
var uri = new URI(url);
var fragment = '?' + uri.fragment();
var fragURI = new URI(fragment);
return fragURI.search(true);
}
function createReplacementButton(text, clickCallback) {
return $('<div />')
.text(text)
.css({
'background-color': '#759AE9',
'background-image': 'linear-gradient(top, #759AE9 0%, #376FE0 50%, #1A5AD9 50%, #2463DE 100%)',
'border-top': '1px solid #1F58CC',
'border-right': '1px solid #1B4DB3',
'border-bottom': '1px solid #174299',
'border-left': '1px solid #1B4DB3',
'border-radius': '0.5em',
'-webkit-box-shadow': 'inset 0 0 2px 0 rgba(57, 140, 255, 0.8)',
'box-shadow': 'inset 0 0 2px 0 rgba(57, 140, 255, 0.8)',
'color': 'white',
'font-family': '"helvetica neue", helvetica, arial, sans-serif',
'padding': '0.5em',
'text-shadow': '0 -1px 1px #1A5AD9',
'display': 'inline'
})
.click(clickCallback);
}
function bindTwitterReplies() {
$("a.js-action-reply").each(function(i, e) {
$(e).unbind("click").click(function(evt) {
var tweet = $(evt.target).parents(".tweet");
var url = "https://twitter.com/" + $(tweet).data('screen-name') + "/status/" + $(tweet).attr('data-item-id');
openNoteUI('reply', {
url: url,
username: '@' + $(tweet).data('screen-name')
});
return false;
});
});
}
function bindTwitterRetweets() {
$("a.js-toggle-rt").each(function(i,e) {
$(e).unbind("click").click(function(evt) {
var tweet = $(evt.target).parents(".tweet");
var tweetUrl = "https://twitter.com/" + $(tweet).data('screen-name') + "/status/" + $(tweet).attr('data-item-id');
openNoteUI('post', {
tweetURL: tweetUrl,
username: '@' + $(tweet).data('screen-name'),
text: $(tweet).find('.js-tweet-text').text()
});
return false;
});
});
}
function bindTwitterFavourites() {
$("a.js-toggle-fav").each(function(i, e) {
$(e).unbind("click").click(function(evt) {
var tweet = $(evt.target).parents(".tweet");
var url = "https://twitter.com/" + $(tweet).data('screen-name') + "/status/" + $(tweet).attr('data-item-id');
openNoteUI('favourite', {
url: url,
username: '@' + $(tweet).data('screen-name'),
text: $(tweet).find('.js-tweet-text').text()
});
return false;
});
});
}
function bindAppDotNetReplies() {
// Set up app.net reply URLs
$("a[data-reply-to='']").each(function(i, e) {
$(e).click(function(evt) {
var post = $(evt.target).parents(".post-container");
var url = "https://alpha.app.net/" + $(post).data('post-author-username') + "/post/" + $(post).attr('data-post-id');
openNoteUI('reply', { url: url });
return false;
});
});
}
function bindTwitterShareButtons() {
$('.twitter-share-button').each(function (i, e) {
var src = $(e).attr('src');
var properties = parseQueryStringFragment(src);
var newButton = createReplacementButton('Post to your Indieweb Site', function () {
openNoteUI('post', {
url: (properties.url ? properties.url : document.location.href),
username: '@' + properties.via,
hashtags: properties.hashtags,
text: properties.text
});
});
$(e).replaceWith(newButton);
});
}
function bindFacebookLikeButtons() {
$('.fb-like').each(function (i, e) {
var url = $(e).attr('href');
var properties = {
url: (url ? url : document.location.href)
};
var newButton = createReplacementButton('Post to your Indieweb Site', function () { openNoteUI('favourite', properties); });
$(e).replaceWith(newButton);
});
}
function bindGooglePlusOneButtons() {
$('[id^="___plusone_"]').each(function (i, e) {
var q = new URI($(e).find('iframe').attr('src')).search(true);
var properties = {
url: (q.url ? q.url : document.location.href)
};
var newButton = createReplacementButton('Post to your Indieweb Site', function () { openNoteUI('favourite', properties); });
});
}
// Public
return {
init: function () {
bindTwitterReplies();
setTimeout(bindTwitterReplies, 5000); /**/
bindTwitterRetweets();
setTimeout(bindTwitterRetweets, 5000); /**/
bindTwitterFavourites();
setTimeout(bindTwitterFavourites, 5000); /**/
bindAppDotNetReplies();
bindTwitterShareButtons();
bindFacebookLikeButtons();
bindGooglePlusOneButtons();
}
};
}());
$(document).ready(function () {
IndieWebReplyModule.init();
});