-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwanikani-mnemonic-images.user.js
221 lines (195 loc) · 8.59 KB
/
wanikani-mnemonic-images.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
217
218
219
220
221
// ==UserScript==
// @name WaniKani Mnemonic Images
// @namespace http://tampermonkey.net/
// @version 1.9
// @description Generate and display mnemonic images on WaniKani
// @author Scott Duffey
// @match https://*.wanikani.com/*
// @grant GM_xmlhttpRequest
// @connect wanikani-mnemonic-images.com
// ==/UserScript==
(function () {
'use strict';
let currentSubjectId = null;
let previousSectionReading = null;
let previousSectionMeaning = null;
// Function to create an image element
function createImageElement(src, id) {
const img = document.createElement('img');
img.src = src;
img.id = id;
img.style.width = '100%';
img.style.height = 'auto';
img.style.maxWidth = '500px';
img.style.maxHeight = '500px';
img.style.marginTop = '10px';
return img;
}
// Function to create a "Generate image..." button with a spinner
function createGenerateButton(subjectId, sectionType) {
const button = document.createElement('button');
const buttonText = document.createElement('span');
buttonText.innerText = `Generate ${sectionType} image...`;
const buttonId = `generate-button-${subjectId}-${sectionType}`;
button.id = buttonId;
button.classList.add('wk-button', 'wk-button--default', 'generate-image-button');
button.style.display = 'inline-block';
button.style.marginTop = '10px';
button.style.width = 'auto';
button.style.cursor = 'pointer';
const spinner = document.createElement('span');
spinner.classList.add('spinner');
spinner.style.marginLeft = '10px';
spinner.style.display = 'none';
spinner.innerHTML = `
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.spinner {
border: 2px solid rgba(0, 0, 0, 0.1);
border-left-color: rgb(85, 85, 85);
border-radius: 50%;
width: 16px;
height: 16px;
display: inline-block;
animation: spin 1s linear infinite;
}
</style>`;
button.appendChild(buttonText);
button.appendChild(spinner);
button.addEventListener('click', async () => {
try {
buttonText.innerText = `Generating ${sectionType} image...`;
spinner.style.display = 'inline-block';
button.disabled = true;
button.style.cursor = 'not-allowed';
const response = await generateImage(subjectId, sectionType);
if (response.status === 201) {
const paddedId = subjectId.toString().padStart(5, '0');
const imageUrl = `https://assets.wanikani-mnemonic-images.com/${paddedId}_${sectionType}.png`;
await waitForImage(imageUrl, button, spinner);
const img = createImageElement(imageUrl, `image-${subjectId}-${sectionType}`);
button.insertAdjacentElement('afterend', img);
button.remove();
} else {
throw new Error('Failed to generate image.');
}
} catch (error) {
alert('Failed to generate image.');
buttonText.innerText = `Generate ${sectionType} image...`;
spinner.style.display = 'none';
button.disabled = false;
button.style.cursor = 'pointer';
}
});
return button;
}
// Function to inject image or generate button for a given subject ID and section type (meaning or reading)
function injectImageOrButton(subjectId, sectionType, sectionContent) {
const paddedId = subjectId.toString().padStart(5, '0');
let url = `https://assets.wanikani-mnemonic-images.com/${paddedId}_${sectionType}.png`;
GM_xmlhttpRequest({
method: 'HEAD',
url: url,
onload: function (response) {
if (response.status === 200) {
const img = createImageElement(url, `image-${subjectId}-${sectionType}`);
sectionContent.appendChild(img);
console.log(`Image successfully loaded for subject ID ${paddedId} (${sectionType})`);
} else if (response.status === 404) {
const button = createGenerateButton(subjectId, sectionType);
sectionContent.appendChild(button);
console.log(`Image not found for subject ID ${paddedId} (${sectionType}), adding generate button.`);
} else {
console.error(`Failed to load image for subject ID ${paddedId} (${sectionType}): ${response.status}`);
}
},
onerror: function (error) {
console.error(`Request error for subject ID ${paddedId} (${sectionType}):`, error);
}
});
}
// Function to observe the body for changes and update relevant sections for lessons and reviews
function observeSections() {
const observer = new MutationObserver(() => {
const lessonSectionReading = Array.from(document.querySelectorAll('.subject-section')).filter((e) => e.textContent.includes('Reading Mnemonic') || e.textContent.includes('Reading Explanation'))?.[0]?.querySelector('.subject-section__content');
const reviewSectionReading = Array.from(document.getElementById('section-reading')?.querySelectorAll('.subject-section__subsection') ?? []).filter((e) => e.textContent.includes('Mnemonic') || e.textContent.includes('Explanation'))?.[0];
const sectionReading = lessonSectionReading || reviewSectionReading;
const lessonSectionMeaning = Array.from(document.querySelectorAll('.subject-section')).filter((e) => e.textContent.includes('Meaning Mnemonic') || e.textContent.includes('Meaning Explanation'))?.[0]?.querySelector('.subject-section__content');
const reviewSectionMeaning = Array.from(document.getElementById('section-meaning')?.querySelectorAll('.subject-section__subsection') ?? []).filter((e) => e.textContent.includes('Mnemonic') || e.textContent.includes('Explanation'))?.[0];
const sectionMeaning = lessonSectionMeaning || reviewSectionMeaning;
const subjectIdElement = document.querySelector('label[for="user-response"][data-subject-id]');
const subjectMeta = document.querySelector('meta[name="subject_id"]');
const turboFrame = document.querySelector('turbo-frame[src*="subject_id="]'); // Look for turbo-frame with subject_id
let newSubjectId = null;
if (subjectIdElement) {
newSubjectId = parseInt(subjectIdElement.getAttribute('data-subject-id'));
} else if (subjectMeta) {
newSubjectId = parseInt(subjectMeta.getAttribute('content'));
} else if (turboFrame) {
const src = turboFrame.getAttribute('src');
const match = src.match(/subject_id=(\d+)/);
if (match) {
newSubjectId = parseInt(match[1]);
}
}
if (newSubjectId && newSubjectId !== currentSubjectId) {
currentSubjectId = newSubjectId;
console.log(`New subject loaded. Subject ID: ${currentSubjectId}`);
}
const meaningButtonOrImage = document.getElementById(`image-${currentSubjectId}-meaning`) || document.getElementById(`generate-button-${currentSubjectId}-meaning`);
const readingButtonOrImage = document.getElementById(`image-${currentSubjectId}-reading`) || document.getElementById(`generate-button-${currentSubjectId}-reading`);
if (sectionReading && (!previousSectionReading || !sectionReading.isEqualNode(previousSectionReading) || (!document.contains(previousSectionReading) && !readingButtonOrImage))) {
previousSectionReading = sectionReading;
injectImageOrButton(currentSubjectId, 'reading', sectionReading);
}
if (sectionMeaning && (!previousSectionMeaning || !sectionMeaning.isEqualNode(previousSectionMeaning) || (!document.contains(previousSectionMeaning) && !meaningButtonOrImage))) {
previousSectionMeaning = sectionMeaning;
injectImageOrButton(currentSubjectId, 'meaning', sectionMeaning);
}
});
observer.observe(document.body, { childList: true, subtree: true });
}
// Function to call the worker API to generate an image
async function generateImage(subjectId, sectionType) {
const url = `https://api.wanikani-mnemonic-images.com/${sectionType}/${subjectId}`;
return await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
}
// Function to wait until an image is available using GM_xmlhttpRequest to avoid CORS issues
async function waitForImage(imageUrl, button, spinner) {
let attempts = 0;
while (attempts < 30) {
const success = await new Promise((resolve) => {
GM_xmlhttpRequest({
method: 'HEAD',
url: imageUrl,
onload: function (response) {
if (response.status === 200) {
resolve(true);
} else {
resolve(false);
}
},
onerror: function () {
resolve(false);
}
});
});
if (success) {
return;
}
await new Promise(resolve => setTimeout(resolve, 1000));
attempts++;
}
throw new Error('Image did not become available in time.');
}
// Initialize the script by observing the body for changes
observeSections();
})();