Skip to content

Commit dc100c3

Browse files
committed
feat: remove PeriodicCheck
1 parent 1ff64f5 commit dc100c3

File tree

4 files changed

+57
-279
lines changed

4 files changed

+57
-279
lines changed

apps/tampermonkey/dist/script.iife.js

Lines changed: 15 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,12 @@
113113
},
114114
{}
115115
);
116-
const DEBUG_KEY = "next-i18n-debug";
117-
function debugLog(...args) {
118-
if (localStorage.getItem(DEBUG_KEY) === "true") {
119-
console.log("[DEBUG]", ...args);
120-
}
116+
function devLog(...args) {
117+
}
118+
function devWarn(...args) {
119+
}
120+
function devError(...args) {
121121
}
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-
);
126122
function waitForLearnButton(callback, maxAttempts = 30) {
127123
let attempts = 0;
128124
const check = () => {
@@ -134,10 +130,9 @@
134130
attempts++;
135131
setTimeout(check, 200);
136132
} else {
137-
console.log("⚠️ Learn button not found after maximum attempts");
133+
devLog("⚠️ Learn button not found after maximum attempts");
138134
}
139135
} catch (error) {
140-
console.warn("Error checking for Learn button:", error);
141136
}
142137
};
143138
check();
@@ -250,10 +245,9 @@
250245
try {
251246
const currentPath = window.location.pathname;
252247
const targetUrl = new URL(currentPath, locale.url).href;
253-
console.log(`🌐 Navigating to ${locale.nativeName}: ${targetUrl}`);
248+
devLog(`🌐 Navigating to ${locale.nativeName}: ${targetUrl}`);
254249
window.location.href = targetUrl;
255250
} catch (error) {
256-
console.error("Error navigating to locale:", error);
257251
window.location.href = locale.url;
258252
}
259253
});
@@ -313,169 +307,69 @@
313307
for (const selector of learnButtonSelectors) {
314308
learnButton = document.querySelector(selector);
315309
if (learnButton) {
316-
console.log(`🎯 Found Learn button with selector: ${selector}`);
317-
debugLog("Learn button element:", learnButton);
318310
break;
319311
}
320312
}
321313
if (!learnButton) {
322-
debugLog("Learn button not found with any selector:", learnButtonSelectors);
323-
console.log("⚠️ Learn button not found, will retry...");
324314
return;
325315
}
326316
try {
327317
const existingButton = document.querySelector(
328318
".next-i18n-translate-container"
329319
);
330320
if (existingButton) {
331-
debugLog("Translation button already exists, skipping");
332-
console.log("✅ Translation button already exists");
321+
devLog("✅ Translation button already exists");
333322
return;
334323
}
335324
const translationDropdown = createTranslationDropdown();
336325
const parentNode = learnButton.parentNode;
337326
if (!parentNode) {
338-
console.error("❌ Learn button has no parent node");
327+
devError("❌ Learn button has no parent node");
339328
return;
340329
}
341330
if (learnButton.nextSibling) {
342331
parentNode.insertBefore(translationDropdown, learnButton.nextSibling);
343332
} else {
344333
parentNode.appendChild(translationDropdown);
345334
}
346-
console.log("✅ Translation button added successfully");
335+
devLog("✅ Translation button added successfully");
347336
setTimeout(() => {
348337
const verifyButton = document.querySelector(
349338
".next-i18n-translate-container"
350339
);
351340
if (!verifyButton) {
352-
console.warn(
341+
devWarn(
353342
"⚠️ Translation button was removed shortly after adding, React might be re-rendering"
354343
);
355344
setTimeout(() => {
356-
console.log(
345+
devLog(
357346
"🔄 Attempting to re-add translation button after React stabilization"
358347
);
359348
addTranslationButton();
360349
}, 1e3);
361350
} else {
362-
console.log("🎉 Translation button is stable and working!");
351+
devLog("🎉 Translation button is stable and working!");
363352
}
364353
}, 500);
365354
} catch (error) {
366-
console.error("❌ Error adding translation button:", error);
367355
}
368356
}
369-
console.log("🚀 Initializing Next.js Translation Helper...");
370357
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...");
374358
setTimeout(() => {
375-
console.log("🎬 React should be stable now, adding translation button");
376359
addTranslationButton();
377360
setTimeout(() => {
378361
if (!document.querySelector(".next-i18n-translate-container")) {
379-
console.log("🔄 First attempt failed, trying again...");
380362
waitForLearnButton(() => {
381-
console.log(
382-
"🎯 Learn button found, attempting to add translation button"
383-
);
363+
devLog("🎯 Learn button found, attempting to add translation button");
384364
addTranslationButton();
385365
});
386366
}
387367
}, 500);
388-
}, 2e3);
368+
}, 1e3);
389369
}
390370
if (document.readyState === "loading") {
391371
document.addEventListener("DOMContentLoaded", initializeScript);
392372
} else {
393373
initializeScript();
394374
}
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-
});
481375
})();

apps/tampermonkey/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"version": "0.0.0",
66
"type": "module",
77
"scripts": {
8-
"dev": "vite build --watch --mode development",
8+
"dev": "NODE_ENV=development vite build --watch --mode development",
99
"build": "vite build",
1010
"type-check": "tsc --noEmit"
1111
},

0 commit comments

Comments
 (0)