Lompat ke isi

MediaWiki:Common.js: Perbedaan antara revisi

Dari Mippedia Data
Tidak ada ringkasan suntingan
Tanda: Suntingan perangkat seluler Suntingan peramban seluler
Tidak ada ringkasan suntingan
Tanda: Dikembalikan Suntingan perangkat seluler Suntingan peramban seluler
Baris 1: Baris 1:
/* ==========================================================
/* ==========================================================
   🧠 MIPPEDIA DATA SMART-FALLBACK + AUTO-TRANSLATE + ORIGINAL TEXT
   🧠 MIPPEDIA DATA OMNI-SCRIPT (FINAL VERSION)
   Priority: ID -> EN (Translate + Toggle) -> CONCISE
   Features: Chaining, Translate, Voice, Mood Color, Toggle
   ========================================================== */
   ========================================================== */
(function() {
(function() {
Baris 7: Baris 7:
         var $descSection = $('#mip-desc-section');
         var $descSection = $('#mip-desc-section');
         var $descBox = $('#mip-auto-description');
         var $descBox = $('#mip-auto-description');
        var $voiceBtn = $('#mip-voice-btn');
         if (!$descBox.length) return;
         if (!$descBox.length) return;


Baris 17: Baris 18:
         ];
         ];


        // 🎨 Fungsi Mood Color (Deteksi Konten)
        function setMoodColor(text) {
            var mood = "#eaecf0";
            var lowerText = text.toLowerCase();
            if (lowerText.includes("musisi") || lowerText.includes("musik") || lowerText.includes("lagu")) mood = "#6a5acd";
            else if (lowerText.includes("sejarah") || lowerText.includes("peristiwa") || lowerText.includes("kerajaan")) mood = "#8b4513";
            else if (lowerText.includes("teknologi") || lowerText.includes("komputer") || lowerText.includes("internet") || lowerText.includes("situs")) mood = "#007bff";
            $descSection.css("border-left", "4px solid " + mood);
        }
        // 🔊 Fungsi Suara
        function speakText(text) {
            window.speechSynthesis.cancel();
            var msg = new SpeechSynthesisUtterance();
            msg.text = text;
            msg.lang = 'id-ID';
            msg.rate = 1.0;
            window.speechSynthesis.speak(msg);
        }
        // ✨ Fungsi Bersih-Bersih
         function cleanExtract(text) {
         function cleanExtract(text) {
             return text.replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim();
             return text.replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim();
         }
         }


        // 🌐 Fungsi Translate
         function translateText(text, callback) {
         function translateText(text, callback) {
             var translateUrl = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=id&dt=t&q=" + encodeURIComponent(text);
             var translateUrl = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=id&dt=t&q=" + encodeURIComponent(text);
             $.ajax({
             $.ajax({ url: translateUrl, success: function(res) {
                url: translateUrl,
                var translated = "";
                success: function(res) {
                res[0].forEach(function(s) { if (s[0]) translated += s[0]; });
                    var translated = "";
                callback(translated);
                    res[0].forEach(function(segment) {
            }, error: function() { callback(text); } });
                        if (segment[0]) translated += segment[0];
                    });
                    callback(translated);
                },
                error: function() { callback(text); }
            });
         }
         }


        // 🚀 Fungsi Utama (Chain Fetch)
         function fetchDescription(index) {
         function fetchDescription(index) {
             if (index >= projects.length) {
             if (index >= projects.length) { $descSection.hide(); return; }
                $descSection.hide();
                return;
            }


             var p = projects[index];
             var p = projects[index];
Baris 59: Baris 74:
                             var sourceLink = p.base + encodeURIComponent(pageTitle);
                             var sourceLink = p.base + encodeURIComponent(pageTitle);
                              
                              
                             // LOGIKA KHUSUS EN (DENGAN VERSI ASLI)
                             var renderUI = function(finalText, isTranslated) {
                                var html = '<div id="mip-text-content" style="margin-bottom: 12px;">' + finalText + '</div>' +
                                          '<div style="font-size: 0.9em; color: #777;">Sumber Dari : <a href="' + sourceLink + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + ' ↗️</a>';
                                if (isTranslated) {
                                    html += '<br><span style="font-size: 0.85em; font-style: italic;">(Diterjemahkan secara otomatis)</span> ' +
                                            '<span id="mip-toggle-orig" style="color: #6a5acd; cursor: pointer; text-decoration: underline; margin-left: 5px;">Tampilkan versi asli</span>';
                                }
                                html += '</div>';
                               
                                $descBox.hide().html(html).fadeIn(400);
                                $voiceBtn.show().off().on('click', function() { speakText($('#mip-text-content').text()); });
                                setMoodColor(finalText);
                            };
 
                             if (p.id === 'en') {
                             if (p.id === 'en') {
                                 translateText(extract, function(translated) {
                                 translateText(extract, function(trans) {  
                                     var formattedContent =
                                     renderUI(trans, true);
                                        '<div id="mip-text-content" style="margin-bottom: 12px;">' + translated + '</div>' +
                                        '<div style="font-size: 0.9em; color: #777;">' +
                                        'Sumber Dari : <a href="' + sourceLink + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + '.</a>' +
                                        '<br><span style="font-size: 0.85em; font-style: italic;">(Diterjemahkan secara otomatis dari bahasa Inggris)</span> ' +
                                        '<span id="mip-toggle-orig" style="color: #6a5acd; cursor: pointer; font-size: 0.9em; text-decoration: underline; margin-left: 5px;">Tampilkan versi asli</span>' +
                                        '</div>';
                                   
                                    $descBox.hide().html(formattedContent).fadeIn(400);
 
                                    // Event Klik Switch Text
                                     $(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() {
                                     $(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() {
                                         var $content = $('#mip-text-content');
                                         var $content = $('#mip-text-content');
                                         if ($(this).text() === 'Tampilkan versi asli') {
                                         if ($(this).text() === 'Tampilkan versi asli') { $content.text(extract); $(this).text('Tampilkan terjemahan'); }
                                            $content.text(extract);
                                         else { $content.text(trans); $(this).text('Tampilkan versi asli'); }
                                            $(this).text('Tampilkan terjemahan');
                                         } else {
                                            $content.text(translated);
                                            $(this).text('Tampilkan versi asli');
                                        }
                                     });
                                     });
                                 });
                                 });
                             } else {
                             } else { renderUI(extract, false); }
                                // NORMAL UNTUK ID & CONCISE
                                var formattedContent =
                                    '<div style="margin-bottom: 12px;">' + extract + '</div>' +
                                    '<div style="font-size: 0.9em; color: #777;">' +
                                    'Sumber Dari : <a href="' + sourceLink + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + '.</a>' +
                                    '</div>';
                                $descBox.hide().html(formattedContent).fadeIn(400);
                            }
                             return;  
                             return;  
                         }
                         }
Baris 104: Baris 109:
         fetchDescription(0);
         fetchDescription(0);


        // Tambah Style Shimmer
         if ($('#mip-shimmer-style').length === 0) {
         if ($('#mip-shimmer-style').length === 0) {
             $('<style id="mip-shimmer-style">').text(`
             $('<style id="mip-shimmer-style">').text(`

Revisi per 14 April 2026 23.20

/* ==========================================================
   🧠 MIPPEDIA DATA OMNI-SCRIPT (FINAL VERSION)
   Features: Chaining, Translate, Voice, Mood Color, Toggle
   ========================================================== */
(function() {
    $(document).ready(function() {
        var $descSection = $('#mip-desc-section');
        var $descBox = $('#mip-auto-description');
        var $voiceBtn = $('#mip-voice-btn');
        if (!$descBox.length) return;

        var pageTitle = mw.config.get('wgPageName');
        
        var projects = [
            { id: 'id', name: 'Mippedia bahasa Indonesia', url: 'https://id.mippedia.org/api.php', base: 'https://id.mippedia.org/wiki/' },
            { id: 'en', name: 'Mippedia bahasa Inggris', url: 'https://en.mippedia.org/api.php', base: 'https://en.mippedia.org/wiki/' },
            { id: 'concise', name: 'Mippedia bahasa Indonesia ringkas', url: 'https://concise.mippedia.org/api.php', base: 'https://concise.mippedia.org/wiki/' }
        ];

        // 🎨 Fungsi Mood Color (Deteksi Konten)
        function setMoodColor(text) {
            var mood = "#eaecf0"; 
            var lowerText = text.toLowerCase();
            if (lowerText.includes("musisi") || lowerText.includes("musik") || lowerText.includes("lagu")) mood = "#6a5acd";
            else if (lowerText.includes("sejarah") || lowerText.includes("peristiwa") || lowerText.includes("kerajaan")) mood = "#8b4513";
            else if (lowerText.includes("teknologi") || lowerText.includes("komputer") || lowerText.includes("internet") || lowerText.includes("situs")) mood = "#007bff";
            $descSection.css("border-left", "4px solid " + mood);
        }

        // 🔊 Fungsi Suara
        function speakText(text) {
            window.speechSynthesis.cancel();
            var msg = new SpeechSynthesisUtterance();
            msg.text = text;
            msg.lang = 'id-ID';
            msg.rate = 1.0;
            window.speechSynthesis.speak(msg);
        }

        // ✨ Fungsi Bersih-Bersih
        function cleanExtract(text) {
            return text.replace(/\{\{[^}]+\}\}/g, '').replace(/\(\s*\)/g, '').replace(/\s\s+/g, ' ').trim();
        }

        // 🌐 Fungsi Translate
        function translateText(text, callback) {
            var translateUrl = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=id&dt=t&q=" + encodeURIComponent(text);
            $.ajax({ url: translateUrl, success: function(res) {
                var translated = "";
                res[0].forEach(function(s) { if (s[0]) translated += s[0]; });
                callback(translated);
            }, error: function() { callback(text); } });
        }

        // 🚀 Fungsi Utama (Chain Fetch)
        function fetchDescription(index) {
            if (index >= projects.length) { $descSection.hide(); return; }

            var p = projects[index];
            $descBox.html('<div class="mip-loading-shimmer"></div>');
            $descSection.show();

            $.ajax({
                url: p.url,
                data: { action: 'query', prop: 'extracts', exintro: true, explaintext: true, titles: pageTitle, format: 'json', origin: '*' },
                dataType: 'json',
                success: function(data) {
                    var pages = data.query.pages;
                    var pageId = Object.keys(pages)[0];
                    
                    if (pageId != "-1") {
                        var extract = cleanExtract(pages[pageId].extract);
                        if (extract !== "") {
                            var sourceLink = p.base + encodeURIComponent(pageTitle);
                            
                            var renderUI = function(finalText, isTranslated) {
                                var html = '<div id="mip-text-content" style="margin-bottom: 12px;">' + finalText + '</div>' + 
                                           '<div style="font-size: 0.9em; color: #777;">Sumber Dari : <a href="' + sourceLink + '" target="_blank" style="color: #6a5acd; font-weight: bold; text-decoration: none;">' + p.name + ' ↗️</a>';
                                if (isTranslated) {
                                    html += '<br><span style="font-size: 0.85em; font-style: italic;">(Diterjemahkan secara otomatis)</span> ' +
                                            '<span id="mip-toggle-orig" style="color: #6a5acd; cursor: pointer; text-decoration: underline; margin-left: 5px;">Tampilkan versi asli</span>';
                                }
                                html += '</div>';
                                
                                $descBox.hide().html(html).fadeIn(400);
                                $voiceBtn.show().off().on('click', function() { speakText($('#mip-text-content').text()); });
                                setMoodColor(finalText);
                            };

                            if (p.id === 'en') {
                                translateText(extract, function(trans) { 
                                    renderUI(trans, true);
                                    $(document).off('click', '#mip-toggle-orig').on('click', '#mip-toggle-orig', function() {
                                        var $content = $('#mip-text-content');
                                        if ($(this).text() === 'Tampilkan versi asli') { $content.text(extract); $(this).text('Tampilkan terjemahan'); }
                                        else { $content.text(trans); $(this).text('Tampilkan versi asli'); }
                                    });
                                });
                            } else { renderUI(extract, false); }
                            return; 
                        }
                    }
                    fetchDescription(index + 1);
                },
                error: function() { fetchDescription(index + 1); }
            });
        }

        fetchDescription(0);

        // Tambah Style Shimmer
        if ($('#mip-shimmer-style').length === 0) {
            $('<style id="mip-shimmer-style">').text(`
                .mip-loading-shimmer { height: 16px; width: 100%; background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%); background-size: 200% 100%; animation: mip-shimmer 1.5s infinite; border-radius: 4px; }
                @keyframes mip-shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
            `).appendTo('head');
        }
    });
})();