From e12983d6308f70ebfa0f6f8b1d0a4e6d694a4704 Mon Sep 17 00:00:00 2001 From: lencx Date: Fri, 12 Jan 2024 22:44:08 +0800 Subject: [PATCH] chore: extensions --- extensions/noi-ask/README.md | 1 + extensions/noi-ask/main.js | 133 ++++++++++++++++++ extensions/noi-ask/manifest.json | 21 +++ extensions/noi-export-chatgpt/README.md | 1 + .../main.js | 0 .../manifest.json | 3 +- .../style.css | 0 7 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 extensions/noi-ask/README.md create mode 100644 extensions/noi-ask/main.js create mode 100644 extensions/noi-ask/manifest.json create mode 100644 extensions/noi-export-chatgpt/README.md rename extensions/{noi-chatgpt => noi-export-chatgpt}/main.js (100%) rename extensions/{noi-chatgpt => noi-export-chatgpt}/manifest.json (66%) rename extensions/{noi-chatgpt => noi-export-chatgpt}/style.css (100%) diff --git a/extensions/noi-ask/README.md b/extensions/noi-ask/README.md new file mode 100644 index 0000000..2008b32 --- /dev/null +++ b/extensions/noi-ask/README.md @@ -0,0 +1 @@ +# Noi Ask diff --git a/extensions/noi-ask/main.js b/extensions/noi-ask/main.js new file mode 100644 index 0000000..335ff8b --- /dev/null +++ b/extensions/noi-ask/main.js @@ -0,0 +1,133 @@ +class NoiAsk { + static sync(message) { + const inputElement = document.querySelector(`textarea`); + if (inputElement) { + const nativeTextareaSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set; + nativeTextareaSetter.call(inputElement, message); + const inputEvent = new InputEvent('input', { + bubbles: true, + cancelable: true, + }); + inputElement.dispatchEvent(inputEvent); + } + } + + static autoClick(btn) { + btn.focus(); + btn.disabled = false; + btn.click(); + } +} + +class OpenAIAsk extends NoiAsk { + static name = 'ChatGPT'; + static url = 'https://chat.openai.com'; + + static submit() { + const btn = document.querySelector(`button[data-testid="send-button"]`); + if (btn) this.autoClick(btn); + } +} + +class PoeAsk extends NoiAsk { + static name = 'Poe'; + static url = 'https://poe.com'; + + static submit() { + const btn = document.querySelectorAll(`button[class*="ChatMessageSendButton_sendButton"]`)[0]; + if (btn) this.autoClick(btn); + } +} + +class ClaudeAsk extends NoiAsk { + static name = 'Claude'; + static url = 'https://claude.ai'; + + static sync(message) { + const inputElement = document.querySelector(`div.ProseMirror`); + if (inputElement) { + inputElement.focus(); + inputElement.innerHTML = ''; + document.execCommand('insertText', false, message); + } + } + + static submit() { + // subsequent screens use this + let btn = document.querySelector(`button[aria-label*="Send Message"]`); + if (!btn) { // new chats use this + btn = document.querySelector(`button:has(div svg)`); + } + if (!btn) { // last ditch attempt + btn = document.querySelector(`button:has(svg)`); + } + if (btn) this.autoClick(btn); + } +} + +class BardAsk extends NoiAsk { + static name = 'Bard'; + static url = 'https://bard.google.com'; + + static sync(message) { + const inputElement = document.querySelector(`.ql-editor.textarea`); + if (inputElement) { + const inputEvent = new Event('input', { bubbles: true }); + inputElement.value = message; + inputElement.dispatchEvent(inputEvent); + // bard is weird + inputElement.querySelector('p').textContent = message; + } + } + + static submit() { + const btn = document.querySelector(`button[aria-label*="Send message"]`); + if (btn) { + btn.setAttribute('aria-disabled', 'false'); // doesn't work alone + btn.focus(); + btn.click(); + } + } +} + +class HuggingChatAsk extends NoiAsk { + static name = 'HuggingChat'; + static url = 'https://huggingface.co/chat'; + + static sync(message) { + var inputElement = document.querySelector(`textarea[placeholder*="Ask anything"]`); + if (inputElement) { + const inputEvent = new Event('input', { bubbles: true }); + inputElement.value = message; + inputElement.dispatchEvent(inputEvent); + } + } + + static submit() { + var btn = document.querySelector(`form.relative > div > button[type="submit"]`); + if (btn) this.autoClick(btn); + } +} + +class PerplexityAsk extends NoiAsk { + static name = 'Perplexity'; + static url = 'https://www.perplexity.ai'; + + static submit() { + const btns = Array.from(document.querySelectorAll(`button.bg-super`)); + if (btns[0]) { + const btnsWithSvgPath = btns.filter(button => button.querySelector('svg path')); + const btn = btnsWithSvgPath[btnsWithSvgPath.length - 1]; + btn.click(); + } + } +} + +window.NoiAsk = { + OpenAIAsk, + PoeAsk, + ClaudeAsk, + BardAsk, + HuggingChatAsk, + PerplexityAsk, +}; diff --git a/extensions/noi-ask/manifest.json b/extensions/noi-ask/manifest.json new file mode 100644 index 0000000..14d1a50 --- /dev/null +++ b/extensions/noi-ask/manifest.json @@ -0,0 +1,21 @@ +{ + "manifest_version": 3, + "name": "@noi/ask", + "version": "0.1.0", + "homepage": "https://github.com/lencx/Noi/tree/main/extensions/noi-ask", + "content_scripts": [ + { + "matches": [ + "https://chat.openai.com/*", + "https://bard.google.com/*", + "https://poe.com/*", + "https://claude.ai/*", + "https://huggingface.co/chat/*", + "https://www.perplexity.ai/*" + ], + "js": ["main.js"], + "run_at": "document_end", + "world": "MAIN" + } + ] +} \ No newline at end of file diff --git a/extensions/noi-export-chatgpt/README.md b/extensions/noi-export-chatgpt/README.md new file mode 100644 index 0000000..b6fa9a6 --- /dev/null +++ b/extensions/noi-export-chatgpt/README.md @@ -0,0 +1 @@ +# Noi Export - ChatGPT diff --git a/extensions/noi-chatgpt/main.js b/extensions/noi-export-chatgpt/main.js similarity index 100% rename from extensions/noi-chatgpt/main.js rename to extensions/noi-export-chatgpt/main.js diff --git a/extensions/noi-chatgpt/manifest.json b/extensions/noi-export-chatgpt/manifest.json similarity index 66% rename from extensions/noi-chatgpt/manifest.json rename to extensions/noi-export-chatgpt/manifest.json index 6a49f58..95c6383 100644 --- a/extensions/noi-chatgpt/manifest.json +++ b/extensions/noi-export-chatgpt/manifest.json @@ -1,7 +1,8 @@ { "manifest_version": 3, - "name": "@noi/chatgpt", + "name": "@noi/export-chatgpt", "version": "0.1.0", + "homepage": "https://github.com/lencx/Noi/tree/main/extensions/noi-export-chatgpt", "content_scripts": [ { "matches": ["https://*.openai.com/*"], diff --git a/extensions/noi-chatgpt/style.css b/extensions/noi-export-chatgpt/style.css similarity index 100% rename from extensions/noi-chatgpt/style.css rename to extensions/noi-export-chatgpt/style.css