forked from loadletter/mangaupdates-urlfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mangaupdates_urlfix.user.js
216 lines (205 loc) · 9.54 KB
/
mangaupdates_urlfix.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
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
// ==UserScript==
// @name Mangaupdates Groups Fix
// @namespace Mangaupdates Groups Fix (https://github.com/loadletter/mangaupdates-urlfix)
// @description Makes clickable links to scanlators websites
// @match *://www.mangaupdates.com/groups.html*
// @match *://www.mangaupdates.com/mylist.html*
// @match *://www.mangaupdates.com/releases.html*
// @version 1.10
// @downloadURL https://github.com/loadletter/mangaupdates-urlfix/raw/master/mangaupdates_urlfix.user.js
// @updateURL https://github.com/loadletter/mangaupdates-urlfix/raw/master/mangaupdates_urlfix.user.js
// @grant none
// February 2019 fixes by https://github.com/anon-programmer
// February 2019 UI fixes by https://github.com/Hyacia
// ==/UserScript==
if (window.location.pathname === "/mylist.html") {
set_lastgroup();
} else if (window.location.pathname === "/releases.html") {
red_lastgroup();
} else if ('id' in get_query_params()) {
fix_irc();
fix_url();
update_groups();
} else {
set_redirection();
}
function update_groups() {
var urlfix_groups = document.createElement('script');
var urlfix_groupshard = parseInt(document.URL.replace(/^.+id=/,'').replace('#', ''), 10) % 20;
if (urlfix_groupshard !== urlfix_groupshard) {
return;
}
urlfix_groups.type = "text/javascript";
urlfix_groups.src = "//loadletter.github.io/mangaupdates-urlfix/src/groups/" + urlfix_groupshard + ".js";
urlfix_groups.onreadystatechange = fix_url;
urlfix_groups.onload = fix_url;
urlfix_groups.onerror = fix_url; /* Why not */
(document.body || document.head || document.documentElement).appendChild(urlfix_groups);
}
function fix_url() {
var oscript = document.createElement('script');
oscript.appendChild(document.createTextNode('('+ insertScript +')();'));
(document.body || document.head || document.documentElement).appendChild(oscript);
}
function fix_irc() {
var list = document.getElementsByClassName("text");
var irc='';
for (var i=0; i<list.length; i++) {
if(list[i].innerHTML == "<u>IRC</u>") {
list[i].id = "fixed_irc_url";
if ((irc=list[i].nextElementSibling.innerText) != "No IRC") {
var a = irc.replace(/^.+@/,'');
var b = irc.replace('#','').replace(/@.*/,'');
list[++i].innerHTML='<a href="irc://'+a+'/'+b+'"><u>'+a+'/'+b+'</u></a>';
}
break;
}
}
}
function get_query_params() {
var match, urlParams,
pl = /\+/g,
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
return urlParams;
}
function set_redirection() {
var prev_el = document.querySelector('a[href="https://www.mangaupdates.com/groups.html?active=false"]');
if (!prev_el) {
return;
}
var toggle_el = document.createElement('a');
var redir_key = 'loadletter.urlfix.settings.redirect';
var toggle_text = function () {
toggle_el.innerHTML = (typeof(localStorage) !== "undefined" && localStorage.getItem(redir_key)) ? '<u>Do not redirect to website</u>' : '<u>Redirect to group website</u>';
};
toggle_text();
toggle_el.title = "Toggle redirection of mangaupdates group entries to their respective website";
toggle_el.onclick = function () {
if (typeof(localStorage) === "undefined") {
alert("Couldn't save setting, your browser does not support HTML5 localStorage");
return;
}
if (localStorage.getItem(redir_key)) {
localStorage.removeItem(redir_key);
} else {
localStorage.setItem(redir_key, "true");
}
toggle_text();
};
var parent_el = prev_el.parentElement;
parent_el.innerHTML = parent_el.innerHTML.replace(/\s+$/, '') + ' ';
parent_el.appendChild(toggle_el);
}
function set_lastgroup () {
var prev_el = document.querySelector('div.low_col1 > a');
if (!prev_el) {
return;
}
var text_el = document.createTextNode(']\u00a0[');
var toggle_el = document.createElement('a');
var redir_key = 'loadletter.urlfix.settings.lastgroup';
var toggle_text = function () {
var toggle_hrefs = function (toggled) {
var latest_chapter = document.querySelectorAll('#list_table span.newlist > a');
for (var i=0; i<latest_chapter.length; i++) {
var hr = latest_chapter[i].href.split('#')[0];
latest_chapter[i].href = toggled ? (hr + '#showLastScanlator') : hr;
}
};
if (typeof(localStorage) !== "undefined" && localStorage.getItem(redir_key)) {
toggle_hrefs(true);
toggle_el.innerHTML = '<u>Show all scanlations</u>';
} else {
toggle_hrefs(false);
toggle_el.innerHTML = '<u>Jump to latest scanlation</u>';
}
};
toggle_text();
toggle_el.title = "Toggles redirection to the scanlator of the last chapter when clicking the link next to the series name";
toggle_el.onclick = function () {
if (typeof(localStorage) === "undefined") {
alert("Couldn't save setting, your browser does not support HTML5 localStorage");
return;
}
if (localStorage.getItem(redir_key)) {
localStorage.removeItem(redir_key);
} else {
localStorage.setItem(redir_key, "true");
}
toggle_text();
};
prev_el.parentNode.insertBefore(text_el, prev_el.nextSibling);
text_el.parentNode.insertBefore(toggle_el, text_el.nextSibling);
}
function red_lastgroup () {
if (!('search' in get_query_params() && window.location.hash === '#showLastScanlator')) {
return;
}
var latest_group = document.querySelector('#main_content a[title="Group Info"][href*="groups.html"]');
if (latest_group) {
window.location.href = latest_group.href;
}
}
/* all the stuff related to the website thing has been moved here,
* since the groups variable can't be accessed from the usescript */
function insertScript() {
window.urlfix_groupID = parseInt(document.URL.replace(/^.+id=/,'').replace('#', ''), 10);
if (window.urlfix_groupID !== window.urlfix_groupID) {
return;
}
var urlfix_local;
var urlfix_local_name = "loadletter.urlfix.groups." + (window.urlfix_groupID % 20);
if(typeof(window.urlfix_grouplist) !== "undefined") {
window.urlfix_groupSite = window.urlfix_grouplist[String(window.urlfix_groupID)];
if(typeof(localStorage) !== "undefined") {
localStorage.setItem(urlfix_local_name, JSON.stringify(window.urlfix_grouplist));
if (window.urlfix_groupSite !== undefined && localStorage.getItem('loadletter.urlfix.settings.redirect') && /^https?:\/\//.test(window.urlfix_groupSite)) {
window.location.href = window.urlfix_groupSite;
}
}
} else {
if(typeof(localStorage) !== "undefined" && (urlfix_local = localStorage.getItem(urlfix_local_name))) {
window.urlfix_groupSite = JSON.parse(urlfix_local)[window.urlfix_groupID];
if (window.urlfix_groupSite !== undefined && localStorage.getItem('loadletter.urlfix.settings.redirect') && /^https?:\/\//.test(window.urlfix_groupSite)) {
window.location.href = window.urlfix_groupSite;
}
} else {
window.urlfix_groupSite = undefined;
}
}
window.urlfix_openSuggBox = function() {
if(typeof(urlfix_grouplist_shard) === "undefined")
alert("Could not fetch latest info, the website might be outdated, consider refreshing the page before submitting a new entry");
var suggboxurl = "http://mufix.herokuapp.com/form?group=" + urlfix_groupID;
if(urlfix_groupSite !== "undefined")
suggboxurl += "&update=yes";
window.open(suggboxurl, '', 'scrollbars=no,resizable=yes, width=700,height=200,status=no,location=no,toolbar=no');
};
var urlfix_site_fixed = document.getElementById('fixed_group_suggestion');
var urlfix_site = urlfix_site_fixed || document.createElement('div');
urlfix_site.setAttribute("class", "p-1 col-6 text");
urlfix_site.id = "fixed_group_suggestion";
urlfix_site.innerHTML = '<u>Site</u><a href="#" onclick="urlfix_openSuggBox();"> (Suggest an update)</a>';
var urlfix_site_par = document.querySelector('div.general_table:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(5)');
urlfix_site_par.parentNode.insertBefore(urlfix_site, urlfix_site_par);
if(!urlfix_site_fixed) {
var urlfix_irc_par = document.getElementById("fixed_irc_url").parentNode;
urlfix_irc_par.parentNode.insertAfter(urlfix_site, urlfix_irc_par.nextSibling);
}
var site_link_fixed = document.getElementById('fixed_site_link');
var site_link = site_link_fixed || document.createElement('div');
site_link.setAttribute("class", "p-1 col-6 text");
site_link.id = "fixed_site_link";
site_link.innerHTML = (urlfix_groupSite === undefined ? '<i>No Info</i>' : ('<a target="_blank" alt="" href="' + urlfix_groupSite + '"><u>' + urlfix_groupSite + '</u></a>'));
var site_link_par = document.querySelector('#fixed_group_suggestion');
site_link_par.parentNode.insertBefore(site_link, urlfix_site);
if(!site_link_fixed) {
var site_link_par = document.getElementById("fixed_site_link");
site_link_par.parentNode.insertBefore(site_link, urlfix_site_par.nextSibling);
}
}