Screenshot → OCR → AI answer + manual text box. Safe JSON parsing.
// ==UserScript== // @name AI Answerer // @namespace http://tampermonkey.net/ // @version 12.21.1 // @description Screenshot → OCR → AI answer + manual text box. Safe JSON parsing. // @license MIT // @author Bradley // @match *://*/* // @grant GM_addStyle // @grant GM_xmlhttpRequest // @connect api.sambanova.ai // @require https://cdn.jsdelivr.net/npm/[email protected]/dist/html2canvas.min.js // @require https://cdn.jsdelivr.net/npm/[email protected]/dist/tesseract.min.js // ==/UserScript== /* MIT License Copyright (c) 2026 Bradley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function () { const API_KEY = "2eb724bd-7532-4847-961b-1db757aedcb4"; const API_URL = "https://api.sambanova.ai/v1/chat/completions"; const MODEL = "Meta-Llama-3.1-8B-Instruct"; GM_addStyle(` #ai-btn { position: fixed; bottom: 20px; right: 20px; background:#4a6cff; color:white; padding:10px 16px; border-radius:999px; cursor:pointer; z-index:999999; font-family:sans-serif; font-size:14px; border:none; box-shadow:0 2px 8px rgba(0,0,0,0.25); } #ai-box { position: absolute; bottom: 70px; right: 20px; width: 360px; background:white; border-radius:10px; box-shadow:0 4px 16px rgba(0,0,0,0.25); z-index:999999; font-family:sans-serif; display:flex; flex-direction:column; overflow:hidden; } #ai-head { background:#eef0ff; padding:8px 12px; font-weight:600; display:flex; justify-content:space-between; cursor: move; } #ai-close { cursor:pointer; font-weight:700; } #ai-body { padding:10px 12px; font-size:13px; } #ai-status { font-size:12px; color:#666; margin-bottom:6px; } #ai-answer { border:1px solid #ddd; padding:8px; border-radius:6px; background:#fafafa; white-space:pre-wrap; max-height:200px; overflow:auto; } `); function createUI() { if (!document.getElementById("ai-btn")) { const btn = document.createElement("button"); btn.id = "ai-btn"; btn.textContent = "Ask AI"; btn.onclick = runOCR; document.body.appendChild(btn); } if (!document.getElementById("ai-box")) { const box = document.createElement("div"); box.id = "ai-box"; box.innerHTML = ` <div id="ai-head"> <span>AI Answer</span> <span id="ai-close">✕</span> </div> <div id="ai-body"> <div id="ai-status"></div> <textarea id="ai-input" placeholder="Type your question..." style="width:100%;height:60px;margin-bottom:6px;padding:6px;border:1px solid #ccc;border-radius:6px;"></textarea> <button id="ai-send" style="width:100%;padding:8px;background:#4a6cff;color:white;border:none;border-radius:6px;margin-bottom:8px;cursor:pointer;"> Send to AI </button> <div id="ai-answer">Press the button to begin.</div> </div> `; document.body.appendChild(box); document.getElementById("ai-close").onclick = () => box.remove(); makeDraggable(box); document.getElementById("ai-send").onclick = async () => { const text = document.getElementById("ai-input").value.trim(); if (!text) return setAnswer("Type something first."); setStatus("Sending..."); const reply = await askAI(text); setStatus("Done."); setAnswer(reply); }; } } function makeDraggable(el) { let offsetX = 0, offsetY = 0, dragging = false; el.addEventListener("mousedown", e => { dragging = true; offsetX = e.clientX - el.offsetLeft; offsetY = e.clientY - el.offsetTop; }); document.addEventListener("mousemove", e => { if (!dragging) return; el.style.left = (e.clientX - offsetX) + "px"; el.style.top = (e.clientY - offsetY) + "px"; }); document.addEventListener("mouseup", () => dragging = false); } async function runOCR() { createUI(); try { setStatus("Capturing screen..."); const canvas = await html2canvas(document.body); const img = canvas.toDataURL("image/png"); setStatus("Running OCR..."); const result = await Tesseract.recognize(img, "eng"); const text = result.data.text.trim(); if (!text) { setStatus("No text found."); setAnswer("OCR found nothing."); return; } setStatus("Sending to AI..."); const reply = await askAI(text); setStatus("Done."); setAnswer(reply); } catch (err) { setStatus("Error."); setAnswer(String(err)); } } function setStatus(msg) { const el = document.getElementById("ai-status"); if (el) el.textContent = msg; } function setAnswer(msg) { const el = document.getElementById("ai-answer"); if (el) el.textContent = msg; } function askAI(text) { return new Promise((resolve) => { GM_xmlhttpRequest({ method: "POST", url: API_URL, headers: { "Content-Type": "application/json", "Authorization": "Bearer " + API_KEY }, data: JSON.stringify({ model: MODEL, max_tokens: 1024, messages: [ { role: "system", content: "Interpret OCR text and answer clearly." }, { role: "user", content: text } ] }), // ✅ SAFE JSON FIX — ONLY CHANGE YOU ASKED FOR onload: function (response) { const text = response.responseText; try { const json = JSON.parse(text); resolve(json.choices?.[0]?.message?.content || "(No response)"); } catch (e) { resolve("Invalid JSON received:\n\n" + text); } }, onerror: function (err) { resolve("Request failed:\n" + JSON.stringify(err)); } }); }); } createUI(); })();