Code a chrome extension where when you write gpt: and then the words you trigger that to…
- User
Code a chrome extension where when you write gpt: and then the words you trigger that to openai api as a prompt and generate the output
- Assistant
To create a Chrome extension that triggers OpenAI's API when you type "gpt:" followed by a prompt, follow these steps:
-
Set up an OpenAI API key:
- Sign up for an OpenAI account at https://openai.com/ if you haven't already.
- Go to the API keys page (https://platform.openai.com/account/api-keys) and create a new secret key. Save it securely.
-
Create the extension files:
- Create a new directory for your extension, e.g.,
gpt-chrome-extension. - Inside the directory, create the following files:
manifest.jsoncontent.jsbackground.jspopup.htmlpopup.js
- Create a new directory for your extension, e.g.,
-
Configure
manifest.json:json{ "name": "GPT Chrome Extension", "version": "1.0", "description": "Trigger OpenAI API with 'gpt:' prefix", "permissions": ["activeTab", "storage"], "background": { "scripts": ["background.js"] }, "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content.js"] } ], "browser_action": { "default_popup": "popup.html" }, "manifest_version": 2 } -
Implement
content.js:javascriptdocument.addEventListener('keyup', (event) => { if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') { const inputValue = event.target.value; if (inputValue.startsWith('gpt:')) { const prompt = inputValue.slice(4).trim(); chrome.runtime.sendMessage({ action: 'generateResponse', prompt }); } } });
-
- User
I want gpt: to be enabled in every platform like twotter tweet, github commit not only address bar
- Assistant
To enable the "gpt:" prefix in text inputs across different platforms like Twitter, GitHub, and others, you can modify the
content.jsfile as follows:javascriptdocument.addEventListener('DOMContentLoaded', () => { const observeMutations = (mutationsList) => { for (const mutation of mutationsList) { if (mutation.addedNodes) { mutation.addedNodes.forEach((node) => { if (node.nodeType === Node.ELEMENT_NODE && (node.tagName === 'INPUT' || node.tagName === 'TEXTAREA')) { node.addEventListener('input', (event) => { const inputValue = event.target.value; if (inputValue.startsWith('gpt:')) { const prompt = inputValue.slice(4).trim(); chrome.runtime.sendMessage({ action: 'generateResponse', prompt }); } }); } }); } } }; const observer = new MutationObserver(observeMutations); observer.observe(document, { childList: true, subtree: true }); });
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.