forked from privly/privly-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivly.js
254 lines (214 loc) · 9.04 KB
/
privly.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*******************************************************************************
Open Source Initiative OSI - The MIT License (MIT):Licensing
[OSI Approved License]
The MIT License (MIT)
Copyright (c) Sean McGregor
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*******************************************************************************/
var privly = {
//Matches:
// http://
// https://
// priv.ly/textAndNumbers/any/number/of/times
//
//also matches localhost:3000
privlyReferencesRegex: /\b(https?:\/\/){0,1}(priv\.ly|localhost:3000)(\/posts)(\/\w*){1,}\b/gi,
// Takes a domain with an optional http(s) in front and returns a fully formed domain name
makeHref: function(domain)
{
var hasHTTPRegex = /^((https?)\:\/\/)/i
if(!hasHTTPRegex.test(domain))
domain = "http://" + domain;
return domain;
},
//Make plain text links into anchor elements
createLinks: function()
{
/*************************************************************************
Inspired by Linkify script:
http://downloads.mozdev.org/greasemonkey/linkify.user.js
Originally written by Anthony Lieuallen of http://arantius.com/
Licensed for unlimited modification and redistribution as long as
this notice is kept intact.
**************************************************************************/
var excludeParents = ["a", "applet", "button", "code", "form",
"input", "option", "script", "select", "meta",
"style", "textarea", "title", "div","span"];
var excludedParentsString = excludeParents.join(" or parent::");
var xpathExpression = ".//text()[not(parent:: " + excludedParentsString +")]";
textNodes = document.evaluate(xpathExpression, document.body, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for(var i=0; i < textNodes.snapshotLength; i++){
item = textNodes.snapshotItem(i);
var itemText = item.nodeValue;
privly.privlyReferencesRegex.lastIndex = 0;
if(privly.privlyReferencesRegex.test(itemText)){
var span = document.createElement("span");
var lastLastIndex = 0;
privly.privlyReferencesRegex.lastIndex = 0;
for(var results = null; results = privly.privlyReferencesRegex.exec(itemText); ){
var href = results[0];
span.appendChild(document.createTextNode(itemText.substring(lastLastIndex, results.index)));
var text = (href.indexOf(" ")==0)?href.substring(1):href;
var href = privly.makeHref(text);
var a = document.createElement("a");
a.setAttribute("href", href);
a.appendChild(document.createTextNode(text.substring(0,4).toLowerCase()+text.substring(4)));
if(href.indexOf(" ")==0)
span.appendChild(document.createTextNode(" "));
span.appendChild(a);
lastLastIndex = privly.privlyReferencesRegex.lastIndex;
}
span.appendChild(document.createTextNode(itemText.substring(lastLastIndex)));
item.parentNode.replaceChild(span, item);
}
}
},
//Kill default link behaviour on Privly Links
makePassive: function(anchor)
{
//Preventing the default link behavior
anchor.addEventListener("mousedown", function(e){
e.cancelBubble = true;
e.stopPropagation();
e.preventDefault();
privly.replaceLink(anchor);
},
true);
},
//Checks link attributes and text for privly links without the proper href attribute.
//Twitter and other hosts change links so they can collect click events.
correctIndirection: function()
{
var anchors = document.links;
var i = anchors.length;
while (i--){
var a = anchors[i];
if(a.href && (a.href.indexOf("priv.ly/posts/") == -1 || a.href.indexOf("priv.ly/posts/") > 9))
{
if (privly.privlyReferencesRegex.test(a.innerHTML)) {
// If the href is not present or is on a different domain
privly.privlyReferencesRegex.lastIndex = 0;
var results = privly.privlyReferencesRegex.exec(a.innerHTML);
var newHref = privly.makeHref(results[0]);
a.setAttribute("href", newHref);
}
}
}
},
nextAvailableFrameID: 0,
// Replace an anchor element with its referenced content.
replaceLink: function(object)
{
var iFrame = document.createElement('iframe');
iFrame.setAttribute("frameborder","0");
iFrame.setAttribute("vspace","0");
iFrame.setAttribute("hspace","0");
iFrame.setAttribute("name","privlyiframe");
iFrame.setAttribute("width","100%");
iFrame.setAttribute("marginwidth","0");
iFrame.setAttribute("marginheight","0");
iFrame.setAttribute("height","1px");
iFrame.setAttribute("src",object.href + ".iframe?frame_id=" + privly.nextAvailableFrameID);
iFrame.setAttribute("id","ifrm"+privly.nextAvailableFrameID);
iFrame.setAttribute("frameborder","0");
privly.nextAvailableFrameID++;
iFrame.setAttribute("style","width: 100%; height: 32px; overflow: hidden;");
iFrame.setAttribute("scrolling","no");
iFrame.setAttribute("overflow","hidden");
object.parentNode.replaceChild(iFrame, object);
},
//Replace all Privly links with their iframe
replaceLinks: function(){
var anchors = document.links;
var i = anchors.length;
while (i--){
var a = anchors[i];
privly.privlyReferencesRegex.lastIndex = 0;
if(a.href && privly.privlyReferencesRegex.test(a.href))
{
var exclude = a.getAttribute("privly");
if(exclude != "exclude")
{
if(privly.active)
{
privly.replaceLink(a);
}
else
{
privly.makePassive(a);
}
}
}
}
},
//resize the iframe using a posted message
resizeIframe: function(message){
if(message.origin !== "https://priv.ly" && message.origin !== "http://localhost:3000")
return;
var data = message.data.split(",");
var iframe = document.getElementById("ifrm"+data[0]);
iframe.style.height = data[1]+'px';
},
//prevents DOMNodeInserted from sending hundreds of extension runs
runPending: false,
//prep the page and replace the links if it is in active mode
run: function(){
//create and correct the links pointing
//to Privly content
privly.createLinks();
privly.correctIndirection();
//replace all available links on load, if in active mode,
//otherwise replace all links default behavior
privly.replaceLinks();
},
//runs privly once then registers the update listener
//for dynamic pages
listeners: function(){
//don't recursively replace links
if(document.URL.indexOf('priv.ly') != -1 || document.URL.indexOf('localhost:3000') != -1)
return;
//The content's iframe will post a message to the hosting document. This listener sets the height
//of the iframe according to the messaged height
window.addEventListener("message", privly.resizeIframe, false, true);
privly.runPending=true;
setTimeout(
function(){
privly.runPending=false;
privly.run();
},
100);
//Everytime the page is updated via javascript, we have to check
//for new Privly content. This might not be supported on other platforms
document.addEventListener("DOMNodeInserted", function(event) {
//we check the page a maximum of two times a second
if(privly.runPending )
return;
privly.runPending=true;
setTimeout(
function(){
privly.runPending=false;
privly.run();
},
500);
});
},
//indicates whether the extension shoud immediatly replace all Privly
//links it encounters
active: true
};
privly.listeners();