-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwop-unpaged-recaps.user.js
More file actions
161 lines (140 loc) · 5.89 KB
/
twop-unpaged-recaps.user.js
File metadata and controls
161 lines (140 loc) · 5.89 KB
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
// ==UserScript==
// @name TwoP unpaged recaps
// @namespace http://blairmitchelmore.com/greasemonkey/twop
// @description Retrieves the other pages of a long recap and fills in the blanks on the existing page
// @include http://televisionwithoutpity.com/*
// @include http://*.televisionwithoutpity.com/*
// @description Version 1.1.6
// ==/UserScript==
var getUrlParam = function(url, param) {
return Number(url.substr(url.indexOf("?") + 1).split("&").filter(function(element) {
try {
return element.split("=")[0] == param;
} catch(e) { }
return false;
}).map(function(element) {
return element.split("=")[1];
})[0]) || 0;
};
var addGlobalStyle = function(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
return style;
};
var lighten = function(colour) {
var match, rgb = [], regex = /[0-9]+/gm, colourString = colour.toString();
while (match = regex.exec(colourString)) {
var calc = (parseFloat(match[0]) || 0) * 1.1;
rgb.push(Math.max(0, Math.min(255, calc)));
}
return "rgb(" + rgb.join(",") + ")";
};
var currentPage = getUrlParam(location.search, "page");
var selectedPage = currentPage == 0 ? (currentPage = 1, 0) : currentPage;
var recap = document.evaluate("//div[@class='body_recap']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var pageListing = document.evaluate("//*[contains(@class,'article_pages')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (recap) {
var recap_contents, recap_wrapper = document.createElement("div");
var addHighlightKiller = function(node) {
node.className = "highlighted";
var singleClick = function() {
this.className = this.className == "highlighted" ? "" : "highlighted";
};
node.addEventListener("click", singleClick, false);
node.addEventListener("dblclick", function() {
node.removeEventListener("click", singleClick, false);
node.removeEventListener("dblclick", arguments.callee, false);
}, false);
};
// move the page listing outside of the recap content
pageListing = recap.parentNode.insertBefore(pageListing, recap);
// find the page links
var links = document.evaluate("//ul[@class='pages' and position() = 1]//a[not(contains(.,'Prev')) and not(contains(.,'Next'))]", pageListing, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
// move this page into its own div
recap_wrapper.innerHTML = recap.innerHTML;
recap.innerHTML = "";
recap.appendChild(recap_wrapper);
// set the id of the current page's div
recap_wrapper.id = "Page_" + currentPage;
if (selectedPage == currentPage) {
addHighlightKiller(recap_wrapper);
}
for (var i = 0; i < links.snapshotLength; ++i) {
var url = links.snapshotItem(i).href;
var pageNumber = getUrlParam(url, "page");
if (pageNumber != currentPage) {
var event = (function(num) {
var wrapper = document.createElement("div");
wrapper.id = "Page_" + num;
var message = document.createElement("p");
message.appendChild(document.createTextNode("Loading content from page " + num + "."));
wrapper.appendChild(message);
wrapper.className = "loading";
// Find the proper place to put the content
if (num < currentPage) {
for (var j = num + 1; j <= currentPage; ++j) {
var sibling = document.getElementById("Page_" + j);
if (sibling) {
sibling.parentNode.insertBefore(wrapper, sibling);
break;
}
}
} else {
for (var j = num - 1; j >= currentPage; --j) {
var sibling = document.getElementById("Page_" + j);
if (sibling) {
sibling.parentNode.insertBefore(wrapper, sibling.nextSibling);
break;
}
}
}
return function(response) {
var html = response.responseText;
var start = html.indexOf("<div class=\"body_recap\">");
var end = html.indexOf("</div", start);
var html_frag = html.substr(start+25, end-start);
var frag = document.createElement("div");
frag.innerHTML = html_frag;
wrapper.className = "";
if (num == selectedPage)
addHighlightKiller(wrapper);
wrapper.innerHTML = frag.innerHTML;
var links = document.evaluate("//*[contains(@class,'article_pages')]", wrapper, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (links)
wrapper.removeChild(links);
};
})(pageNumber);
// get the content for the other page
GM_xmlhttpRequest({
method: 'GET',
url: url,
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'text/html',
},
onload: event
});
}
}
if (pageListing)
pageListing.parentNode.removeChild(pageListing);
var subheader = document.evaluate("//div[contains(@class,'report_card')]//span[contains(.,'Episode Report Card')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var style = document.defaultView.getComputedStyle(subheader, null);
var backgroundColor = style.getPropertyValue("background-color");
addGlobalStyle("div[id^=Page_][class=loading] {\
background-color:" + backgroundColor + ";\
padding:5px;\
clear:both;\
margin:5px;\
}");
addGlobalStyle("div[id^=Page_][class=highlighted] {\
background-color:" + lighten(backgroundColor) + ";\
border:1px solid " + backgroundColor + ";\
padding:5px;\
}");
}