PageSourceSearch

https://chinesedora.com/115-hk.js?115082332

js chinesedora.com collected 2026-10-02 16:31:48 UTC 8,113 bytes, 473 lines download raw bytes

1(function () {
2
3    "use strict";
4
5
6    /*
7     * =========================================================
8     * 哆啦A夢中文網
9     * 台灣/香港用詞轉換
10     *
11     * 正式判斷方式:
12     *
13     * data-country="HK"
14     *     → 香港
15     *
16     * data-country="TW"
17     *     → 台灣
18     *
19     *
20     * 測試:
21     *
22     * ?region=HK
23     *     → 強制香港版
24     *
25     * ?region=TW
26     *     → 強制台灣版
27     *
28     *
29     * 沒有任何設定:
30     *     → 預設台灣
31     * =========================================================
32     */
33
34
35    /*
36     * =========================================================
37     * 取得地區
38     * =========================================================
39     */
40
41    function getRegion() {
42
43        /*
44         * -----------------------------------------------------
45         * 1. URL 測試參數
46         * -----------------------------------------------------
47         */
48
49        var params =
50            new URLSearchParams(
51                window.location.search
52            );
53
54
55        var testRegion =
56            params.get("region");
57
58
59        if (testRegion) {
60
61            testRegion =
62                testRegion.toUpperCase();
63
64
65            if (
66                testRegion === "HK" ||
67                testRegion === "TW"
68            ) {
69
70                return testRegion;
71
72            }
73
74        }
75
76
77        /*
78         * -----------------------------------------------------
79         * 2. HTML data-country
80         *
81         * 例如:
82         *
83         * <html
84         *     lang="zh-Hant-TW"
85         *     data-country="HK">
86         *
87         * -----------------------------------------------------
88         */
89
90        var country =
91            document.documentElement.getAttribute(
92                "data-country"
93            );
94
95
96        if (country) {
97
98            country =
99                country.toUpperCase();
100
101
102            if (country === "HK") {
103
104                return "HK";
105
106            }
107
108
109            if (country === "TW") {
110
111                return "TW";
112
113            }
114
115        }
116
117
118        /*
119         * -----------------------------------------------------
120         * 3. 沒有地區資訊
121         *
122         * 一律台灣。
123         *
124         * 不再看 navigator.language。
125         * -----------------------------------------------------
126         */
127
128        return "TW";
129
130    }
131
132
133    var region =
134        getRegion();
135
136
137    /*
138     * =========================================================
139     * 香港用詞
140     * =========================================================
141     */
142
143    var replacements = [
144
145        {
146            from: /哆啦A夢/g,
147            to: "多啦A夢"
148        },
149
150        {
151            from: /秘密道具/g,
152            to: "神奇法寶"
153        },
154
155        {
156            from: /祕密道具/g,
157            to: "神奇法寶"
158        }
159
160    ];
161
162
163    /*
164     * =========================================================
165     * 排除元素
166     * =========================================================
167     */
168
169    function shouldIgnore(element) {
170
171        if (!element) {
172            return true;
173        }
174
175
176        var tag =
177            element.tagName;
178
179
180        return (
181            tag === "SCRIPT" ||
182            tag === "STYLE" ||
183            tag === "NOSCRIPT" ||
184            tag === "TEXTAREA" ||
185            tag === "INPUT" ||
186            tag === "SELECT"
187        );
188
189    }
190
191
192    /*
193     * =========================================================
194     * 替換文字節點
195     * =========================================================
196     */
197
198    function replaceTextNode(node) {
199
200        if (
201            !node ||
202            node.nodeType !== Node.TEXT_NODE
203        ) {
204
205            return;
206
207        }
208
209
210        if (!node.nodeValue) {
211            return;
212        }
213
214
215        var parent =
216            node.parentElement;
217
218
219        if (
220            parent &&
221            shouldIgnore(parent)
222        ) {
223
224            return;
225
226        }
227
228
229        var oldText =
230            node.nodeValue;
231
232
233        var newText =
234            oldText;
235
236
237        replacements.forEach(
238            function (item) {
239
240                newText =
241                    newText.replace(
242                        item.from,
243                        item.to
244                    );
245
246            }
247        );
248
249
250        if (
251            newText !== oldText
252        ) {
253
254            node.nodeValue =
255                newText;
256
257        }
258
259    }
260
261
262    /*
263     * =========================================================
264     * 掃描內容
265     * =========================================================
266     */
267
268    function scan(root) {
269
270        if (!root) {
271            return;
272        }
273
274
275        if (
276            root.nodeType ===
277            Node.TEXT_NODE
278        ) {
279
280            replaceTextNode(root);
281
282            return;
283
284        }
285
286
287        if (
288            root.nodeType ===
289            Node.ELEMENT_NODE
290        ) {
291
292            if (
293                shouldIgnore(root)
294            ) {
295
296                return;
297
298            }
299
300        }
301
302
303        var walker =
304            document.createTreeWalker(
305                root,
306                NodeFilter.SHOW_TEXT
307            );
308
309
310        var nodes = [];
311
312
313        while (
314            walker.nextNode()
315        ) {
316
317            nodes.push(
318                walker.currentNode
319            );
320
321        }
322
323
324        nodes.forEach(
325            function (node) {
326
327                replaceTextNode(node);
328
329            }
330        );
331
332    }
333
334
335    /*
336     * =========================================================
337     * 監控動態內容
338     * =========================================================
339     */
340
341    function startObserver() {
342
343        if (
344            typeof MutationObserver ===
345            "undefined"
346        ) {
347
348            return;
349
350        }
351
352
353        var observer =
354            new MutationObserver(
355                function (mutations) {
356
357                    mutations.forEach(
358                        function (mutation) {
359
360                            if (
361                                mutation.type ===
362                                "childList"
363                            ) {
364
365                                mutation.addedNodes.forEach(
366                                    function (node) {
367
368                                        scan(node);
369
370                                    }
371                                );
372
373                            }
374
375
376                            if (
377                                mutation.type ===
378                                "characterData"
379                            ) {
380
381                                replaceTextNode(
382                                    mutation.target
383                                );
384
385                            }
386
387                        }
388                    );
389
390                }
391            );
392
393
394        observer.observe(
395            document.body,
396            {
397                childList: true,
398                subtree: true,
399                characterData: true
400            }
401        );
402
403    }
404
405
406    /*
407     * =========================================================
408     * 初始化
409     * =========================================================
410     */
411
412    function init() {
413
414        console.log(
415            "[CD Region] 地區:",
416            region
417        );
418
419
420        /*
421         * 台灣不做任何處理
422         */
423
424        if (
425            region !== "HK"
426        ) {
427
428            return;
429
430        }
431
432
433        /*
434         * 先處理現有 HTML
435         */
436
437        scan(
438            document.body
439        );
440
441
442        /*
443         * 再監控動態內容
444         */
445
446        startObserver();
447
448    }
449
450
451    /*
452     * =========================================================
453     * 啟動
454     * =========================================================
455     */
456
457    if (
458        document.readyState ===
459        "loading"
460    ) {
461
462        document.addEventListener(
463            "DOMContentLoaded",
464            init
465        );
466
467    } else {
468
469        init();
470
471    }
472
473})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.