|
|
| Line 1: |
Line 1: |
| /* Any JavaScript here will be loaded for all users on every page load. */ | | /* Any JavaScript here will be loaded for all users on every page load. */ |
| /* NanoFab Chat Assistant — embed iframe widget on every page */
| |
| (function () {
| |
| if (window.self !== window.top) return;
| |
|
| |
| var action = mw.config.get('wgAction');
| |
| if (action && action !== 'view') return;
| |
|
| |
| var CHAT_URL = 'https://nanofab-assistant.tailf13c08.ts.net';
| |
|
| |
| var btn = document.createElement('button');
| |
| btn.id = 'nanofab-chat-btn';
| |
| btn.setAttribute('aria-label', 'Open NanoFab Assistant');
| |
| btn.textContent = 'Chat';
| |
|
| |
| var frameWrap = document.createElement('div');
| |
| frameWrap.id = 'nanofab-chat-wrap';
| |
| frameWrap.style.display = 'none';
| |
|
| |
| var iframe = document.createElement('iframe');
| |
| iframe.src = CHAT_URL;
| |
| iframe.title = 'NanoFab Assistant';
| |
| iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-forms');
| |
| iframe.setAttribute('loading', 'lazy');
| |
| frameWrap.appendChild(iframe);
| |
|
| |
| // Greeting speech bubble — lives entirely on the wiki page (not inside the
| |
| // iframe), so this is the only "pop up" text this code can actually control.
| |
| var greeting = document.createElement('div');
| |
| greeting.id = 'nanofab-chat-greeting';
| |
| greeting.innerHTML = "Hi! I'm your NanoFab chatbot \u2014 ask me any question!" +
| |
| '<button id="nanofab-chat-greeting-close" aria-label="Dismiss">\u00d7</button>';
| |
|
| |
| document.body.appendChild(frameWrap);
| |
| document.body.appendChild(btn);
| |
| document.body.appendChild(greeting);
| |
|
| |
| function openChat() {
| |
| frameWrap.style.display = 'block';
| |
| btn.textContent = 'Close';
| |
| greeting.style.display = 'none';
| |
| }
| |
| function closeChat() {
| |
| frameWrap.style.display = 'none';
| |
| btn.textContent = 'Chat';
| |
| }
| |
|
| |
| btn.addEventListener('click', function () {
| |
| var isOpen = frameWrap.style.display === 'block';
| |
| if (isOpen) { closeChat(); } else { openChat(); }
| |
| });
| |
|
| |
| greeting.addEventListener('click', function () {
| |
| openChat();
| |
| });
| |
| greeting.querySelector('#nanofab-chat-greeting-close').addEventListener('click', function (e) {
| |
| e.stopPropagation();
| |
| greeting.style.display = 'none';
| |
| });
| |
|
| |
| // Show the greeting bubble ~1.5s after the page loads.
| |
| setTimeout(function () {
| |
| greeting.style.display = 'block';
| |
| }, 1500);
| |
| })();
| |