User:Jono Bean/common.js: Difference between revisions

From DQWiki
Jump to navigationJump to search
mNo edit summary
m (update to more robust script)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Lorekeeper Standalone Terminal v3 - Failsafe ES5 */
/* Lorekeeper Widget Loader - v1.1.4 (MediaWiki Hardened Loader) */
(function() {
(function () {
     var checkCount = 0;
    if (window.__lorekeeperWidgetLoaded) return;
     var initTimer = setInterval(function() {
    window.__lorekeeperWidgetLoaded = true;
         checkCount++;
 
        if (document.body) {
    var APP_ORIGIN = 'https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app';
             clearInterval(initTimer);
    var APP_WIDGET_URL = APP_ORIGIN + '/widget';
            setupLore();
    var MOBILE_BREAKPOINT = 768;
     var CONTAINER_ID = 'lorekeeper-widget-container';
     var IFRAME_ID = 'lorekeeper-widget-frame';
 
    function isMobileViewport() {
         return window.innerWidth < MOBILE_BREAKPOINT;
    }
 
    function getUsername() {
        try {
             return (mw && mw.config && mw.config.get('wgUserName')) || '';
        } catch (err) {
            return '';
         }
         }
        if (checkCount > 100) clearInterval(initTimer); // Stop trying after 10s
     }
     }, 100);


     function setupLore() {
     function buildIframeUrl() {
         if (document.getElementById('lore-terminal-btn')) return;
         var params = [];
        var username = getUsername();


         // 1. Create Floating Button
         if (username) {
        var btn = document.createElement('div');
            params.push('user=' + encodeURIComponent(username));
         btn.id = 'lore-terminal-btn';
         } else {
        btn.innerHTML = '&#10024;'; // Sparkles icon
            params.push('guest=' + encodeURIComponent('Traveler'));
        btn.setAttribute('style', 'position:fixed !important; bottom:20px !important; right:20px !important; width:60px !important; height:60px !important; background:#8b5cf6 !important; border-radius:50% !important; display:flex !important; align-items:center !important; justify-content:center !important; font-size:30px !important; cursor:pointer !important; box-shadow:0 10px 25px rgba(0,0,0,0.5) !important; z-index:999999 !important; border:2px solid #fff !important;');
        }


         // 2. Create Box
         if (isMobileViewport()) {
        var box = document.createElement('div');
             params.push('isMobile=true');
        box.id = 'lore-terminal-box';
        }
        box.setAttribute('style', 'position:fixed !important; bottom:90px !important; right:20px !important; width:380px !important; height:550px !important; background:#0f0f14 !important; border:2px solid #8b5cf6 !important; border-radius:15px !important; display:none; flex-direction:column !important; box-shadow:0 20px 50px rgba(0,0,0,0.8) !important; z-index:999999 !important; color:#fff !important; font-family:sans-serif !important;');
       
        box.innerHTML =
            '<div style="padding:15px; border-bottom:1px solid #333; display:flex; justify-content:space-between; align-items:center;">' +
                '<b style="color:#a78bfa">Lorekeeper Terminal</b>' +
                '<span id="lore-close" style="cursor:pointer; padding:5px;">&times;</span>' +
             '</div>' +
            '<div id="lore-chat" style="flex:1; overflow-y:auto; padding:15px; font-size:13px; line-height:1.4;">' +
                '<div style="background:#1a1a24; padding:10px; border-radius:8px; margin-bottom:10px;">The archives are ready. What is your inquiry?</div>' +
            '</div>' +
            '<div style="padding:15px; border-top:1px solid #333; display:flex;">' +
                '<input id="lore-input" type="text" style="flex:1; background:#000; color:#fff; border:1px solid #444; padding:8px; border-radius:5px; outline:none;">' +
                '<button id="lore-send" style="margin-left:5px; background:#8b5cf6; color:#fff; border:none; padding:8px 15px; border-radius:5px; cursor:pointer;">Ask</button>' +
            '</div>';


         document.body.appendChild(btn);
         params.push('parentOrigin=' + encodeURIComponent(window.location.origin));
        document.body.appendChild(box);


         // Logic
         return APP_WIDGET_URL + '?' + params.join('&');
        btn.onclick = function() {
    }
            box.style.display = (box.style.display == 'none') ? 'flex' : 'none';
        };
        document.getElementById('lore-close').onclick = function() { box.style.display = 'none'; };


         var input = document.getElementById('lore-input');
    function setCollapsedSize(container) {
         var chat = document.getElementById('lore-chat');
        if (isMobileViewport()) {
            container.style.width = '80px';
            container.style.height = '80px';
        } else {
            container.style.width = '300px';
            container.style.height = '120px';
        }
    }
 
    function ensureContainer() {
         var existing = document.getElementById(CONTAINER_ID);
        if (existing) return existing;
 
         var container = document.createElement('div');
        container.id = CONTAINER_ID;
        container.style.position = 'fixed';
        container.style.bottom = '0';
        container.style.right = '0';
        container.style.zIndex = '99999';
        container.style.pointerEvents = 'none';
        container.style.overflow = 'hidden';
        container.style.background = 'transparent';
        container.style.transition = 'width 0.3s ease, height 0.3s ease';
        setCollapsedSize(container);


         document.getElementById('lore-send').onclick = function() {
         return container;
            var val = input.value.trim();
    }
            if (!val) return;
            input.value = '';


            var uMsg = document.createElement('div');
    function ensureIframe() {
             uMsg.style.cssText = 'margin-bottom:10px; background:#8b5cf6; padding:8px; border-radius:8px; align-self:flex-end;';
        var existing = document.getElementById(IFRAME_ID);
             uMsg.innerText = val;
        if (existing) return existing;
             chat.appendChild(uMsg);
 
        var iframe = document.createElement('iframe');
        iframe.id = IFRAME_ID;
        iframe.src = buildIframeUrl();
        iframe.style.width = '100%';
        iframe.style.height = '100%';
        iframe.style.border = 'none';
        iframe.style.background = 'transparent';
        iframe.style.pointerEvents = 'auto';
        iframe.setAttribute('allow', 'clipboard-write');
        iframe.setAttribute('loading', 'lazy');
        iframe.setAttribute('referrerpolicy', 'strict-origin-when-cross-origin');
 
        return iframe;
    }
 
    function mountWidget() {
        if (!document.body) return false;
 
        var container = ensureContainer();
        var iframe = ensureIframe();
 
        if (!container.parentNode) {
            container.appendChild(iframe);
            document.body.appendChild(container);
        } else if (!iframe.parentNode) {
            container.appendChild(iframe);
        }
 
        return true;
    }
 
    function applyResize(container, width, height) {
        if (!container) return;
 
        if (typeof width === 'number') {
            container.style.width = width + 'px';
        } else if (typeof width === 'string' && width) {
            container.style.width = width;
        }
 
        if (typeof height === 'number') {
            container.style.height = height + 'px';
        } else if (typeof height === 'string' && height) {
             container.style.height = height;
        }
    }
 
    function handleMessage(event) {
        if (event.origin !== APP_ORIGIN) return;
        if (!event.data || event.data.type !== 'lorekeeper-resize') return;
 
        var container = document.getElementById(CONTAINER_ID);
        if (!container) return;
 
        applyResize(container, event.data.width, event.data.height);
    }
 
    function handleWindowResize() {
        var container = document.getElementById(CONTAINER_ID);
        var iframe = document.getElementById(IFRAME_ID);
        if (!container || !iframe) return;
 
        var wasMobile = iframe.getAttribute('data-is-mobile') === 'true';
        var nowMobile = isMobileViewport();
 
        if (wasMobile !== nowMobile) {
            iframe.setAttribute('data-is-mobile', nowMobile ? 'true' : 'false');
            iframe.src = buildIframeUrl();
            setCollapsedSize(container);
            return;
        }
 
        if (
            container.style.width === '80px' ||
            container.style.width === '300px' ||
            container.style.width === '100vw'
        ) {
            setCollapsedSize(container);
        }
    }
 
    function init() {
        if (!mountWidget()) {
             window.setTimeout(init, 50);
            return;
        }
 
        var iframe = document.getElementById(IFRAME_ID);
        if (iframe) {
             iframe.setAttribute('data-is-mobile', isMobileViewport() ? 'true' : 'false');
        }


            var aMsg = document.createElement('div');
        window.addEventListener('message', handleMessage, false);
            aMsg.style.cssText = 'margin-bottom:10px; background:#222; padding:10px; border-radius:8px; border-left:3px solid #8b5cf6;';
        window.addEventListener('resize', handleWindowResize, false);
            aMsg.innerText = 'Consulting the records...';
    }
            chat.appendChild(aMsg);


            // Using simple AJAX for compatibility
    if (document.readyState === 'loading') {
            var xhr = new XMLHttpRequest();
        document.addEventListener('DOMContentLoaded', init, { once: true });
            xhr.open("POST", "https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app/api/askLore", true);
    } else {
            xhr.setRequestHeader("Content-Type", "application/json");
        init();
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        try {
                            var response = JSON.parse(xhr.responseText);
                            aMsg.innerText = response.text || response.data.text;
                        } catch(e) { aMsg.innerText = "Error parsing response."; }
                    } else {
                        aMsg.innerText = "API Error: " + xhr.status;
                    }
                }
            };
            xhr.send(JSON.stringify({ prompt: val }));
        };
     }
     }
})();
})();

Latest revision as of 04:59, 3 April 2026

/* Lorekeeper Widget Loader - v1.1.4 (MediaWiki Hardened Loader) */
(function () {
    if (window.__lorekeeperWidgetLoaded) return;
    window.__lorekeeperWidgetLoaded = true;

    var APP_ORIGIN = 'https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app';
    var APP_WIDGET_URL = APP_ORIGIN + '/widget';
    var MOBILE_BREAKPOINT = 768;
    var CONTAINER_ID = 'lorekeeper-widget-container';
    var IFRAME_ID = 'lorekeeper-widget-frame';

    function isMobileViewport() {
        return window.innerWidth < MOBILE_BREAKPOINT;
    }

    function getUsername() {
        try {
            return (mw && mw.config && mw.config.get('wgUserName')) || '';
        } catch (err) {
            return '';
        }
    }

    function buildIframeUrl() {
        var params = [];
        var username = getUsername();

        if (username) {
            params.push('user=' + encodeURIComponent(username));
        } else {
            params.push('guest=' + encodeURIComponent('Traveler'));
        }

        if (isMobileViewport()) {
            params.push('isMobile=true');
        }

        params.push('parentOrigin=' + encodeURIComponent(window.location.origin));

        return APP_WIDGET_URL + '?' + params.join('&');
    }

    function setCollapsedSize(container) {
        if (isMobileViewport()) {
            container.style.width = '80px';
            container.style.height = '80px';
        } else {
            container.style.width = '300px';
            container.style.height = '120px';
        }
    }

    function ensureContainer() {
        var existing = document.getElementById(CONTAINER_ID);
        if (existing) return existing;

        var container = document.createElement('div');
        container.id = CONTAINER_ID;
        container.style.position = 'fixed';
        container.style.bottom = '0';
        container.style.right = '0';
        container.style.zIndex = '99999';
        container.style.pointerEvents = 'none';
        container.style.overflow = 'hidden';
        container.style.background = 'transparent';
        container.style.transition = 'width 0.3s ease, height 0.3s ease';
        setCollapsedSize(container);

        return container;
    }

    function ensureIframe() {
        var existing = document.getElementById(IFRAME_ID);
        if (existing) return existing;

        var iframe = document.createElement('iframe');
        iframe.id = IFRAME_ID;
        iframe.src = buildIframeUrl();
        iframe.style.width = '100%';
        iframe.style.height = '100%';
        iframe.style.border = 'none';
        iframe.style.background = 'transparent';
        iframe.style.pointerEvents = 'auto';
        iframe.setAttribute('allow', 'clipboard-write');
        iframe.setAttribute('loading', 'lazy');
        iframe.setAttribute('referrerpolicy', 'strict-origin-when-cross-origin');

        return iframe;
    }

    function mountWidget() {
        if (!document.body) return false;

        var container = ensureContainer();
        var iframe = ensureIframe();

        if (!container.parentNode) {
            container.appendChild(iframe);
            document.body.appendChild(container);
        } else if (!iframe.parentNode) {
            container.appendChild(iframe);
        }

        return true;
    }

    function applyResize(container, width, height) {
        if (!container) return;

        if (typeof width === 'number') {
            container.style.width = width + 'px';
        } else if (typeof width === 'string' && width) {
            container.style.width = width;
        }

        if (typeof height === 'number') {
            container.style.height = height + 'px';
        } else if (typeof height === 'string' && height) {
            container.style.height = height;
        }
    }

    function handleMessage(event) {
        if (event.origin !== APP_ORIGIN) return;
        if (!event.data || event.data.type !== 'lorekeeper-resize') return;

        var container = document.getElementById(CONTAINER_ID);
        if (!container) return;

        applyResize(container, event.data.width, event.data.height);
    }

    function handleWindowResize() {
        var container = document.getElementById(CONTAINER_ID);
        var iframe = document.getElementById(IFRAME_ID);
        if (!container || !iframe) return;

        var wasMobile = iframe.getAttribute('data-is-mobile') === 'true';
        var nowMobile = isMobileViewport();

        if (wasMobile !== nowMobile) {
            iframe.setAttribute('data-is-mobile', nowMobile ? 'true' : 'false');
            iframe.src = buildIframeUrl();
            setCollapsedSize(container);
            return;
        }

        if (
            container.style.width === '80px' ||
            container.style.width === '300px' ||
            container.style.width === '100vw'
        ) {
            setCollapsedSize(container);
        }
    }

    function init() {
        if (!mountWidget()) {
            window.setTimeout(init, 50);
            return;
        }

        var iframe = document.getElementById(IFRAME_ID);
        if (iframe) {
            iframe.setAttribute('data-is-mobile', isMobileViewport() ? 'true' : 'false');
        }

        window.addEventListener('message', handleMessage, false);
        window.addEventListener('resize', handleWindowResize, false);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init, { once: true });
    } else {
        init();
    }
})();