Greasy Fork

Repubblica - Sintesi vocale

Aggiunge una sintesi vocale agli articoli

目前为 2020-07-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         Repubblica - Sintesi vocale
// @name:it      Repubblica - Sintesi vocale
// @namespace    http://cosoleto.free.fr/
// @version      0.1
// @description  Aggiunge una sintesi vocale agli articoli
// @description:it  Aggiunge una sintesi vocale agli articoli
// @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;

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

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

    if (location.host.startsWith("rep.")) {
        speechButtonsDestinationElement = document.getElementsByClassName("detail-article_container")[1];
    } else if (location.host.startsWith("ricerca.")) {
        articleElement = articleElement.querySelector("article");
        var uselessElement = articleElement.querySelector("aside");
        if (uselessElement) {
            articleElement.removeChild(uselessElement);
        }
        speechButtonsDestinationElement = document.getElementById("header");
    } else {
        speechButtonsDestinationElement = document.getElementById("utility-print").parentNode;
    }

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

    speechButtonsElement.innerHTML = `<a 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);

        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");
        sleeptime *= 2;
        await sleep(sleeptime);
        if (sleeptime > 90000) {
            return;
        }
    } while (articleElement === null);

    main();
}

(function() {
    "use strict";

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