diff --git a/apps/chess extension/background.html b/apps/chess extension/background.html deleted file mode 100644 index 90abd75..0000000 --- a/apps/chess extension/background.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - Background Page - - - - - \ No newline at end of file diff --git a/apps/chess extension/background.js b/apps/chess extension/background.js deleted file mode 100644 index 834a594..0000000 --- a/apps/chess extension/background.js +++ /dev/null @@ -1,145 +0,0 @@ -console.log("Background script loaded"); - -let lastTabId = null; -let copiedText = null; -let readyTabs = new Set(); - -function isValidTab(tabId) { - return new Promise((resolve) => { - chrome.tabs.get(tabId, (tab) => { - if (chrome.runtime.lastError) { - resolve(false); - } else { - resolve(!tab.url.startsWith("chrome://")); - } - }); - }); -} - -function injectContentScript(tabId) { - return new Promise(async (resolve, reject) => { - if (!(await isValidTab(tabId))) { - console.log("Skipping script injection for invalid or chrome:// URL"); - resolve(false); - return; - } - - chrome.scripting.executeScript({ - target: { tabId: tabId }, - files: ['content.js'] - }, () => { - if (chrome.runtime.lastError) { - console.error('Error injecting script: ', chrome.runtime.lastError.message); - reject(chrome.runtime.lastError); - } else { - console.log('Content script injected successfully'); - resolve(true); - } - }); - }); -} - -function sendMessageToTab(tabId, message) { - return new Promise((resolve, reject) => { - chrome.tabs.sendMessage(tabId, { ...message, from: 'background' }, response => { - if (chrome.runtime.lastError) { - console.error('Error sending message to tab: ', chrome.runtime.lastError.message); - reject(chrome.runtime.lastError); - } else { - console.log('Message sent successfully, response: ', response); - resolve(response); - } - }); - }); -} - -async function ensureContentScriptLoaded(tabId) { - if (!readyTabs.has(tabId)) { - try { - const injected = await injectContentScript(tabId); - if (!injected) { - console.log("Content script not injected. Skipping this tab."); - return false; - } - await new Promise(resolve => setTimeout(resolve, 100)); // Short delay to ensure script is ready - await sendMessageToTab(tabId, { action: 'ping' }); - readyTabs.add(tabId); - return true; - } catch (error) { - console.error('Error ensuring content script is loaded:', error); - return false; - } - } - return true; -} - -async function handleTabChange(tabId) { - console.log("Handling tab change for tab:", tabId); - - if (lastTabId !== null && lastTabId !== tabId) { - const sourceTabValid = await isValidTab(lastTabId); - const destTabValid = await isValidTab(tabId); - - if (sourceTabValid) { - try { - const sourceScriptLoaded = await ensureContentScriptLoaded(lastTabId); - if (sourceScriptLoaded) { - console.log("Attempting to copy from tab:", lastTabId); - const copyResponse = await sendMessageToTab(lastTabId, {action: "copy"}); - - if (copyResponse && copyResponse.text) { - copiedText = copyResponse.text; - console.log("Text copied:", copiedText); - } else if (copyResponse && copyResponse.error) { - console.log("Copy error:", copyResponse.error); - } - } else { - console.log("Skipping copy operation. Content script not loaded in source tab."); - } - } catch (error) { - console.error("Error during copy operation:", error); - } - } else { - console.log("Skipping copy operation. Source tab is not valid."); - } - - if (destTabValid && copiedText) { - try { - const destScriptLoaded = await ensureContentScriptLoaded(tabId); - if (destScriptLoaded) { - console.log("Attempting to paste to tab:", tabId); - const pasteResponse = await sendMessageToTab(tabId, {action: "paste", text: copiedText}); - - if (pasteResponse && pasteResponse.success) { - console.log("Text pasted successfully"); - } else { - console.log("No suitable element found for pasting or paste verification failed"); - } - } else { - console.log("Skipping paste operation. Content script not loaded in destination tab."); - } - } catch (error) { - console.error("Error during paste operation:", error); - } - } else { - console.log("Skipping paste operation. Destination tab is not valid or no text to paste."); - } - } - - lastTabId = tabId; -} - -chrome.tabs.onActivated.addListener(function(activeInfo) { - console.log("Tab activated:", activeInfo.tabId); - handleTabChange(activeInfo.tabId).catch(error => { - console.error("Error in tab activation handler:", error); - }); -}); - -chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { - if (message.action === 'contentScriptReady') { - console.log('Content script ready in tab:', sender.tab.id); - readyTabs.add(sender.tab.id); - sendResponse({received: true}); - } -}); \ No newline at end of file diff --git a/apps/chess extension/content.js b/apps/chess extension/content.js deleted file mode 100644 index b8d4c6d..0000000 --- a/apps/chess extension/content.js +++ /dev/null @@ -1,116 +0,0 @@ -console.log("Content script loaded"); - -function performCopy() { - if (window.location.href.includes('chat.openai.com')) { - const flexDivs = document.querySelectorAll('.flex.max-w-full.flex-col.flex-grow'); - if (flexDivs.length > 0) { - const lastFlexDiv = flexDivs[flexDivs.length - 1]; - const text = lastFlexDiv.innerText; - console.log("Copying text from ChatGPT:", text); - return { text: text }; - } - } else { - const notationDisplay = document.getElementById('notationDisplay'); - if (notationDisplay) { - const text = notationDisplay.textContent || notationDisplay.innerText; - const items = text.trim().split(/\s+/); - const lastItem = items[items.length - 1]; - if (lastItem) { - console.log("Copying text:", lastItem); - return { text: lastItem }; - } - } - } - console.log("No suitable element found for copying"); - return { error: "No suitable element found for copying" }; -} - -function triggerInputEvent(element) { - const inputEvent = new Event('input', { bubbles: true, cancelable: true }); - element.dispatchEvent(inputEvent); -} - -function performPaste(text) { - if (window.location.href.includes('chat.openai.com')) { - const promptTextarea = document.getElementById('prompt-textarea'); - if (promptTextarea && text) { - promptTextarea.value = text; - triggerInputEvent(promptTextarea); - - if (promptTextarea.value !== text) { - console.log("Paste verification failed for ChatGPT. Expected:", text, "Actual:", promptTextarea.value); - return { error: "Paste verification failed" }; - } - - console.log("Text pasted successfully to ChatGPT"); - - setTimeout(() => { - const sendButton = document.querySelector('button[data-testid="send-button"]'); - if (sendButton) { - sendButton.click(); - console.log("Send button clicked for ChatGPT"); - } else { - console.log("Send button not found for ChatGPT"); - } - }, 100); - - return { success: true }; - } else { - console.log("ChatGPT textarea not found or no text to paste"); - return { error: "ChatGPT textarea not found or no text to paste" }; - } - } else { - const notationInput = document.getElementById('notationInput'); - if (notationInput && text) { - notationInput.value = text; - triggerInputEvent(notationInput); - - if (notationInput.value !== text) { - console.log("Paste verification failed for notationInput. Expected:", text, "Actual:", notationInput.value); - return { error: "Paste verification failed" }; - } - - console.log("Text pasted successfully to notationInput"); - - setTimeout(() => { - const submitMoveButton = document.querySelector('button[onclick="makeMove()"]'); - if (submitMoveButton) { - submitMoveButton.click(); - console.log("Submit Move button clicked"); - } else { - console.log("Submit Move button not found"); - } - }, 100); - - return { success: true }; - } else { - console.log("notationInput not found or no text to paste"); - return { error: "notationInput not found or no text to paste" }; - } - } -} - -chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { - console.log("Message received in content script:", request); - - if (request.from === 'background') { - if (request.action === "ping") { - sendResponse({ loaded: true }); - } else if (request.action === "copy") { - sendResponse(performCopy()); - } else if (request.action === "paste") { - sendResponse(performPaste(request.text)); - } - } - return true; -}); - -chrome.runtime.sendMessage({ action: 'contentScriptReady' }, response => { - if (chrome.runtime.lastError) { - console.error('Error notifying background script:', chrome.runtime.lastError); - } else { - console.log('Background script notified of content script ready'); - } -}); - -Version 32 of 32 \ No newline at end of file diff --git a/apps/chess extension/icon48.png b/apps/chess extension/icon48.png deleted file mode 100644 index ba6830d..0000000 Binary files a/apps/chess extension/icon48.png and /dev/null differ diff --git a/apps/chess extension/manifest.json b/apps/chess extension/manifest.json deleted file mode 100644 index b5c7bd0..0000000 --- a/apps/chess extension/manifest.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "manifest_version": 3, - "name": "Chatbot Chess", - "version": "1.0", - "description": "Automates copying pasting between a chatbot and a simple chess web page", - "permissions": [ - "tabs", - "storage", - "scripting" - ], - "host_permissions": [ - "" - ], - "background": { - "service_worker": "background.js", - "type": "module" - }, - "action": {} -} \ No newline at end of file