orgmode-to-gemini-blog/templates/js/main_script.js

87 lines
No EOL
3.2 KiB
JavaScript

// search bar to send a request to a search engine for the current domain when sent
document.addEventListener('DOMContentLoaded', function () {
// Obtient le nom de domaine de la page courante
const currentDomain = window.location.hostname;
const form = document.getElementById('recherche');
// Ajoute un écouteur d'événement pour l'événement 'submit'
form.addEventListener('submit', function (event) {
// Empêche le comportement par défaut du formulaire
event.preventDefault()
const query = document.getElementById('query').value
const url = `https://duckduckgo.com/?q=${encodeURIComponent(query)}+site%3A${encodeURIComponent(currentDomain)}`;
window.location.href = url;
});
// navigation au clavier
// Sélectionnez le lien à cliquer
const nextArticleLink = document.querySelector('a.next-article');
const previousArticleLink = document.querySelector('a.previous-article');
// Ajoutez un événement de clic au lien
nextArticleLink.addEventListener('click', function () {
// Redirigez l'utilisateur vers la page suivante
window.location.href = nextArticleLink.href;
});
previousArticleLink.addEventListener('click', function () {
// Redirigez l'utilisateur vers la page suivante
window.location.href = nextArticleLink.href;
});
// Ajoutez un événement de touche au document
document.addEventListener('keydown', function (event) {
// Vérifiez si la touche appuyée est 'n' ou la flèche droite
if (event.key === 'n' || event.key === 'ArrowRight') {
// Empêche le comportement par défaut de la touche
event.preventDefault();
// Cliquez sur le lien
nextArticleLink.click();
}
if (event.key === 'p' || event.key === 'ArrowLeft') {
// Empêche le comportement par défaut de la touche
event.preventDefault();
// Cliquez sur le lien
previousArticleLink.click();
}
});
const themes_calsses = ['default', 'mastodon', 'another']
function makeThemesButton(){
// Crée un bouton pour changer de thème et l'ajoute au body
const boutonTheme = document.createElement('button');
boutonTheme.textContent = 'Changer de thème';
boutonTheme.id = 'theme-switcher';
document.body.appendChild(boutonTheme);
addEventListener('click', onClickThemeButton)
}
function onClickThemeButton(){
// Passer au thème suivant et appliquer la classe au body
// On suppose que themes_calsses est un tableau global
if (!window.currentThemeIndex && window.currentThemeIndex !== 0) {
// Initialiser l'index du thème courant en fonction de la classe actuelle du body
const currentClass = document.body.className;
window.currentThemeIndex = themes_calsses.indexOf(currentClass);
if (window.currentThemeIndex === -1) {
window.currentThemeIndex = 0;
}
}
// Passer au thème suivant
window.currentThemeIndex = (window.currentThemeIndex + 1) % themes_calsses.length;
// Appliquer la nouvelle classe au body
document.body.className = themes_calsses[window.currentThemeIndex];
}
});