Twitter Traduko Esperanto

On tweets tagged with #Esperanto hashtag, adds a link that translates the tweet on Google Translate, opening it in a new tab

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Twitter Traduko Esperanto
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  On tweets tagged with #Esperanto hashtag, adds a link that translates the tweet on Google Translate, opening it in a new tab
// @author       nulll
// @license      Artistic-2.0
// @match        https://twitter.com/*
// @icon         https://www.google.com/s2/favicons?domain=twitter.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // heavly inspired by
    // https://greasyfork.org/it/scripts/429954-twitter-1-click-tranlsate/

    let observer;
    const interval = setInterval(init, 500);
    const lang = document.getElementsByTagName('html')[0].getAttribute('lang');

    function init() {

        const el = document.querySelectorAll('div[data-testid="primaryColumn"] section article');

        if (el && el.length > 0) {
            clearInterval(interval);

            //add links to already exisiting articles
            observer = new MutationObserver(newTweets);
            observer.observe(document.body, {
                childList: true,
                attributes: false,
                subtree: true,
                characterData: false
            });

            el.forEach(article => {
                createLink(article);
            });
        }
    }

    function newTweets(mutationList, observer) {

        mutationList.forEach(mut => {

            if (mut.addedNodes.length > 0) {

                mut.addedNodes.forEach(node => {

                    if (node.innerHTML && node.innerHTML.indexOf('<article ') > -1) {

                        createLink(node.querySelector('article'));

                    }
                });
            }

        });
    }

    function createLink(article) {

        // Translate link appears only on tweets having the #Esperanto hashtag
        const htag = article.querySelector('a[href*="/hashtag/Esperanto"]');

        if (htag) {
            let tweet = article.querySelector('[data-testid="tweetText"]');

            let gt_url = "https://translate.google.com/?sl=eo&tl=" + encodeURIComponent(lang) + "&op=translate&text=" + encodeURIComponent(tweet.textContent);

            let link = document.createElement("a");
            link.href = gt_url;
            link.target = '_blank';
            link.textContent = '💚 traduki el Esperanto';

            let css = window.getComputedStyle(htag);
            Array.from(css).forEach(key => link.style.setProperty(key, css.getPropertyValue(key), css.getPropertyPriority(key)));
            link.style.textDecoration = 'none';

            let div = document.createElement("div");
            div.style.marginTop = '1em';
            div.style.marginBottom = '1em';
            div.appendChild(link);

            tweet.after(div);
        }
    }

})();