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

From DQWiki
Jump to navigationJump to search
mNo edit summary
m (update to more robust script)
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Lorekeeper Standalone Terminal v2 */
/* Lorekeeper Widget Loader - v1.1.4 (MediaWiki Hardened Loader) */
(function() {
(function () {
     function initLorekeeper() {
     if (window.__lorekeeperWidgetLoaded) return;
        if (document.getElementById('lore-terminal-btn')) return;
    window.__lorekeeperWidgetLoaded = true;


        // 1. Create Floating Button
    var APP_ORIGIN = 'https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app';
        var btn = document.createElement('div');
    var APP_WIDGET_URL = APP_ORIGIN + '/widget';
        btn.id = 'lore-terminal-btn';
    var MOBILE_BREAKPOINT = 768;
        btn.innerHTML = '?';
    var CONTAINER_ID = 'lorekeeper-widget-container';
        btn.style.cssText = 'position:fixed !important; bottom:30px !important; right:30px !important; width:60px; height:60px; background:#8b5cf6; border-radius:50%; display:flex; align-items:center; justify-content:center; font-size:30px; cursor:pointer; box-shadow:0 10px 25px rgba(139,92,246,0.6); z-index:9999999 !important; border:2px solid rgba(255,255,255,0.3); transition: transform 0.2s ease;';
    var IFRAME_ID = 'lorekeeper-widget-frame';
       
        // 2. Create Chat Box
        var box = document.createElement('div');
        box.id = 'lore-terminal-box';
        box.style.cssText = 'position:fixed !important; bottom:100px !important; right:30px !important; width:400px; height:600px; background:rgb(15,15,20); border:1px solid rgba(139,92,246,0.5); border-radius:24px; display:none; flex-direction:column; box-shadow:0 25px 60px rgba(0,0,0,0.8); overflow:hidden; z-index:9999999 !important; color:#ffffff; font-family: sans-serif;';
       
        box.innerHTML =
            '<div style="padding:15px 20px; background:rgba(139,92,246,0.15); border-bottom:1px solid rgba(139,92,246,0.3); display:flex; justify-content:space-between; align-items:center;">' +
                '<div style="font-weight:bold; color:#a78bfa; font-size:16px;">Archives of the Western Kingdom</div>' +
                '<div id="lore-close" style="cursor:pointer; font-size:20px; color:#fff; padding:5px;">?</div>' +
            '</div>' +
            '<div id="lore-chat" style="flex:1; overflow-y:auto; padding:20px; display:flex; flex-direction:column; gap:12px; font-size:14px; line-height:1.5;">' +
                '<div style="background:rgba(255,255,255,0.08); padding:12px 16px; border-radius:12px; border-left:4px solid #8b5cf6;">Greetings, Scribe. What knowledge do you seek from the archives?</div>' +
            '</div>' +
            '<div style="padding:15px; background:rgba(0,0,0,0.3); border-top:1px solid rgba(255,255,255,0.1); display:flex; gap:10px;">' +
                '<input id="lore-input" placeholder="Ask about the Iron Guild..." style="flex:1; background:rgba(255,255,255,0.05); border:1px solid rgba(139,92,246,0.3); padding:10px 14px; border-radius:10px; color:#fff; outline:none; font-size:14px;">' +
                '<button id="lore-send" style="background:#8b5cf6; border:none; color:#fff; padding:0 18px; border-radius:10px; cursor:pointer; font-weight:bold; font-size:14px;">Ask</button>' +
            '</div>';


        document.body.appendChild(btn);
    function isMobileViewport() {
         document.body.appendChild(box);
         return window.innerWidth < MOBILE_BREAKPOINT;
    }


         // Events
    function getUsername() {
        var chat = document.getElementById('lore-chat');
         try {
         var input = document.getElementById('lore-input');
            return (mw && mw.config && mw.config.get('wgUserName')) || '';
        var send = document.getElementById('lore-send');
         } catch (err) {
         var convId = 'anon-' + Math.random().toString(36).substring(7);
            return '';
         }
    }


        btn.onclick = function() {
    function buildIframeUrl() {
            box.style.display = (box.style.display === 'none' || box.style.display === '') ? 'flex' : 'none';
        var params = [];
         };
         var username = getUsername();
        document.getElementById('lore-close').onclick = function() { box.style.display = 'none'; };


         async function handleAsk() {
         if (username) {
             var text = input.value.trim();
             params.push('user=' + encodeURIComponent(username));
            if (!text) return;
        } else {
             input.value = '';
             params.push('guest=' + encodeURIComponent('Traveler'));
        }


            // User UI
        if (isMobileViewport()) {
             var uDiv = document.createElement('div');
             params.push('isMobile=true');
            uDiv.style.cssText = 'align-self:flex-end; background:#8b5cf6; padding:10px 14px; border-radius:12px; max-width:85%; font-weight:500;';
        }
            uDiv.textContent = text;
            chat.appendChild(uDiv);
            chat.scrollTop = chat.scrollHeight;


            // AI UI Placeholder
        params.push('parentOrigin=' + encodeURIComponent(window.location.origin));
            var aDiv = document.createElement('div');
            aDiv.style.cssText = 'background:rgba(255,255,255,0.05); padding:12px 16px; border-radius:12px; max-width:92%; border-left:4px solid #8b5cf6; white-space: pre-wrap;';
            aDiv.innerHTML = '<i>Searching the archives...</i>';
            chat.appendChild(aDiv);


            try {
        return APP_WIDGET_URL + '?' + params.join('&');
                const response = await fetch('https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app/api/lore/stream', {
    }
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ question: text, conversationId: convId })
                });


                const reader = response.body.getReader();
    function setCollapsedSize(container) {
                const decoder = new TextDecoder();
        if (isMobileViewport()) {
                aDiv.textContent = '';
            container.style.width = '80px';
                var content = '';
            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;
        }


                while (true) {
        var iframe = document.getElementById(IFRAME_ID);
                    const { done, value } = await reader.read();
        if (iframe) {
                    if (done) break;
            iframe.setAttribute('data-is-mobile', isMobileViewport() ? 'true' : 'false');
                    content += decoder.decode(value);
                    aDiv.textContent = content; // Typewriter
                    chat.scrollTop = chat.scrollHeight;
                }
            } catch (e) {
                aDiv.innerHTML = '<span style="color:#f87171;">Connection to archives lost.</span>';
            }
         }
         }


         send.onclick = handleAsk;
         window.addEventListener('message', handleMessage, false);
         input.onkeydown = function(e) { if (e.key === 'Enter') handleAsk(); };
         window.addEventListener('resize', handleWindowResize, false);
     }
     }


    // Wait for the document to be ready
     if (document.readyState === 'loading') {
     if (document.readyState === "complete" || document.readyState === "interactive") {
         document.addEventListener('DOMContentLoaded', init, { once: true });
         initLorekeeper();
     } else {
     } else {
         window.addEventListener("DOMContentLoaded", initLorekeeper);
         init();
     }
     }
})();
})();

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();
    }
})();