-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kanji_content_detect.js
293 lines (274 loc) · 10.3 KB
/
kanji_content_detect.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
283
284
285
286
287
288
289
290
291
292
293
/***************************************************************
* This script set to run_at document load. See manifest.json.
***************************************************************/
var USER_KANJI_REGEXP;
var INCLUDE_LINK_TEXT = false;
var INSERTED_NODES_TO_CHECK = [];
var INSERTED_NODE_CHECK_TIMEOUT_ID = null;
var MUTATION_OBSERVER = null
var PERSISTENT_MODE
// Add an empty onunload function to force run this content_script even when back/forward
// https://stackoverflow.com/questions/2638292/after-travelling-back-in-firefox-history-javascript-wont-run
window.addEventListener('unload', function () { })
function autoSetBrowserActionIcon() {
const state = document.body.hasAttribute("fiprocessed") ? 'INSERTED' : 'UNTOUCHED'
browser.runtime.sendMessage({ message: "set_page_action_icon_status", value: state });
return state
}
browser.runtime.sendMessage({ message: "config_values_request" }).then(function (response) {
const alreadyEnabled = autoSetBrowserActionIcon() === 'INSERTED' // TODO: Use MutationObserver to auto call this function?
if (alreadyEnabled) {
// REFACTORING: May needn't because never happened after adding document.onunload ...?
// If already enabled, just init dom_parser directly without detecting kanji again.
// This situation may happened when using back/next of browser
browser.runtime.sendMessage({ message: "init_dom_parser_for_tab" });
return
}
USER_KANJI_REGEXP = new RegExp("[" + response.userKanjiList + "]");
INCLUDE_LINK_TEXT = JSON.parse(response.includeLinkText);
PERSISTENT_MODE = JSON.parse(response.persistentMode);
let useMobileFloatingButton = JSON.parse(response.useMobileFloatingButton);
let globallyShowMobileFloatingButton = JSON.parse(response.globallyShowMobileFloatingButton);
if (globallyShowMobileFloatingButton) {
fiAddFloatingIcon()
}
// Having received the config data, start searching for relevant kanji
// If none find, do nothing for now except start a listener for node insertions
// If persistent mode enabled - enable furigana right away
if (document.body.innerText.match(/[\u3400-\u9FBF]/) || PERSISTENT_MODE || globallyShowMobileFloatingButton) {
browser.runtime.sendMessage({message: "init_dom_parser_for_tab"});
} else {
MUTATION_OBSERVER = new MutationObserver(DOMNodeInsertedHandler);
MUTATION_OBSERVER.observe(document, { childList: true, subtree: true });
}
});
function DOMNodeInsertedHandler(mutationList, observer) {
for (let mutation of mutationList) {
if (mutation.type === 'childList') {
e = mutation;
if (INSERTED_NODES_TO_CHECK.includes(e.target)) { continue }
if (INSERTED_NODES_TO_CHECK.includes(e.target.parentNode)) { continue }
if ((e.target.nodeType == Node.TEXT_NODE || e.target.nodeType == Node.CDATA_SECTION_NODE) &&
e.target.innerText !== undefined &&
e.target.innerText !== '' &&
e.target.parentNode) {
// console.log('type 1', e.target)
INSERTED_NODES_TO_CHECK.push(e.target.parentNode)
} else if (
e.target.nodeType === Node.ELEMENT_NODE &&
e.target.tagName !== "IMG" &&
e.target.tagName !== "SVG" &&
e.target.tagName !== "CANVAS" &&
e.target.tagName !== "OBJECT" &&
e.target.tagName !== "EMBED" &&
e.target.tagName !== "BODY" &&
e.target.tagName !== "HEAD" &&
e.target.innerText !== undefined &&
e.target.innerText !== ''
) {
// console.log('type 2', e.target)
INSERTED_NODES_TO_CHECK.push(e.target);
} else {
return;
}
window.clearTimeout(INSERTED_NODE_CHECK_TIMEOUT_ID)
INSERTED_NODE_CHECK_TIMEOUT_ID = window.setTimeout(processChangedNodes, 1000);
}
}
}
function processChangedNodes() {
for (const node of INSERTED_NODES_TO_CHECK) {
if (node.innerText.length === 0) { continue }
if (node.innerText.match(/[\u3400-\u9FBF]/)) {
browser.runtime.sendMessage({message: "init_dom_parser_for_tab"});
MUTATION_OBSERVER.disconnect()
break
}
}
INSERTED_NODES_TO_CHECK = [];
INSERTED_NODE_CHECK_TIMEOUT_ID = null;
}
function hasOnlySimpleKanji(rubySubstr) {
var foundKanji = rubySubstr.match(/[\u3400-\u9FBF]/g);
if (foundKanji) {
for (var x = 0; x < foundKanji.length; x++) {
if (!USER_KANJI_REGEXP.exec(foundKanji[x]))
return false;
}
} else {
return null;
}
return true;
}
function fiFloatingIconIsExist () {
return document.getElementById('furiganaize_buttons_container')
}
function fiToggleFloatingIcon() {
const el = fiFloatingIconIsExist()
if (el) {
el.remove()
} else {
fiAddFloatingIcon()
}
}
function fiRemoveFloatingIcon() {
const el = fiFloatingIconIsExist()
if (el) { el.remove() }
}
async function safeToggleFurigana() {
if ((typeof toggleFurigana) !== 'function') {
await browser.runtime.sendMessage({ message: "force_load_dom_parser" })
}
fiRemoveNoScriptTags()
toggleFurigana() // In `text_to_furigana_dom_parse.js`
}
function transposeFloatButton() {
const el = document.querySelector('#furiganaize_buttons_container')
if (el.classList.contains('leftSide')) {
el.classList.remove('leftSide')
} else {
el.classList.add('leftSide')
}
}
function fiAddFloatingIcon() {
const existed = fiFloatingIconIsExist()
if (existed) {
existed.remove()
}
const btnsWrapper = document.createElement('div')
btnsWrapper.id = 'furiganaize_buttons_container'
const ledIndicator = document.createElement('div')
ledIndicator.classList.add('led_indicator')
ledIndicator.classList.add('red')
const triggerBtn = document.createElement('div')
triggerBtn.id = 'furiganaize_trigger_button'
triggerBtn.classList.add('furiganaize_button')
const transposeBtn = document.createElement('div')
transposeBtn.id = 'furiganaize_transpose_button'
transposeBtn.classList.add('furiganaize_button')
transposeBtn.innerText = 'うつす'
btnsWrapper.appendChild(triggerBtn)
btnsWrapper.appendChild(ledIndicator)
btnsWrapper.appendChild(transposeBtn)
const fu = document.createElement('span')
fu.innerText = `ふ`
triggerBtn.append(fu)
// TODO: Draggable floating button
// div.draggable = true
// div.ondrag = function (ev) {
// console.log(ev)
// }
triggerBtn.onclick = function () { safeToggleFurigana() }
transposeBtn.onclick = function () { transposeFloatButton() }
const styleEl = document.createElement('style')
styleEl.innerText = `
#furiganaize_buttons_container {
position: fixed;
bottom: 30px;
display: flex;
flex-direction: column;
right: 20px;
z-index: 555555;
}
#furiganaize_buttons_container.leftSide {
left: 20px;
}
#furiganaize_buttons_container .led_indicator {
display: block;
position: absolute;
top: 4px;
left: 4px;
width: 6px;
height: 6px;
border-radius: 6px;
border: 1px solid #333333;
background: #666666
}
#furiganaize_buttons_container .led_indicator.purple {
border: 1px solid #5500a1;
background: #ee33f1
}
#furiganaize_buttons_container .led_indicator.red {
border: 1px solid #660000;
background: #ff6666
}
#furiganaize_buttons_container .led_indicator.green {
border: 1px solid #337700;
background: #99dd22
}
#furiganaize_trigger_button {
display: flex;
align-items: center;
justify-content: center;
width: 50px;
height: 50px;
}
#furiganaize_trigger_button span {
font-size: 40px;
user-select: none;
margin-top: -0.5rem;
}
.furiganaize_button {
color: #000000;
background : #eeeeee;
border: 1px solid #aaa;
margin-top: -1px;
cursor: pointer;
user-select: none;
font-family: sans;
width: 50px;
}
.furiganaize_button:active {
background: #cccccc;
}
#furiganaize_buttons_container.busy .furiganaize_button {
background: #cccccc;
pointer-events: none;
}
#furiganaize_buttons_container.busy .furiganaize_button span,
#furiganaize_buttons_container.busy .led_indicator {
animation: furiganaize_blinking 0.5s linear infinite;
}
#furiganaize_transpose_button {
text-align: center;
font-size: 12px;
}
@keyframes furiganaize_blinking {
50% { opacity: 0.2; }
}
`
document.body.append(btnsWrapper)
document.body.append(styleEl)
}
function fiSetFloatingButtonState(state) {
const wrapper = document.querySelector('#furiganaize_buttons_container')
if (!wrapper) { return }
const led = document.querySelector('#furiganaize_buttons_container .led_indicator')
led.className = 'led_indicator'
// console.trace('Button====>', wrapper, state)
switch (state) {
case 'UNTOUCHED': {
wrapper.classList.remove('busy')
led.classList.add('red')
break
}
case 'PROCESSING': {
wrapper.classList.add('busy')
led.classList.add('purple')
break
}
case 'INSERTED': {
wrapper.classList.remove('busy')
led.classList.add('green')
break
}
}
}
/** <noscript> tag will cause many issues to Furiganaize, for example:
* - Google: force reload / redirect page. (Clue: 1. Use window.beforeunload to avoid reload, then insert furigana. 2. A suspicious style tag is found: <style>table,div,span,p{display:none}</style> 3. This tag is in <noscript>)
* - Twitter: the whole page becomes blank.
* so remove it before inserting furigana.
**/
function fiRemoveNoScriptTags() {
document.querySelectorAll('noscript').forEach(el => el.remove())
}