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.