File size: 4,630 Bytes
39dc472
53dd75c
39dc472
53dd75c
 
39dc472
 
 
 
 
 
 
 
53dd75c
 
39dc472
 
 
 
53dd75c
39dc472
 
 
 
 
 
 
53dd75c
 
39dc472
53dd75c
 
39dc472
53dd75c
 
 
 
 
39dc472
 
53dd75c
 
 
39dc472
53dd75c
39dc472
 
 
53dd75c
 
39dc472
 
 
 
 
 
 
 
 
 
 
53dd75c
39dc472
 
 
 
 
 
53dd75c
 
 
39dc472
53dd75c
39dc472
 
53dd75c
 
 
 
 
 
 
 
 
 
39dc472
53dd75c
39dc472
 
 
 
 
53dd75c
 
 
 
39dc472
 
 
 
53dd75c
 
39dc472
 
 
 
53dd75c
 
39dc472
53dd75c
39dc472
53dd75c
 
39dc472
 
 
53dd75c
39dc472
 
 
 
 
 
 
 
53dd75c
39dc472
 
 
 
53dd75c
 
39dc472
53dd75c
 
39dc472
2065625
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// ==UserScript==
// @name         Quiz Assistant (Lightning AI)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Reads questions and queries Lightning AI (Gemini 3.1 Pro)
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // --- 1. CONFIGURATION ---
    const LIGHTNING_API_KEY = "f42e9493-71bd-4d04-affd-55d4ee6ed60f"; 
    // UPDATE THIS: Find the CSS selector for the question text.
    const QUESTION_SELECTOR = ".loaded"; 

    // --- 2. CREATE MODAL UI ---
    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);

    // --- 3. KEYBOARD SHORTCUTS ---
    document.addEventListener('keydown', (e) => {
        // Show: Ctrl + M
        if (e.ctrlKey && e.key.toLowerCase() === 'm') {
            e.preventDefault();
            modal.style.display = 'block';
        }
        // Hide: Ctrl + H
        if (e.ctrlKey && e.key.toLowerCase() === 'h') {
            e.preventDefault();
            modal.style.display = 'none';
        }
    });

    // --- 4. LIGHTNING AI API LOGIC ---
    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();
            
            // Standard OpenAI-style response path
            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);
        }
    }

    // --- 5. AUTOMATION LOGIC ---
    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 });

    // Initial check
    setTimeout(checkForNewQuestion, 1000);

})();