Greasy Fork

Repubblica - Sintesi vocale

Aggiunge la sintesi vocale agli articoli di repubblica.it

目前为 2020-10-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         Repubblica - Sintesi vocale
// @name:it      Repubblica - Sintesi vocale
// @namespace    http://cosoleto.free.fr/
// @version      0.5
// @description  Aggiunge la sintesi vocale agli articoli di repubblica.it
// @description:it  Aggiunge la sintesi vocale agli articoli repubblica.it
// @author       Francesco Cosoleto
// @match        http*://www.repubblica.it/*
// @match        http*://rep.repubblica.it/*
// @match        http*://ricerca.repubblica.it/*
// @match        http*://bari.repubblica.it/*
// @match        http*://bologna.repubblica.it/*
// @match        http*://firenze.repubblica.it/*
// @match        http*://genova.repubblica.it/*
// @match        http*://milano.repubblica.it/*
// @match        http*://napoli.repubblica.it/*
// @match        http*://palermo.repubblica.it/*
// @match        http*://parma.repubblica.it/*
// @match        http*://roma.repubblica.it/*
// @match        http*://torino.repubblica.it/*
// @grant        none
// ==/UserScript==

const PAUSA = '(Pausa)';
const RESUME = '(Riprendi)';
var articleElement;
var shadow;

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

function main(){
    var speechButtonsElement, speechButtonsDestinationElement, pauseButton;

    var cloneElement = articleElement.cloneNode(true);

    if (location.host.startsWith("rep.")) {
        if (shadow) {
            speechButtonsDestinationElement = shadow.getElementsByClassName("detail-article_container")[1];
        } else {
            speechButtonsDestinationElement = document.getElementsByClassName("detail-article_container")[1];
        }
        cloneElement.querySelectorAll("script,.detail_bottom-trigger,#detail-values_big,.detail-tag_container").forEach(el => el.remove());
    } else if (location.host.startsWith("ricerca.")) {
        cloneElement = cloneElement.querySelector("article");
        var uselessElement = cloneElement.querySelector("aside");
        if (uselessElement) {
            cloneElement.removeChild(uselessElement);
            cloneElement.innerText = cloneElement.innerText.replace("© RIPRODUZIONE RISERVATA","");
        }
        speechButtonsDestinationElement = document.getElementById("header");
    } else {
//        speechButtonsDestinationElement = document.getElementById("utility-print").parentNode;
        speechButtonsDestinationElement = document.getElementsByClassName("gs-sharebar")[1];

        cloneElement.querySelectorAll("article, figure, section, #adv-Bottom").forEach(el => el.remove());
    }

    articleElement = cloneElement;

    speechButtonsElement = document.createElement("div");
    //speechButtonsElement.setAttribute("id", "speechbar");

    speechButtonsElement.innerHTML = `<a class="share gs-link" href="javascript:void(0);" title="Leggi">🔊</a>`;

    window.addEventListener("unload", function() { if (speechSynthesis.speaking) { speechSynthesis.cancel(); } } );

    pauseButton = document.createElement("a");
    pauseButton.setAttribute("href", "javascript:void(0);");
    pauseButton.setAttribute("style", "display: none");

    speechButtonsElement.firstChild.addEventListener("click", function () {
        var utterance = new SpeechSynthesisUtterance(articleElement.innerText);
        utterance.lang = 'it-IT';
        utterance.onend = function () {pauseButton.style = "display: none";};

        speechSynthesis.cancel();
        speechSynthesis.speak(utterance);
        //console.log(articleElement.innerText);

        setTimeout(function() {
            pauseButton.style = "display: block";
            pauseButton.innerText = PAUSA;
        }, 1000);
    });

    pauseButton.addEventListener("click", function () {
        if (speechSynthesis.paused) {
            speechSynthesis.resume();
            pauseButton.innerText = PAUSA;
        }
        else {
            speechSynthesis.pause();
            pauseButton.innerText = RESUME;
        }
    });

    speechButtonsDestinationElement.appendChild(speechButtonsElement);
    speechButtonsElement.appendChild(pauseButton);
}

async function find_article() {
    var sleeptime = 1;
    do {
        articleElement = document.getElementById("article-body") || document.getElementsByClassName("paywall")[0] || document.getElementById("singolo-elemento");
        if (!articleElement) {
            try {
                shadow = document.querySelector("news-app").shadowRoot.getElementById("1").shadowRoot.querySelector(".amp-doc-host").shadowRoot.querySelector("main")
                articleElement = shadow.querySelector(".paywall");
            } catch (e) {}
        }
        sleeptime *= 2;
        await sleep(sleeptime);
        if (sleeptime > 90000) {
            return;
        }
    } while (articleElement === null);

    main();
}

(function() {
    "use strict";

    if (location.pathname.length === 1) {
        return;
    }

    if (document.readyState === "complete" || document.readyState === "loaded") {
        find_article();
    } else {
        window.addEventListener("load", find_article);
    }
})();