MediaWiki:Common.js: differenze tra le versioni

Da Wikiliscio.
Nessun oggetto della modifica
Nessun oggetto della modifica
Riga 4: Riga 4:


// CODICE PER IL TRANSLATE BUTTON
// CODICE PER IL TRANSLATE BUTTON
// Aggiungi il bottone quando il DOM è pronto
mw.loader.using('mediawiki.util', function() {
document.addEventListener("DOMContentLoaded", function() {
     console.log("Common.js is loaded");
     console.log("DOM fully loaded and parsed");


     // Crea il bottone
     // Aggiungi il bottone quando il DOM è pronto
     var translateButton = document.createElement("button");
     $(document).ready(function() {
    translateButton.textContent = "Traduci Pagina";
        console.log("DOM fully loaded and parsed");
    translateButton.style.position = "fixed";
    translateButton.style.bottom = "10px";
    translateButton.style.right = "10px";
    translateButton.style.zIndex = "1000";
    translateButton.style.padding = "10px";
    translateButton.style.backgroundColor = "#007bff";
    translateButton.style.color = "#ffffff";
    translateButton.style.border = "none";
    translateButton.style.borderRadius = "5px";
    translateButton.style.cursor = "pointer";


    console.log("Button created");
        // Crea il bottone
        var translateButton = document.createElement("button");
        translateButton.textContent = "Traduci Pagina";
        translateButton.style.position = "fixed";
        translateButton.style.bottom = "10px";
        translateButton.style.right = "10px";
        translateButton.style.zIndex = "1000";
        translateButton.style.padding = "10px";
        translateButton.style.backgroundColor = "#007bff";
        translateButton.style.color = "#ffffff";
        translateButton.style.border = "none";
        translateButton.style.borderRadius = "5px";
        translateButton.style.cursor = "pointer";


    // Aggiungi l'evento click al bottone
        console.log("Button created");
    translateButton.addEventListener("click", function() {
 
        var currentUrl = window.location.href;
        // Aggiungi l'evento click al bottone
        var userLang = navigator.language || navigator.userLanguage; // Ottieni la lingua del browser
        translateButton.addEventListener("click", function() {
        var targetLanguage = userLang.split('-')[0]; // Prendi solo la parte principale della lingua (es. "it" da "it-IT")
            var currentUrl = window.location.href;
        var translateUrl = "https://translate.google.com/translate?hl=&sl=auto&tl=" + targetLanguage + "&u=" + encodeURIComponent(currentUrl);
            var userLang = navigator.language || navigator.userLanguage; // Ottieni la lingua del browser
        window.open(translateUrl, '_blank');
            var targetLanguage = userLang.split('-')[0]; // Prendi solo la parte principale della lingua (es. "it" da "it-IT")
        console.log("Button clicked, translating to: " + targetLanguage);
            var translateUrl = "https://translate.google.com/translate?hl=&sl=auto&tl=" + targetLanguage + "&u=" + encodeURIComponent(currentUrl);
    });
            window.open(translateUrl, '_blank');
            console.log("Button clicked, translating to: " + targetLanguage);
        });


    console.log("Event listener added");
        console.log("Event listener added");


    // Aggiungi il bottone al corpo della pagina
        // Aggiungi il bottone al corpo della pagina
    document.body.appendChild(translateButton);
        document.body.appendChild(translateButton);
    console.log("Button appended to body");
        console.log("Button appended to body");
    });
});
});

Versione delle 13:19, 3 lug 2024

/* Any JavaScript here will be loaded for all users on every page load. */

console.error('TEST EX')

// CODICE PER IL TRANSLATE BUTTON
mw.loader.using('mediawiki.util', function() {
    console.log("Common.js is loaded");

    // Aggiungi il bottone quando il DOM è pronto
    $(document).ready(function() {
        console.log("DOM fully loaded and parsed");

        // Crea il bottone
        var translateButton = document.createElement("button");
        translateButton.textContent = "Traduci Pagina";
        translateButton.style.position = "fixed";
        translateButton.style.bottom = "10px";
        translateButton.style.right = "10px";
        translateButton.style.zIndex = "1000";
        translateButton.style.padding = "10px";
        translateButton.style.backgroundColor = "#007bff";
        translateButton.style.color = "#ffffff";
        translateButton.style.border = "none";
        translateButton.style.borderRadius = "5px";
        translateButton.style.cursor = "pointer";

        console.log("Button created");

        // Aggiungi l'evento click al bottone
        translateButton.addEventListener("click", function() {
            var currentUrl = window.location.href;
            var userLang = navigator.language || navigator.userLanguage; // Ottieni la lingua del browser
            var targetLanguage = userLang.split('-')[0]; // Prendi solo la parte principale della lingua (es. "it" da "it-IT")
            var translateUrl = "https://translate.google.com/translate?hl=&sl=auto&tl=" + targetLanguage + "&u=" + encodeURIComponent(currentUrl);
            window.open(translateUrl, '_blank');
            console.log("Button clicked, translating to: " + targetLanguage);
        });

        console.log("Event listener added");

        // Aggiungi il bottone al corpo della pagina
        document.body.appendChild(translateButton);
        console.log("Button appended to body");
    });
});