|
113 | 113 | },
|
114 | 114 | {}
|
115 | 115 | );
|
116 | | - constDEBUG_KEY="next-i18n-debug"; |
117 | | - functiondebugLog(...args){ |
118 | | - if(localStorage.getItem(DEBUG_KEY)==="true") { |
119 | | - console.log("[DEBUG]", ...args); |
120 | | - } |
| 116 | + functiondevLog(...args){ |
| 117 | + } |
| 118 | + functiondevWarn(...args) { |
| 119 | + } |
| 120 | + functiondevError(...args){ |
121 | 121 | }
|
122 | | - console.log("π Next.js Translation Helper loaded (React-compatible version)"); |
123 | | - debugLog( |
124 | | - 'Debug mode enabled. Use localStorage.removeItem("next-i18n-debug") to disable.' |
125 | | - ); |
126 | 122 | function waitForLearnButton(callback, maxAttempts = 30) {
|
127 | 123 | let attempts = 0;
|
128 | 124 | const check = () => {
|
|
134 | 130 | attempts++;
|
135 | 131 | setTimeout(check, 200);
|
136 | 132 | } else {
|
137 | | - console.log("β οΈ Learn button not found after maximum attempts"); |
| 133 | + devLog("β οΈ Learn button not found after maximum attempts"); |
138 | 134 | }
|
139 | 135 | } catch (error) {
|
140 | | - console.warn("Error checking for Learn button:", error); |
141 | 136 | }
|
142 | 137 | };
|
143 | 138 | check();
|
|
250 | 245 | try {
|
251 | 246 | const currentPath = window.location.pathname;
|
252 | 247 | const targetUrl = new URL(currentPath, locale.url).href;
|
253 | | - console.log(`π Navigating to ${locale.nativeName}: ${targetUrl}`); |
| 248 | + devLog(`π Navigating to ${locale.nativeName}: ${targetUrl}`); |
254 | 249 | window.location.href = targetUrl;
|
255 | 250 | } catch (error) {
|
256 | | - console.error("Error navigating to locale:", error); |
257 | 251 | window.location.href = locale.url;
|
258 | 252 | }
|
259 | 253 | });
|
|
313 | 307 | for (const selector of learnButtonSelectors) {
|
314 | 308 | learnButton = document.querySelector(selector);
|
315 | 309 | if (learnButton) {
|
316 | | - console.log(`π― Found Learn button with selector: ${selector}`); |
317 | | - debugLog("Learn button element:", learnButton); |
318 | 310 | break;
|
319 | 311 | }
|
320 | 312 | }
|
321 | 313 | if (!learnButton) {
|
322 | | - debugLog("Learn button not found with any selector:", learnButtonSelectors); |
323 | | - console.log("β οΈ Learn button not found, will retry..."); |
324 | 314 | return;
|
325 | 315 | }
|
326 | 316 | try {
|
327 | 317 | const existingButton = document.querySelector(
|
328 | 318 | ".next-i18n-translate-container"
|
329 | 319 | );
|
330 | 320 | if (existingButton) {
|
331 | | - debugLog("Translation button already exists, skipping"); |
332 | | - console.log("β
Translation button already exists"); |
| 321 | + devLog("β
Translation button already exists"); |
333 | 322 | return;
|
334 | 323 | }
|
335 | 324 | const translationDropdown = createTranslationDropdown();
|
336 | 325 | const parentNode = learnButton.parentNode;
|
337 | 326 | if (!parentNode) {
|
338 | | - console.error("β Learn button has no parent node"); |
| 327 | + devError("β Learn button has no parent node"); |
339 | 328 | return;
|
340 | 329 | }
|
341 | 330 | if (learnButton.nextSibling) {
|
342 | 331 | parentNode.insertBefore(translationDropdown, learnButton.nextSibling);
|
343 | 332 | } else {
|
344 | 333 | parentNode.appendChild(translationDropdown);
|
345 | 334 | }
|
346 | | - console.log("β
Translation button added successfully"); |
| 335 | + devLog("β
Translation button added successfully"); |
347 | 336 | setTimeout(() => {
|
348 | 337 | const verifyButton = document.querySelector(
|
349 | 338 | ".next-i18n-translate-container"
|
350 | 339 | );
|
351 | 340 | if (!verifyButton) {
|
352 | | - console.warn( |
| 341 | + devWarn( |
353 | 342 | "β οΈ Translation button was removed shortly after adding, React might be re-rendering"
|
354 | 343 | );
|
355 | 344 | setTimeout(() => {
|
356 | | - console.log( |
| 345 | + devLog( |
357 | 346 | "π Attempting to re-add translation button after React stabilization"
|
358 | 347 | );
|
359 | 348 | addTranslationButton();
|
360 | 349 | }, 1e3);
|
361 | 350 | } else {
|
362 | | - console.log("π Translation button is stable and working!"); |
| 351 | + devLog("π Translation button is stable and working!"); |
363 | 352 | }
|
364 | 353 | }, 500);
|
365 | 354 | } catch (error) {
|
366 | | - console.error("β Error adding translation button:", error); |
367 | 355 | }
|
368 | 356 | }
|
369 | | - console.log("π Initializing Next.js Translation Helper..."); |
370 | 357 | function initializeScript() {
|
371 | | - console.log("π Current URL:", window.location.href); |
372 | | - console.log("π Document ready state:", document.readyState); |
373 | | - console.log("β³ Waiting for React to stabilize..."); |
374 | 358 | setTimeout(() => {
|
375 | | - console.log("π¬ React should be stable now, adding translation button"); |
376 | 359 | addTranslationButton();
|
377 | 360 | setTimeout(() => {
|
378 | 361 | if (!document.querySelector(".next-i18n-translate-container")) {
|
379 | | - console.log("π First attempt failed, trying again..."); |
380 | 362 | waitForLearnButton(() => {
|
381 | | - console.log( |
382 | | - "π― Learn button found, attempting to add translation button" |
383 | | - ); |
| 363 | + devLog("π― Learn button found, attempting to add translation button"); |
384 | 364 | addTranslationButton();
|
385 | 365 | });
|
386 | 366 | }
|
387 | 367 | }, 500);
|
388 | | - }, 2e3); |
| 368 | + }, 1e3); |
389 | 369 | }
|
390 | 370 | if (document.readyState === "loading") {
|
391 | 371 | document.addEventListener("DOMContentLoaded", initializeScript);
|
392 | 372 | } else {
|
393 | 373 | initializeScript();
|
394 | 374 | }
|
395 | | - let lastUrl = location.href; |
396 | | - let checkInterval; |
397 | | - function startPeriodicCheck() { |
398 | | - if (checkInterval) { |
399 | | - clearInterval(checkInterval); |
400 | | - } |
401 | | - let checkCount = 0; |
402 | | - let missCount = 0; |
403 | | - checkInterval = window.setInterval(() => { |
404 | | - checkCount++; |
405 | | - if (!window.location.href.includes("nextjs.org")) { |
406 | | - debugLog("Not on nextjs.org anymore, stopping periodic check"); |
407 | | - clearInterval(checkInterval); |
408 | | - return; |
409 | | - } |
410 | | - const learnButton = document.querySelector('a[href="/learn"]'); |
411 | | - const ourButton = document.querySelector(".next-i18n-translate-container"); |
412 | | - debugLog("Periodic check:", { |
413 | | - count: checkCount, |
414 | | - learnButton: !!learnButton, |
415 | | - ourButton: !!ourButton, |
416 | | - missCount |
417 | | - }); |
418 | | - if (learnButton && !ourButton) { |
419 | | - missCount++; |
420 | | - console.log("π React re-render detected, re-adding translation button"); |
421 | | - addTranslationButton(); |
422 | | - } else if (ourButton) { |
423 | | - missCount = 0; |
424 | | - } |
425 | | - if (checkCount > 20 && missCount === 0) { |
426 | | - clearInterval(checkInterval); |
427 | | - checkInterval = window.setInterval(() => { |
428 | | - const learnBtn = document.querySelector('a[href="/learn"]'); |
429 | | - const ourBtn = document.querySelector(".next-i18n-translate-container"); |
430 | | - if (learnBtn && !ourBtn) { |
431 | | - console.log( |
432 | | - "π Late React re-render detected, re-adding translation button" |
433 | | - ); |
434 | | - addTranslationButton(); |
435 | | - } |
436 | | - }, 2e3); |
437 | | - console.log("π― Switching to low-frequency monitoring"); |
438 | | - } |
439 | | - }, 500); |
440 | | - } |
441 | | - const observer = new MutationObserver((mutations) => { |
442 | | - var _a, _b; |
443 | | - const url = location.href; |
444 | | - let shouldRecheck = false; |
445 | | - if (url !== lastUrl) { |
446 | | - lastUrl = url; |
447 | | - shouldRecheck = true; |
448 | | - console.log("π URL changed, rechecking translation button"); |
449 | | - debugLog("URL change:", { from: lastUrl, to: url }); |
450 | | - } |
451 | | - for (const mutation of mutations) { |
452 | | - if (mutation.type === "childList") { |
453 | | - for (const node of mutation.removedNodes) { |
454 | | - if (node.nodeType === Node.ELEMENT_NODE) { |
455 | | - const element = node; |
456 | | - if (((_a = element.classList) == null ? void 0 : _a.contains("next-i18n-translate-container")) || ((_b = element.querySelector) == null ? void 0 : _b.call(element, ".next-i18n-translate-container"))) { |
457 | | - shouldRecheck = true; |
458 | | - console.log("π Translation button removed by React, will re-add"); |
459 | | - } |
460 | | - } |
461 | | - } |
462 | | - } |
463 | | - } |
464 | | - if (shouldRecheck) { |
465 | | - setTimeout(addTranslationButton, 300); |
466 | | - } |
467 | | - }); |
468 | | - observer.observe(document, { |
469 | | - subtree: true, |
470 | | - childList: true, |
471 | | - attributes: false |
472 | | - // Don't watch attributes to reduce noise |
473 | | - }); |
474 | | - startPeriodicCheck(); |
475 | | - window.addEventListener("beforeunload", () => { |
476 | | - if (checkInterval) { |
477 | | - clearInterval(checkInterval); |
478 | | - } |
479 | | - observer.disconnect(); |
480 | | - }); |
481 | 375 | })();
|
0 commit comments