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

From DQWiki
Jump to navigationJump to search
mNo edit summary
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Lorekeeper Standalone Terminal v4 - Premium Streaming */
/* Lorekeeper Widget Loader - v1.1.3 (Hardened Responsive Iframe) */
(function() {
(function() {
     var checkCount = 0;
     var username = mw.config.get('wgUserName');
     var initTimer = setInterval(function() {
    var isMobileParent = window.innerWidth < 768; // Measure the true Wiki window size
         if (document.body) {
   
            clearInterval(initTimer);
    // Uses the correct Firebase Hosted App URL
            setupLore();
    var iframeUrl = 'https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app/widget';
        }
   
        if (checkCount++ > 100) clearInterval(initTimer);
     var params = [];
     }, 100);
    if (username) {
         params.push('user=' + encodeURIComponent(username));
    } else {
        params.push('guest=Traveler');
    }
   
    // Tell the iframe if the user is ACTUALLY on mobile
    if (isMobileParent) {
        params.push('isMobile=true');
    }
   
    if (params.length > 0) {
        iframeUrl += '?' + params.join('&');
     }


     function setupLore() {
     // Create a container that doesn't block the Wiki
        if (document.getElementById('lore-terminal-btn')) return;
    var widgetContainer = document.createElement('div');
    widgetContainer.id = 'lorekeeper-widget-container';
    widgetContainer.style.cssText = 'position:fixed; bottom:0; right:0; z-index:99999; pointer-events:none; transition: width 0.3s ease, height 0.3s ease;';
    widgetContainer.style.width = isMobileParent ? '80px' : '300px';
    widgetContainer.style.height = isMobileParent ? '80px' : '120px';


        // 1. Inject Styles
    var iframe = document.createElement('iframe');
        var style = document.createElement('style');
    iframe.src = iframeUrl;
        style.innerHTML =  
    iframe.style.cssText = 'width:100%; height:100%; border:none; background:transparent; pointer-events:auto;';
            '#lore-chat::-webkit-scrollbar { width: 4px; }' +
    iframe.setAttribute('allow', 'clipboard-write'); // For links/sharing later
            '#lore-chat::-webkit-scrollbar-thumb { background: #8b5cf6; border-radius: 10px; }' +
            '.lore-msg { margin-bottom: 15px; padding: 12px 16px; border-radius: 15px; font-size: 14px; position: relative; animation: fadeIn 0.3s ease; }' +
            '.lore-user { background: #8b5cf6; color: #fff; align-self: flex-end; margin-left: 40px; border-bottom-right-radius: 4px; }' +
            '.lore-ai { background: #1a1a24; color: #e2e8f0; align-self: flex-start; margin-right: 40px; border-left: 4px solid #8b5cf6; border-bottom-left-radius: 4px; }' +
            '@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }';
        document.head.appendChild(style);


        // 2. Create Floating Button
    widgetContainer.appendChild(iframe);
        var btn = document.createElement('div');
    document.body.appendChild(widgetContainer);
        btn.id = 'lore-terminal-btn';
        btn.innerHTML = '?';
        btn.setAttribute('style', 'position:fixed !important; bottom:25px !important; right:25px !important; width:65px !important; height:65px !important; background:linear-gradient(135deg, #8b5cf6, #6d28d9) !important; border-radius:50% !important; display:flex !important; align-items:center !important; justify-content:center !important; font-size:32px !important; cursor:pointer !important; box-shadow:0 10px 30px rgba(139,92,246,0.5) !important; z-index:999999 !important; border:2px solid rgba(255,255,255,0.4) !important; transition:0.3s ease;');


        // 3. Create Chat Box
    // Listen for the widget telling the Wiki to "Grow" and "Shrink"
        var box = document.createElement('div');
    window.addEventListener('message', function(event) {
         box.id = 'lore-terminal-box';
         // Enforce secure origin check for the correct App Hosting domain
         box.setAttribute('style', 'position:fixed !important; bottom:105px !important; right:25px !important; width:420px !important; height:650px !important; background:rgba(10,10,15,0.98) !important; backdrop-filter:blur(15px); border:1px solid rgba(139,92,246,0.4) !important; border-radius:30px !important; display:none; flex-direction:column !important; box-shadow:0 30px 60px rgba(0,0,0,0.8) !important; z-index:999999 !important; color:#fff !important; font-family:system-ui, sans-serif !important; overflow:hidden;');
         if (event.origin !== 'https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app') return;
          
          
         box.innerHTML =
         if (event.data.type === 'lorekeeper-resize') {
            '<div style="padding:20px; border-bottom:1px solid rgba(139,92,246,0.2); background:rgba(139,92,246,0.1); display:flex; justify-content:space-between; align-items:center;">' +
             var w = event.data.width;
                '<div><b style="color:#a78bfa; font-size:16px;">The Lorekeeper</b><br><small style="opacity:0.6; font-size:10px; text-transform:uppercase; letter-spacing:1px;">Royal Archives</small></div>' +
            var h = event.data.height;
                '<span id="lore-close" style="cursor:pointer; font-size:24px; opacity:0.5;">&times;</span>' +
             // Handle both pixel numbers (desktop) and string values like "100vw" (mobile)
            '</div>' +
            widgetContainer.style.width = (typeof w === 'number') ? w + 'px' : w;
            '<div id="lore-chat" style="flex:1; overflow-y:auto; padding:20px; display:flex; flex-direction:column; gap:5px;">' +
            widgetContainer.style.height = (typeof h === 'number') ? h + 'px' : h;
                '<div class="lore-msg lore-ai">Greetings, Traveler. I am the Scribe of the Western Kingdom. What knowledge do you seek from the archives?</div>' +
             '</div>' +
            '<div style="padding:20px; background:rgba(0,0,0,0.4); border-top:1px solid rgba(255,255,255,0.05); display:flex; gap:10px;">' +
                '<input id="lore-input" placeholder="Inquire about the world..." style="flex:1; background:rgba(255,255,255,0.05); border:1px solid rgba(139,92,246,0.3); padding:12px 18px; border-radius:15px; color:#fff; outline:none; font-size:14px;">' +
                '<button id="lore-send" style="background:#8b5cf6; color:#fff; border:none; width:50px; height:45px; border-radius:12px; cursor:pointer; font-weight:bold; font-size:18px;">&rarr;</button>' +
             '</div>';
 
        document.body.appendChild(btn);
        document.body.appendChild(box);
 
        var input = document.getElementById('lore-input');
        var chat = document.getElementById('lore-chat');
        var convId = 'anon-' + Math.random().toString(36).substring(7);
 
        btn.onclick = function() { box.style.display = (box.style.display == 'none') ? 'flex' : 'none'; };
        document.getElementById('lore-close').onclick = function() { box.style.display = 'none'; };
 
        function addMessage(text, isAi) {
            var m = document.createElement('div');
            m.className = 'lore-msg ' + (isAi ? 'lore-ai' : 'lore-user');
            m.innerText = text;
            chat.appendChild(m);
            chat.scrollTop = chat.scrollHeight;
            return m;
         }
         }
    });


        async function handleAsk() {
    // Optional: Resync if they resize the window wildly (desktop to mobile mode live)
            var q = input.value.trim();
    window.addEventListener('resize', function() {
            if (!q) return;
        var newIsMobile = window.innerWidth < 768;
            input.value = '';
        if (newIsMobile !== isMobileParent) {
            addMessage(q, false);
            isMobileParent = newIsMobile;
 
             // You could reload the iframe here, but keeping it simple is usually safest.
            var aMsg = addMessage('', true);
            aMsg.innerHTML = '<i style="opacity:0.5">Consulting the Scribe Notes...</i>';
 
            try {
                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: q, conversationId: convId })
                });
 
                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                aMsg.innerText = '';
                var content = '';
 
                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;
                    content += decoder.decode(value);
                    aMsg.innerText = content;
                    chat.scrollTop = chat.scrollHeight;
                }
             } catch (e) { aMsg.innerText = "The archives are sealed for now. Please try again later."; }
         }
         }
 
    });
        document.getElementById('lore-send').onclick = handleAsk;
        input.onkeydown = function(e) { if (e.key === 'Enter') handleAsk(); };
    }
})();
})();

Latest revision as of 08:07, 1 March 2026

/* Lorekeeper Widget Loader - v1.1.3 (Hardened Responsive Iframe) */
(function() {
    var username = mw.config.get('wgUserName');
    var isMobileParent = window.innerWidth < 768; // Measure the true Wiki window size
    
    // Uses the correct Firebase Hosted App URL
    var iframeUrl = 'https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app/widget';
    
    var params = [];
    if (username) {
        params.push('user=' + encodeURIComponent(username));
    } else {
        params.push('guest=Traveler');
    }
    
    // Tell the iframe if the user is ACTUALLY on mobile
    if (isMobileParent) {
        params.push('isMobile=true');
    }
    
    if (params.length > 0) {
        iframeUrl += '?' + params.join('&');
    }

    // Create a container that doesn't block the Wiki
    var widgetContainer = document.createElement('div');
    widgetContainer.id = 'lorekeeper-widget-container';
    widgetContainer.style.cssText = 'position:fixed; bottom:0; right:0; z-index:99999; pointer-events:none; transition: width 0.3s ease, height 0.3s ease;';
    widgetContainer.style.width = isMobileParent ? '80px' : '300px'; 
    widgetContainer.style.height = isMobileParent ? '80px' : '120px';

    var iframe = document.createElement('iframe');
    iframe.src = iframeUrl;
    iframe.style.cssText = 'width:100%; height:100%; border:none; background:transparent; pointer-events:auto;';
    iframe.setAttribute('allow', 'clipboard-write'); // For links/sharing later

    widgetContainer.appendChild(iframe);
    document.body.appendChild(widgetContainer);

    // Listen for the widget telling the Wiki to "Grow" and "Shrink"
    window.addEventListener('message', function(event) {
        // Enforce secure origin check for the correct App Hosting domain
        if (event.origin !== 'https://npc-forge--npc-forge-qh4gc.asia-southeast1.hosted.app') return;
        
        if (event.data.type === 'lorekeeper-resize') {
            var w = event.data.width;
            var h = event.data.height;
            // Handle both pixel numbers (desktop) and string values like "100vw" (mobile)
            widgetContainer.style.width = (typeof w === 'number') ? w + 'px' : w;
            widgetContainer.style.height = (typeof h === 'number') ? h + 'px' : h;
        }
    });

    // Optional: Resync if they resize the window wildly (desktop to mobile mode live)
    window.addEventListener('resize', function() {
        var newIsMobile = window.innerWidth < 768;
        if (newIsMobile !== isMobileParent) {
            isMobileParent = newIsMobile;
            // You could reload the iframe here, but keeping it simple is usually safest.
        }
    });
})();