Greasy Fork

Duolingo Audio

try to take over the world!

目前为 2020-11-28 提交的版本。查看 最新版本

// ==UserScript==
// @name         Duolingo Audio
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @include      https://www.duolingo.com/checkpoint*
// @include      https://www.duolingo.com/skill/*
// @grant        none
// ==/UserScript==

let header = "";
let sentence = "";

(function() {
    console.log("DuoMod Script mounted")
    'use strict';

    // Toggle console outputs
    let logOutput = false;
    let log = function(){
        if (logOutput){
            return console.log.apply(console, arguments);
        }
    };

    const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

    const onMutation = (mutations) => {
        const headerEl = document.querySelector('[data-test="challenge-header"]');
        const hintEl = document.querySelector('[data-test="hint-sentence"]');
        log("Something changes, maybe will mute audio", mutations, hintEl, headerEl)
        if (hintEl && headerEl && headerEl.innerText === "Write this in English") {
            const curHeader = headerEl.innerText;
            const curSentence = hintEl.innerText;
            if(curSentence !== sentence || curHeader !== header) {
                log("Audio muted");
                Howler.stop();
                header = curHeader;
                sentence = curSentence;
            }
            else {
                log("Audio was already muted");
            }
        }
    }

    var observer = new MutationObserver(onMutation);

    var settings = {
        childList: true, subtree: true, attributes: false, characterData: false,
    }

    observer.observe(document.body, settings); // I tried to find a better thing to observe (like the next button), but nothing worked

})();