-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMALStatus.js
More file actions
159 lines (137 loc) · 5.59 KB
/
MALStatus.js
File metadata and controls
159 lines (137 loc) · 5.59 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
// ==UserScript==
// @name MALStatus
// @version 1.1.4
// @description
// @author Woreec
// @grant GM_setValue
// @grant GM_getValue
// @namespace https://github.com/Woreec/MALStatus
// @match https://myanimelist.net/*
// @downloadURL https://raw.githubusercontent.com/Woreec/MALStatus/refs/heads/main/MALStatus.js
// @updateURL https://raw.githubusercontent.com/Woreec/MALStatus/refs/heads/main/MALStatus.js
// ==/UserScript==
// Get Status from page, and send to be StatusScript (addStatus)
function StatusScript() {
const LOCATION_HREF = location.href;
const URL_REGEX = /https:\/\/myanimelist\.net\/(anime|manga)\/([1-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)\/?.*/;
const URL_PHP_REGEX = /https:\/\/myanimelist\.net\/(anime|manga)\.php\?id\=([1-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)\/?.*/;
// Anime Seasonal
if (LOCATION_HREF.includes('https://myanimelist.net/anime/season')) {
let results = document.getElementsByClassName('link-title');
for (let i = 0; i < results.length && i < 72; i++) {
if (!document.getElementById('status' + i)) {
let url = results[i].href;
let urlDecoded = decodeURIComponent(url);
let id = url.split('/')[4];
let selector = 'a[href="' + urlDecoded + '"].link-title';
addStatus('anime', i, url, id, selector, false, true);
}
}
}
}
// Get get status and add to page
function addStatus(type, count, url, id, selector, parent = false, tile = false, producer = false) {
let styleId = "";
let styleIdEnd = "";
if (tile) {
// Determine text color based on status
let textColor = '';
if (CheckStatusAge(id)) {
let statusText = storedAnimeStatus[id][0];
if (statusText === "Currently Airing") {
textColor = 'color: #8e0000;';
} else if (statusText === "Finished Airing") {
textColor = 'color: green;';
}
}
styleId = '<h3 class="h3_anime_subtitle" id="status' + count + '" style="font-weight: bold; ' + textColor + '">';
styleIdEnd = '</h3>';
} else {
// Determine text color based on status
let textColor = '';
if (CheckStatusAge(id)) {
let statusText = storedAnimeStatus[id][0];
if (statusText === "Currently Airing") {
textColor = 'color: #8e0000;';
} else if (statusText === "Finished Airing") {
textColor = 'color: green;';
}
}
styleId = '<div style="font-weight:bold; ' + textColor + '" id="status' + count + '">';
styleIdEnd = '</div>';
}
if (type === 'anime') {
if (producer) {
document.getElementsByClassName('category')[count].style.visibility = 'hidden';
}
// Check if the status exists
if (CheckStatusAge(id)) {
document.querySelectorAll(selector).forEach(function (element) {
// Find the parent .title-text element
let titleTextElement = element.closest('.title-text');
if (titleTextElement) {
// Insert the HTML as a sibling above the h2 element
titleTextElement.insertAdjacentHTML('afterbegin', styleId + storedAnimeStatus[id][0] + styleIdEnd);
}
});
} else {
// If the status doesn't exist, get the English title
getStatus(type, url, id, selector, parent, styleId, styleIdEnd, count);
}
}
}
// Request Status from MAL and send to be stored (storeAnimeStatus)
function getStatus(type, url, id, selector, parent, styleId, styleIdEnd, count) {
// Create new request
let xhr = new XMLHttpRequest();
xhr.responseType = 'document';
// Set the callback
xhr.onload = function () {
if (xhr.readyState === xhr.DONE && xhr.status === 200 && xhr.responseXML !== null) {
// === Get the Status ===
let spaceitPadElements = xhr.responseXML.querySelectorAll('.spaceit_pad');
let statusText = '';
spaceitPadElements.forEach(function (element) {
let darkTextElement = element.querySelector('.dark_text');
if (darkTextElement && darkTextElement.innerText.trim() === 'Status:') {
// Get the status text after the "Status:" span
statusText = darkTextElement.nextSibling.textContent.trim();
}
});
// === Store the Status ===
if (type === 'anime') {
storeAnimeStatus(id, statusText);// Store the status
}
// === Insert Status into the Document ===
addStatus(type, count, url, id, selector, parent)
}
};
// Send the request
xhr.open('GET', url);
xhr.send();
}
function storeAnimeStatus(id, status) {
storedAnimeStatus[id] = [status, Date.now()];
GM_setValue('status', storedAnimeStatus);
}
function CheckStatusAge(id) {
if (storedAnimeStatus.hasOwnProperty(id)) {
if (typeof storedAnimeStatus[id][0] === 'string' && storedAnimeStatus[id][0] !== 'Finished Airing') {
let dateNow = Date.now();
let dateOld = storedAnimeStatus[id][1];
if (dateNow - dateOld > 10800000) {
return false;
}
}
return true;
}
return false;
}
var storedAnimeStatus = GM_getValue('status')
if (!storedAnimeStatus) {
GM_setValue('status', {});
storedAnimeStatus = {};
}
// Launch actual script
console.log('Status Scritp Running')
StatusScript();