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.
This commit is contained in:
Kumi 2024-09-24 15:11:22 +02:00
parent 7d88784870
commit b1c616ea50
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 31 additions and 0 deletions

30
assets/js/duckmode.js Normal file
View file

@ -0,0 +1,30 @@
// 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);
}

View file

@ -30,6 +30,7 @@ function duck_behavior_journal_scripts()
wp_enqueue_script('jquery', get_template_directory_uri() . '/assets/dist/js/jquery-3.7.1.min.js', array(), null, true);
wp_enqueue_script('bootstrap-js', get_template_directory_uri() . '/assets/dist/js/bootstrap.bundle.min.js', array(), null, true);
wp_enqueue_script('lightbox', get_template_directory_uri() . '/assets/js/lightbox.js', array(), null, true);
wp_enqueue_script('duckmode', get_template_directory_uri() . '/assets/js/duckmode.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'duck_behavior_journal_scripts');