Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name New script - amcforum.wiki
- // @namespace Violentmonkey Scripts
- // @match https://amcforum.wiki/chat/c/*
- // @grant GM_xmlhttpRequest
- // @version 1.0
- // @author -
- // @description 3/10/2023, 8:17:49 pm
- // ==/UserScript==
- (function() {
- // Constants
- const API_URL = "https://api.openai.com/v1/chat/completions";
- const API_KEY = "sk-apikey";
- const MODEL = "gpt-3.5-turbo";
- const SYSTEM_PROMPT = `You are Grammarly, an AI Language model which fixes spelling mistakes and punctuation.
- Do NOT try to interfere with anything else.
- Do NOT give AI Vibes, just reframe like a 13 year old texting.
- JUST give the reframed message, nothing more.`;
- let HISTORY = [];
- const HISTORY_LENGTH = 3;
- // Function to update HISTORY array with chat messages
- const updateHistory = () => {
- const chatMessages = document.querySelectorAll('.chat-message-text p');
- HISTORY = [];
- // Populate HISTORY array from bottom to top
- for (let i = chatMessages.length - 1; i >= 0; i--) {
- const role = 'user';
- const content = chatMessages[i].textContent;
- HISTORY.push({ role, content });
- }
- HISTORY = HISTORY.slice(0, HISTORY_LENGTH);
- };
- // Function to fetch AI-generated text from OpenAI API
- const fetchAIResponse = async (userMessage) => {
- return fetch(
- API_URL,
- {
- body: JSON.stringify({
- "model": MODEL,
- "messages": [
- {
- "role": "system",
- "content": SYSTEM_PROMPT
- },
- ...HISTORY,
- {
- "role": "user",
- "content": `Reframe this message: ${userMessage}`
- }
- ],
- "temperature": 0,
- "max_tokens": 500
- }),
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- "Authorization": "Bearer " + API_KEY
- }
- }
- ).then(response => {
- if (response.ok) {
- return response.json();
- } else {
- throw new Error('Failed to fetch AI response');
- }
- }).then(json => {
- return json['choices'][0]['message']['content'];
- });
- };
- // Function to modify textarea value and send
- const modifyAndSend = async (event) => {
- const textarea = document.querySelector('.chat-composer__input');
- const sendButton = document.querySelector('.chat-composer-button.-send');
- if (textarea && sendButton) {
- // Stop the event propagation and prevent the default action
- event.stopImmediatePropagation();
- event.preventDefault();
- // Store the original value and disable textarea, then show loading
- const originalValue = textarea.value;
- textarea.disabled = true;
- textarea.value = 'Loading...';
- // Update HISTORY before sending a new message
- updateHistory();
- try {
- // Fetch AI-generated text and update the textarea value
- const aiResponse = await fetchAIResponse(originalValue);
- textarea.value = aiResponse;
- } catch (error) {
- console.error(error);
- textarea.value = originalValue;
- }
- // Re-enable textarea
- textarea.disabled = false;
- // Trigger an input event to update the UI
- const inputEvent = new Event('input', {
- 'bubbles': true,
- 'cancelable': true
- });
- textarea.dispatchEvent(inputEvent);
- // Send the message
- // sendButton.click();
- }
- };
- // Listen for the keydown event on the textarea
- document.addEventListener('keydown', function(event) {
- if (event.target.matches('.chat-composer__input') && event.key === 'Enter') {
- modifyAndSend(event);
- }
- }, true); // Use capture phase to handle the event as soon as possible
- })();
Advertisement
Add Comment
Please, Sign In to add comment