forked from nmaier/repagination
-
Notifications
You must be signed in to change notification settings - Fork 5
/
content-script.js
436 lines (399 loc) · 14 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
{
console.log("Framescript loaded!");
let port = browser.runtime.connect();
var focusElement = null;
let _ = function () {
let args = Array.from(arguments).map(e => e.toString());
return browser.i18n.getMessage(args[0], args.slice(1));
}
let equalLinks = (left, right) =>
left.pathname === right.pathname &&
left.search === right.search &&
left.host === right.host &&
left.protocol === right.protocol;
let getFirstSnapshot = (doc, node, query) =>
doc.evaluate(query, node, null, 7, null).snapshotItem(0);
let createPageRequest = (srcurl, allowScripts, loadFun) => {
let errorCount = 0;
function sendRequest() {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (xhr.status == 200) {
console.info("XHR loaded, going to process");
try {
loadFun(xhr.responseXML);
}
catch (ex) {
console.error("failed to invoke callback on XHR", ex);
}
} else if (++errorCount <= 5) {
console.info("XHR err'ed out, retrying");
setTimeout(function(){ sendRequest(); }, 500 * (2**errorCount));
} else {
console.error("XHR err'ed out, giving up");
}
}
xhr.open('GET', srcurl);
xhr.responseType = "document";
xhr.send();
}
sendRequest();
};
let Repaginator = function Repaginator(count, allowScripts, yielding) {
this.pageLimit = count || 0;
this.allowScripts = allowScripts;
this.yielding = yielding;
this.init();
};
Repaginator.prototype = {
slideshow: false,
pageLimit: 0,
seconds: 0,
pageCount: 0,
attemptToIncrement: true,
init: function R_init() {
// find anchor
(function findInitialAnchor() {
for (let parent = focusElement; parent; parent = parent.parentNode) {
if (parent.localName == "a") {
focusElement = parent;
return;
}
}
throw new Error("No focus element");
})();
},
buildQuery: function R_buildQuery(el) {
function escapeXStr(mystr) {
return mystr.replace(/'/g, "\\'");
}
this.query = "";
(function buildQuery() {
// Homestuck hack
if(el.href && el.href.includes("mspaintadventures.com")) {
this.query = "//center[position() = 1]/table[position() = 1]/tbody[position() = 1]/tr[position() = 2]/td[position() = 1]/table[position() = 1]/tbody[position() = 1]/tr[position() = 1]/td[position() = 1]/table[position() = 1]/tbody[position() = 1]/tr[position() = 2]/td[position() = 1]/center[position() = 1]/table[position() = 1]/tbody[position() = 1]/tr[position() = 6]/td[position() = 1]/table[position() = 1]/tbody[position() = 1]/tr[position() = 1]/td[position() = 1]/font[position() = 1]/a[position() = 1]";
this.numberToken = /(\[@href='.*)(\d+)(.*?'\])/;
return;
}
// See if the anchor has an ID
// Note: cannot use the id() xpath function here, as there might
// be duplicate ids
if (el.id) {
this.query = "//a[@id='" + escapeXStr(el.id) + "']";
this.numberToken = /(\[@id='.*?)(\d+)(.*?'\])/;
return;
}
// See if the document has a link rel="..." pointing to the same place
let linkEl = getFirstSnapshot(el.ownerDocument, el.ownerDocument,
"//head//link[@href='" +
escapeXStr(el.href) + "']");
if (linkEl) {
let rel = linkEl.getAttribute("rel") || "";
if (rel.trim()) {
this.query = "/html/head//link[@rel='" + escapeXStr(rel) + "']";
// no point in checking for numbers
this.attemptToIncrement = false;
console.log("using link[@rel]");
return;
}
}
// Find an id in the ancestor chain, or alternatively a class
// that we may operate on
(function findPathPrefix() {
let pieces = [];
for (let pn = el.parentNode; pn; pn = pn.parentNode) {
if (pn.localName == "body") {
break;
}
if (pn.id) {
console.log("got id: " + pn.id);
pieces.unshift("//" + pn.localName + "[@id='" +
escapeXStr(pn.id) + "']");
break; // one id is enough
}
if (pn.className) {
console.log("got class: " + pn.className);
pieces.unshift("//" + pn.localName + "[@class='" +
escapeXStr(pn.className) + "']");
break;
}
}
this.query = pieces.join("");
console.log("findPathPrefix result: " + this.query);
}).call(this);
// find the anchor
(function findAnchor() {
let text = el.textContent;
// See if there is rel=next or rel=prev
let rel = (el.getAttribute("rel") || "").trim();
if (rel && (rel.includes("next") || rel.includes("prev"))) {
this.query += "//a[@rel='" + escapeXStr(rel) + "']";
// no point in checking for numbers
this.attemptToIncrement = false;
console.log("using a[@rel]");
return;
}
// Try the node text
if (text.trim()) {
this.query += "//a[.='" + escapeXStr(text) + "']";
this.numberToken = /(a\[.='.*?)(\d+)(.*?\])/;
console.log("using text");
return;
}
// See if it has a descendant with a @src we may use
let srcEl = getFirstSnapshot(el.ownerDocument, el, "descendant::*[@src]");
if (srcEl) {
let src = srcEl.getAttribute("src") || "";
if (src.trim()) {
this.query += "//" + srcEl.localName + "[@src='" + escapeXStr(src) +
"']/ancestor::a";
this.numberToken = /(\[@src='.*?)(\d+)(.*?'\])/;
console.log("using @src");
return;
}
}
// See if there is a descendant with a @value we may use
srcEl = getFirstSnapshot(el.ownerDocument, el, "descendant::*[@value]");
if (srcEl) {
let val = srcEl.getAttribute("value") || "";
if (val.trim()) {
this.query += "//" + srcEl.localName + "[@value='" +
escapeXStr(val) + "']/ancestor::a";
this.numberToken = /(\[@value='.*?)(\d+)(.*?'\])/;
console.log("using @value");
return;
}
}
// See if there is a class we may use
if (el.className) {
this.query += "//a[@class='" + escapeXStr(el.className) + "']";
this.numberToken = /(\[@class='.*?)(\d+)(.*?'\])/;
console.log("using a[@class]");
return;
}
// See if there is a descendant with an id we may use
srcEl = getFirstSnapshot(el.ownerDocument, el, "descendant::*[@id]");
if (srcEl) {
let val = srcEl.getAttribute("id") || "";
if (val.trim()) {
this.query += "//" + srcEl.localName + "[@id='" +
escapeXStr(val) + "']/ancestor::a";
this.numberToken = /(\[@value='.*?)(\d+)(.*?'\])/;
console.log("using descendant class");
return;
}
}
throw new Error("No anchor expression found!");
}).call(this);
}).call(this);
// We're after the last result
this.query = "(" + this.query + ")[last()]";
console.info("query: " + this.query);
},
setTitle: function R_setTitle() {
if (this.pageLimit) {
document.title =
_("repagination_limited", this.pageCount, this.pageLimit);
}
else if (this.pageCount > 0) {
document.title = _("repagination_unlimited", this.pageCount);
}
else {
document.title = _("repagination_running");
}
},
finished: function R_finished() {
port.postMessage({msg: "finished"});
stop();
// restore title
if("_title" in this) {
document.title = this._title;
delete this._title;
}
},
repaginate: function R_repaginate() {
this._title = document.title;
this.setTitle();
try {
let node = document.evaluate(
this.query, document, null, 9, null).singleNodeValue;
if (!node) {
throw new Error("no node");
}
document.body.setAttribute("repagination", "true");
createPageRequest(node.href, this.allowScripts, frame => {
this.loadNext(node.href, frame, 0);
});
} catch (ex) {
this.finished();
console.error("repaginate failed", ex);
}
},
incrementQuery: function R_incrementQuery() {
return this.query.replace(this.numberToken, function(g, pre, num, post) {
return pre + (parseInt(num, 10) + 1) + post;
});
},
loadNext: function R_loadNext(src, element, delay) {
if (delay > 0) {
console.log("delaying (sto) " + delay);
setTimeout(() => this.loadNext(src, element, 0), delay);
return;
}
try {
this._loadNext_gen.bind(this, src, element)();
} catch (ex) {
console.error("failed to process loadNext (non-yielding)", ex);
this.finished();
}
return;
},
_loadNext_gen: function R__loadNext_gen(src, element) {
let ownerDoc = document;
try {
if (!ownerDoc.body.hasAttribute("repagination")) {
throw new Error("not running");
}
var doc = element;
this.pageCount++;
// Remove scripts from frame
// The scripts should already be present in the parent (first page)
// Duplicate scripts would cause more havoc (performance-wise) than
// behaviour failures due to missing scripts
// Note: This is NOT a security mechanism, but a performance thing.
doc.querySelectorAll("script").forEach(s => s.parentNode.removeChild(s));
doc.querySelectorAll("style").forEach(s => s.parentNode.removeChild(s));
// Do the dirty deed
if (this.slideshow) {
console.info("replacing content (slideshow)");
// this should be safe since innerHTML returns serialized HTML
ownerDoc.body.innerHTML = doc.body.innerHTML;
ownerDoc.body.setAttribute("repagination", "true");
}
else {
console.info("inserting content (normal)");
// Remove non-same-origin iframes, such as ad iframes
// Otherwise we might create a shitload of (nearly) identical frames
// which might even kill the browser
if (!this.pageLimit || this.pageLimit > 10) {
console.info("removing non-same-origin iframes to avoid dupes");
let host = ownerDoc.defaultView.location.hostName;
doc.querySelectorAll("iframe").forEach(function(f) {
var url = new URL(f.src, ownerDoc.defaultView.location.href);
if (url.hostname != host) {
f.parentNode.removeChild(f);
}
});
}
for (let c = doc.body.firstChild; c; c = c.nextSibling) {
ownerDoc.body.appendChild(ownerDoc.importNode(c, true));
}
}
if (this.pageLimit && this.pageCount >= this.pageLimit) {
throw new Error("done");
}
let savedQuery;
if (this.attemptToIncrement) {
console.log("attempting to increment query");
let nq = this.incrementQuery();
if (nq == this.query) {
console.log("query did not increment");
this.attemptToIncrement = false;
}
else {
console.log("query did increment");
savedQuery = this.query;
this.query = nq;
}
}
let node = doc.evaluate(this.query, doc, null, 9, null).singleNodeValue;
let loc = src || null;
if (this.attemptToIncrement && (!node || node.href == loc)) {
console.log("no result after incrementing; restoring");
console.log("inc:" + this.query + " orig:" + savedQuery);
this.query = savedQuery;
node = doc.evaluate(this.query, doc, null, 9, null).singleNodeValue;
this.attemptToIncrement = false;
}
if (!node) {
throw new Error("no next node found for query: " + this.query);
}
let nexturl = node.href.toString();
if (loc && loc == nexturl) {
throw new Error("location did not change for query" + this.query);
}
if (equalLinks(node,window.location)) {
throw new Error("loop back to first item");
}
this.setTitle();
console.info("next please: " + nexturl);
createPageRequest(nexturl, this.allowScripts, frame => {
if (this.slideshow && this.seconds) {
console.info("slideshow; delay: " + this.seconds * 1000);
this.loadNext(nexturl, frame, this.seconds * 1000);
}
else {
console.info("regular; no-delay");
this.loadNext(nexturl, frame, 0);
}
});
}
catch (ex) {
console.log(ex);
console.info("loadNext complete");
this.finished();
}
}
};
Object.seal(Repaginator.prototype);
let Slideshow = function Slideshow(seconds, allowScripts, yielding) {
this.seconds = seconds || 0;
this.slideshow = true;
this.allowScripts = allowScripts;
this.yielding = yielding;
this.init();
};
Slideshow.prototype = Repaginator.prototype;
let repaginate = (target, num, slideshow, allowScripts, yielding) => {
try {
let Ctor = slideshow ? Slideshow : Repaginator;
let rep;
focusElement = browser.menus.getTargetElement(target);
rep = new Ctor(num, allowScripts, yielding);
rep.buildQuery(focusElement);
rep.repaginate();
} catch (ex) {
console.error("Failed to run repaginate", ex);
}
};
let stop = () => {
try {
let body = document.body;
if (body) {
body.removeAttribute("repagination");
}
} catch (ex) {
console.error("failed to stop repagination", ex);
}
};
port.onMessage.addListener(msg => {
switch (msg.msg) {
case "normal": repaginate(msg.target, msg.num, msg.slideshow, msg.allowScripts, msg.yielding); break;
case "stop" : stop(); break;
}
});
// https://bugzilla.mozilla.org/show_bug.cgi?id=1370368
window.addEventListener('pagehide', function(event) {
stop();
try {
port.disconnect();
} catch (ex) {
console.log(ex)
}
});
}
/* vim: set et ts=2 sw=2 : */