-
Notifications
You must be signed in to change notification settings - Fork 2
/
kitsu-mangadex.user.js
125 lines (114 loc) · 4.04 KB
/
kitsu-mangadex.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
// ==UserScript==
// @name Kitsu MangaDex Links
// @namespace https://greasyfork.org/users/649
// @version 3.0.8
// @description Adds MangaDex links to Kitsu manga pages
// @author Adrien Pyke
// @match *://kitsu.io/*
// @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
(() => {
'use strict';
const SCRIPT_NAME = 'Kitsu MangaDex Links';
const REGEX = /^https?:\/\/kitsu\.io\/manga\/[^/]+\/?(?:\?.*)?$/u;
const Util = {
log(...args) {
args.unshift(`%c${SCRIPT_NAME}:`, 'font-weight: bold;color: #233c7b;');
console.log(...args);
},
q(query, context = document) {
return context.querySelector(query);
},
qq(query, context = document) {
return Array.from(context.querySelectorAll(query));
},
encodeQuery(query) {
return encodeURIComponent(query.trim().replace(/\s+/gu, ' '));
}
};
const App = {
cache: {},
getMangaDexPage(title, cb) {
const self = this;
if (self.cache[title]) {
Util.log('Loading cached info');
cb(self.cache[title]);
} else {
const url = `https://mangadex.org/quick_search/${Util.encodeQuery(
title
)}`;
Util.log('Searching MangaDex:', url);
GM_xmlhttpRequest({
method: 'GET',
url,
onload(response) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = response.responseText;
const manga = Util.q('#search_manga .manga_title', tempDiv);
if (manga) {
manga.href = `https://mangadex.org${manga.getAttribute('href')}`;
Util.log('Link:', manga.href);
self.cache[title] = manga.href;
cb(manga.href);
} else {
Util.log('No results found');
self.cache[title] = null;
cb(null);
}
},
onerror() {
Util.log('Error searching MangaDex');
}
});
}
}
};
waitForUrl(REGEX, () => {
waitForElems({
sel: '.media-sidebar',
stop: true,
onmatch(node) {
const title = Util.q('.media--title h3');
const url = location.href;
App.getMangaDexPage(title.textContent, manga => {
const check = Util.q('.where-to-watch-widget');
if (!manga && check) check.remove();
if (location.href === url && manga) {
if (check) {
const updateLink = Util.q('#mangadex-link');
updateLink.href = manga;
} else {
const section = document.createElement('div');
section.className = 'where-to-watch-widget';
const header = document.createElement('span');
header.className = 'where-to-watch-header';
const headerText = document.createElement('span');
headerText.textContent = 'Read Online';
header.appendChild(headerText);
section.appendChild(header);
const listWrap = document.createElement('ul');
listWrap.className = 'nav';
const list = document.createElement('li');
listWrap.appendChild(list);
section.appendChild(listWrap);
const link = document.createElement('a');
link.id = 'mangadex-link';
link.href = manga;
link.target = '_blank';
link.rel = 'noopener noreferrer';
link.setAttribute('aria-label', 'MangaDex');
link.className = 'hint--top hint--bounce hint--rounded';
const img = document.createElement('img');
img.src = 'https://mangadex.org/images/misc/navbar.svg';
img.style.verticalAlign = 'text-bottom';
link.appendChild(img);
list.appendChild(link);
node.appendChild(section);
}
}
});
}
});
});
})();