| |
| |
| |
| |
| |
| |
| |
| |
|
|
| (function() { |
| 'use strict'; |
|
|
| |
| const LIGHTNING_API_KEY = "f42e9493-71bd-4d04-affd-55d4ee6ed60f"; |
| |
| const QUESTION_SELECTOR = ".loaded"; |
|
|
| |
| const modal = document.createElement('div'); |
| modal.id = 'lightning-assistant-modal'; |
| Object.assign(modal.style, { |
| position: 'fixed', |
| top: '20px', |
| right: '20px', |
| width: '400px', |
| maxHeight: '80vh', |
| overflowY: 'auto', |
| backgroundColor: '#121212', |
| color: '#e0e0e0', |
| padding: '20px', |
| borderRadius: '12px', |
| boxShadow: '0 10px 30px rgba(0,0,0,0.5)', |
| zIndex: '999999', |
| display: 'none', |
| fontFamily: 'Segoe UI, Tahoma, Geneva, Verdana, sans-serif', |
| fontSize: '14px', |
| lineHeight: '1.5', |
| border: '1px solid #333' |
| }); |
| |
| const title = document.createElement('div'); |
| title.innerHTML = '⚡ <strong>Lightning Assistant</strong>'; |
| title.style.marginBottom = '15px'; |
| title.style.paddingBottom = '10px'; |
| title.style.borderBottom = '1px solid #333'; |
| modal.appendChild(title); |
|
|
| const contentDiv = document.createElement('div'); |
| contentDiv.id = 'lightning-response-content'; |
| contentDiv.innerText = 'Listening for questions...'; |
| modal.appendChild(contentDiv); |
|
|
| document.body.appendChild(modal); |
|
|
| |
| document.addEventListener('keydown', (e) => { |
| |
| if (e.ctrlKey && e.key.toLowerCase() === 'm') { |
| e.preventDefault(); |
| modal.style.display = 'block'; |
| } |
| |
| if (e.ctrlKey && e.key.toLowerCase() === 'h') { |
| e.preventDefault(); |
| modal.style.display = 'none'; |
| } |
| }); |
|
|
| |
| async function queryLightning(questionText) { |
| contentDiv.innerHTML = '<i>Processing question...</i>'; |
| |
| const url = "https://lightning.ai/api/v1/chat/completions"; |
| |
| const payload = { |
| model: "google/gemini-3.1-pro-preview", |
| messages: [ |
| { |
| role: "user", |
| content: [ |
| { |
| type: "text", |
| text: `Answer this quiz question concisely. Provide the correct option or a short explanation: ${questionText}` |
| } |
| ] |
| } |
| ] |
| }; |
|
|
| try { |
| const response = await fetch(url, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'Authorization': `Bearer ${LIGHTNING_API_KEY}` |
| }, |
| body: JSON.stringify(payload) |
| }); |
|
|
| if (!response.ok) { |
| const errorData = await response.json(); |
| throw new Error(errorData.error?.message || `Status: ${response.status}`); |
| } |
|
|
| const data = await response.json(); |
| |
| |
| const answer = data.choices[0].message.content; |
| |
| contentDiv.innerHTML = `<strong>Answer:</strong><br>${answer.replace(/\n/g, '<br>')}`; |
| } catch (error) { |
| contentDiv.innerHTML = `<span style="color:#ff6b6b;">Error: ${error.message}</span>`; |
| console.error("Lightning API Error:", error); |
| } |
| } |
|
|
| |
| let lastProcessedQuestion = ""; |
|
|
| function checkForNewQuestion() { |
| const questionElement = document.querySelector(QUESTION_SELECTOR); |
| if (questionElement) { |
| const currentQuestionText = questionElement.innerText.trim(); |
| if (currentQuestionText && currentQuestionText !== lastProcessedQuestion) { |
| lastProcessedQuestion = currentQuestionText; |
| queryLightning(currentQuestionText); |
| } |
| } |
| } |
|
|
| const observer = new MutationObserver(checkForNewQuestion); |
| observer.observe(document.body, { childList: true, subtree: true, characterData: true }); |
|
|
| |
| setTimeout(checkForNewQuestion, 1000); |
|
|
| })(); |