Last active
November 25, 2023 08:17
-
-
Save nicosuave/e06a4f7d5fc442958c57491e13bce535 to your computer and use it in GitHub Desktop.
Zoomer Boomer Tweet Translate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Zoomer/Boomer Speak Converter | |
// @version 0.1 | |
// @description Translate tweets to zoomer or boomer speak with enhanced UI, response handling, and retry mechanism | |
// @author Nico Ritschel | |
// @match *://twitter.com/* | |
// @grant GM_xmlhttpRequest | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
console.log("[Zoomer/Boomer Tweet Converter] Script initiated."); | |
function addTranslateButtons() { | |
const tweets = document.querySelectorAll('[data-testid="tweetText"]:not(.converter-button-added)'); | |
tweets.forEach(tweet => { | |
const translateZoomerButton = createTranslateButton('Zoomer', tweet); | |
const translateBoomerButton = createTranslateButton('Boomer', tweet); | |
tweet.parentElement.appendChild(translateZoomerButton); | |
tweet.parentElement.appendChild(translateBoomerButton); | |
tweet.classList.add('converter-button-added'); | |
}); | |
} | |
function createTranslateButton(type, tweet) { | |
const button = document.createElement('span'); | |
button.innerText = `Translate to ${type}`; | |
button.style.color = 'rgb(29, 155, 240)'; // Change color | |
button.style.cursor = 'pointer'; | |
button.style.paddingTop = '5px'; | |
button.style.fontFamily = 'Arial, sans-serif'; | |
button.style.fontSize = '0.8em'; | |
button.onclick = (event) => { | |
event.preventDefault(); | |
convertTweet(tweet, button, type, 0); | |
}; | |
return button; | |
} | |
function convertTweet(tweetElement, translateButton, type, attempt) { | |
const maxAttempts = 2; | |
if (attempt >= maxAttempts) { | |
translateButton.innerText = `Error translating to ${type}. Retry?`; | |
console.error(`[Zoomer/Boomer Tweet Converter] Error translating to ${type}.`); | |
return; | |
} | |
translateButton.innerText = `Translating to ${type}...`; | |
const originalText = tweetElement.textContent; | |
const payload = { | |
model: "mistral", | |
prompt: getPromptByType(type, originalText), | |
stream: false | |
}; | |
console.log(`[Zoomer/Boomer Tweet Converter] Sending request for ${type}:`, JSON.stringify(payload)); | |
GM_xmlhttpRequest({ | |
method: "POST", | |
url: 'http://localhost:11434/api/generate', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
data: JSON.stringify(payload), | |
onload: function(response) { | |
try { | |
let jsonResponse = JSON.parse(response.responseText); | |
let convertedText = jsonResponse.response; | |
convertedText = convertedText.replace(/^['"]|['"]$/g, '').trim(); | |
tweetElement.textContent = convertedText; | |
translateButton.innerText = `Translated to ${type}`; | |
translateButton.style.cursor = 'default'; | |
translateButton.onclick = null; // Remove the click event | |
} catch (error) { | |
console.error(`[Zoomer/Boomer Tweet Converter] Error parsing response on attempt ${attempt + 1} for ${type}:`, error); | |
convertTweet(tweetElement, translateButton, type, attempt + 1); | |
} | |
}, | |
onerror: function(error) { | |
console.error(`[Zoomer/Boomer Tweet Converter] Error converting tweets on attempt ${attempt + 1} for ${type}:`, error); | |
convertTweet(tweetElement, translateButton, type, attempt + 1); | |
} | |
}); | |
} | |
function getPromptByType(type, originalText) { | |
if (type === 'Zoomer') { | |
return `convert tweet to cringe Zoomer speak (capping, yeet, bussin) but retaining the message '${originalText}'`; | |
} else if (type === 'Boomer') { | |
return `convert tweet to cringe Boomer speak in a snarky style like Marvin from The Hitchhiker's Guide to the Galaxy, but retaining the message: '${originalText}'`; | |
} | |
return ''; // Fallback for unknown type | |
} | |
setInterval(addTranslateButtons, 1000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment