wp-duckbehaviorjournal/assets/js/duckmode.js
Kumi b1c616ea50
feat: add duckmode translation feature
Introduced a new "duckmode" feature which replaces text content on the webpage with "quack" variations, while preserving capitalization and punctuation. The feature activates when the "duckmode" parameter is present in the URL. Included the necessary script and enqueued it in the theme's functions.php.
2024-09-24 15:11:22 +02:00

30 lines
904 B
JavaScript

// Function to transform a word to "quack", preserving capitalization
function toQuack(word) {
if (word.length > 1 && word === word.toUpperCase()) {
return 'QUACK';
} else if (word[0] === word[0].toUpperCase()) {
return 'Quack';
} else {
return 'quack';
}
}
// Function to replace text with "quack", preserving punctuation
function translateToDuckLanguage(node) {
if (node.nodeType === Node.TEXT_NODE) {
if (node.textContent.trim()) {
node.textContent = node.textContent.replace(
/\b[a-zA-Z]+\b/g,
match => toQuack(match)
);
}
} else {
node.childNodes.forEach(translateToDuckLanguage);
}
}
// Translate if the "duckmode" parameter is present in the URL
if (window.location.search.includes('duckmode')) {
document.body.childNodes.forEach(translateToDuckLanguage);
}