This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkSort.js
282 lines (254 loc) · 11.1 KB
/
linkSort.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
// Storage initial config
chrome.storage.local.get('globalConfig', function (items) {
if (items.globalConfig == undefined) {
// Initialise values
chrome.storage.local.set({ 'globalConfig': { 'active': true, 'quality': 'any', 'lang': 'any', 'subs': 'any' } }, function (itm) {
chrome.storage.local.get('spec', function (items) {
if (items.spec == undefined) {
items.spec = { 'serie': {}, 'peli': {} };
chrome.storage.local.set({ 'spec': items.spec }, function (itemsSpec) {
setInterval(checkLinks, 1000);
});
} else {
setInterval(checkLinks, 1000);
}
});
});
} else {
// Start checkLinks loop
chrome.storage.local.get('spec', function (items) {
if (items.spec == undefined) {
items.spec = { 'serie': {}, 'peli': {} };
chrome.storage.local.set({ 'spec': items.spec }, function (itemsSpec) {
setInterval(checkLinks, 1000);
});
} else {
setInterval(checkLinks, 1000);
}
});
}
});
// Define Node creator function
function createElementFromHTML(htmlString) {
var div = document.createElement('div');
div.innerHTML = htmlString.trim();
// Change this to div.childNodes to support multiple top-level nodes
return div.firstChild;
}
// Map saved lang value to HTML src image name
function toLang(lang) {
switch (lang) {
case 'esp':
return 'spanish.png';
break;
case 'eng':
return 'english.png';
break;
case 'lat':
return 'latino.png';
break;
default:
return null;
break;
}
}
// Check if links are shown
function displayedLinks() {
var popupaportes = document.getElementsByClassName('popup-aportes');
return popupaportes.length != 0;
}
// Create sorted links section
function createSortedLinksSection(type) {
// Get links container
var linksContainer = document.getElementById(type);
// Create sortedHosts container
var sortedHostsContainer = document.createElement("div");
sortedHostsContainer.id = "pls-sorted-hosts-container-" + type;
sortedHostsContainer.classList.add("pls-sorted-hosts-container");
// Create and insert title + tooltip
var titleWrap = document.createElement("div"); // Create wrapper for title + tooltip
titleWrap.id = "pls-title-wrap-" + type;
titleWrap.classList.add("pls-title-wrap");
var h = document.createElement("H4"); // Create HTML elements
var p = document.createElement("p");
var tooltip = document.createElement("span");
h.id = 'pls-title-sorted-links-' + type; // Assign some IDs
h.classList.add("pls-title-sorted-links");
p.id = 'pls-title-info-tooltip-' + type;
p.classList.add("pls-title-info-tooltip");
tooltip.id = "pls-title-info-tooltip-text-" + type;
tooltip.classList.add("pls-title-info-tooltip-text");
titleWrap.appendChild(h); // Title + tooltip inside wrapper
titleWrap.appendChild(p);
var t = document.createTextNode("Sorted Hosts"); // Create inside-text nodes
var infoTooltip = document.createTextNode("");
var tooltipText = document.createTextNode("Megadede Link Sort Extension");
tooltip.appendChild(tooltipText); // Insert text into hidden span
p.appendChild(infoTooltip); // Insert tooltip text into tooltip
p.appendChild(tooltip); // Insert hidden span into tooltip
h.appendChild(t); // Insert Title text into title
sortedHostsContainer.appendChild(titleWrap);
// Create cotainer for links
var sortedLinksContainer = document.createElement("div"); // Where links will be
sortedLinksContainer.id = "pls-sorted-links-container-" + type;
sortedLinksContainer.classList.add("pls-sorted-links-container");
sortedHostsContainer.appendChild(sortedLinksContainer);
// Insert before first (default) title
var st = linksContainer.getElementsByTagName('h4') // Get titles
linksContainer.insertBefore(sortedHostsContainer, st[0]);
}
// Remove added title and links
function clearSortedLinksSection(type) {
// Remove whole DIV
if (displayedLinks()) {
let section = document.getElementById("pls-sorted-hosts-container-" + type);
section.parentNode.removeChild(section);
}
}
// Type must be 'online' or 'download' (Online/Download link list IDs)
function sortLinks(type) {
// Retrieve selected quality, lang and subs.
if (displayedLinks()) {
// Get peli/serie name
var urlSplit = window.location.pathname.split('/');
var urlType = urlSplit[urlSplit.length - 2];
var urlName = urlSplit[urlSplit.length - 1];
// Get spec JSON
chrome.storage.local.get('spec', function (items) {
// Use gloabl config
chrome.storage.local.get('globalConfig', function (it) {
// First time it wont exist
if (items.spec[urlType][urlName] != undefined && items.spec[urlType][urlName].enable) {
linkQuality = items.spec[urlType][urlName].config.quality;
linkLang = items.spec[urlType][urlName].config.lang;
linkSubs = items.spec[urlType][urlName].config.subs;
} else {
linkQuality = it.globalConfig.quality;
linkLang = it.globalConfig.lang;
linkSubs = it.globalConfig.subs;
}
// Get sorted links container
var linksContainer = document.getElementById(type);
var links = linksContainer.getElementsByClassName('aporte');
links = Array.from(links); // Convert HTMLCollection to JS Array
// Check for no links
if (links.length == 0) {
return;
}
// Sort by quality
if (linkQuality != 'any') {
for (var i = 0; i < links.length; i += 1) {
if (!links[i].className.includes(linkQuality)) {
// Remove from array
links.splice(i, 1);
i -= 1; // Counter-update index
}
}
}
// Sort by Lang and sub
for (var i = 0; i < links.length; i += 1) {
// Get flags
var langSubsFlags = links[i].getElementsByClassName('language')[0].getElementsByTagName('img');
// Check lang
if (linkLang != 'any') {
if (!langSubsFlags[0].src.includes(toLang(linkLang))) {
links.splice(i, 1);
i -= 1; // Counter-update index (we just altered the array length)
continue; // Link removed, don't care about subs
}
}
// Check subs
if (linkSubs != 'any') {
// Remove if no subs but some sub selected OR sub selected but not matching lang
if ((langSubsFlags.length == 1 && linkSubs != 'none') ||
(langSubsFlags.length > 1 && !langSubsFlags[1].src.includes(toLang(linkSubs)))) {
links.splice(i, 1);
i -= 1; // Counter-update index (we just altered the array length)
}
}
}
// Insert links
var sortedLinksContainer = document.getElementById("pls-sorted-links-container-" + type);
if (links.length > 0) {
for (var i = 0; i < links.length; i += 1) {
sortedLinksContainer.appendChild(createElementFromHTML(links[i].outerHTML));
}
} else {
let noLinksWarn = createElementFromHTML('<p> No links matching criteria </p>');
noLinksWarn.className = "alert alert-warning"; // Classes from Megadede's css
sortedLinksContainer.appendChild(noLinksWarn);
}
});
});
}
}
// Remove sorted links
function clearSortedLinks(type) {
chrome.storage.local.get('globalConfig', function (items) {
if (items.globalConfig.active == undefined || items.globalConfig.active) {
// Check if links loaded
if (displayedLinks()) {
// Check if already sorted
if (document.getElementById('pls-title-sorted-links-online') != null) { // Could test for either online or download
// Clear sorted links
let links = document.getElementById("pls-sorted-links-container-" + type);
while (links.firstChild) {
links.removeChild(links.firstChild);
}
}
}
}
});
}
// Check for links displayed and sort if active (periodically executed)
function checkLinks() {
// Check if links are displayed
if (displayedLinks()) {
// Check if sorting active
chrome.storage.local.get('globalConfig', function (items) {
if (items.globalConfig.active == undefined || items.globalConfig.active) {
// Check if already sorted
if (document.getElementById('pls-title-sorted-links-online') == null) { // Could test for either online or download
// Sort links
createSortedLinksSection('online');
createSortedLinksSection('download');
sortLinks('online');
sortLinks('download');
}
}
});
}
}
// Updates listener (vendor specific)
function onMsgFirefox(message) {
if (message.action != undefined) {
// Sorting criteria or active change
if (displayedLinks() && message.action.match(/^(quality|lang|subs|sync)$/)) {
// Clear sorted hosts
clearSortedLinks("online");
clearSortedLinks("download");
// Update sorted hosts
sortLinks("online");
sortLinks("download");
} else if (displayedLinks() && message.action == "disable") {
// Remove sorted hosts section
clearSortedLinksSection("online");
clearSortedLinksSection("download");
}
}
}
function onMsgChrome(request, sender, sendResponse) {
if (!sender.tab) {
onMsgFirefox(request);
}
}
// Vendor test
var isChrome = navigator.userAgent.indexOf("Chrome") != -1;
var isFirefox = navigator.userAgent.indexOf("Firefox") != -1;
if (isChrome) {
chrome.runtime.onMessage.addListener(onMsgChrome);
} else if (isFirefox) {
chrome.runtime.onMessage.addListener(onMsgFirefox);
} else {
console.error("SOMETHING IS VERY WRONG...");
}