Skip to content

Instantly share code, notes, and snippets.

@imshaiknasir
Created September 10, 2024 06:51
Show Gist options
  • Save imshaiknasir/3513c1d57d52bc75846be326b84f954b to your computer and use it in GitHub Desktop.
Save imshaiknasir/3513c1d57d52bc75846be326b84f954b to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Twitter Translate Button
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add a translate button to tweets
// @match https://twitter.com/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
// Function to add translate button to a tweet
function addTranslateButton(tweetElement) {
if (tweetElement.querySelector('.translate-btn')) return; // Button already added
const actionBar = tweetElement.querySelector('[role="group"]');
if (!actionBar) return;
const translateBtn = document.createElement('div');
translateBtn.className = 'translate-btn';
translateBtn.innerHTML = '🌐'; // You can replace this with an icon of your choice
translateBtn.style.cursor = 'pointer';
translateBtn.style.marginLeft = '8px';
translateBtn.addEventListener('click', function() {
const tweetText = tweetElement.querySelector('[data-testid="tweetText"]').textContent;
translateTweet(tweetText, tweetElement);
});
actionBar.insertBefore(translateBtn, actionBar.children[2]); // Insert before the like button
}
// Function to translate tweet using Google Translate API
function translateTweet(text, tweetElement) {
// Note: You'll need to replace 'YOUR_API_KEY' with an actual Google Translate API key
const apiKey = 'YOUR_API_KEY';
const url = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`;
GM_xmlhttpRequest({
method: 'POST',
url: url,
data: JSON.stringify({
q: text,
target: 'en'
}),
headers: {
'Content-Type': 'application/json'
},
onload: function(response) {
const result = JSON.parse(response.responseText);
if (result.data && result.data.translations && result.data.translations[0]) {
const translatedText = result.data.translations[0].translatedText;
displayTranslation(translatedText, tweetElement);
}
}
});
}
// Function to display the translated text
function displayTranslation(translatedText, tweetElement) {
const translationElement = document.createElement('div');
translationElement.className = 'tweet-translation';
translationElement.textContent = 'Translation: ' + translatedText;
translationElement.style.marginTop = '8px';
translationElement.style.padding = '8px';
translationElement.style.backgroundColor = '#f0f0f0';
translationElement.style.borderRadius = '8px';
const existingTranslation = tweetElement.querySelector('.tweet-translation');
if (existingTranslation) {
existingTranslation.replaceWith(translationElement);
} else {
tweetElement.appendChild(translationElement);
}
}
// Observe for new tweets being added to the page
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
const tweets = node.querySelectorAll('[data-testid="tweet"]');
tweets.forEach(addTranslateButton);
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
// Add translate buttons to existing tweets
document.querySelectorAll('[data-testid="tweet"]').forEach(addTranslateButton);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment