Greasy Fork

多邻国diy开发

多邻国做题配合划词翻译显示翻译文本

目前为 2024-04-23 提交的版本。查看 最新版本

// ==UserScript==
// @name        多邻国diy开发
// @description 多邻国做题配合划词翻译显示翻译文本
// @match       https://www.duolingo.com/*
// @require     https://code.jquery.com/jquery-3.4.1.js
// @version 1.3.6.0
// @namespace https://greasyfork.org/users/157318
// ==/UserScript==


function isLesson() {
    return /https:\/\/www.duolingo.com\/(lesson|practice|skill.*)/.test(window.location.href);
}
// 添加css
  function addStyle(css) {
    var style = document.createElement('style');
    style.innerHTML = css;
    document.head.appendChild(style);
  }




function fy(){
    var mydiv=$("#myDiv");
    if (mydiv.length==1){
        $("#myDiv").remove();
        return;
    }
    var fanyi=$("._2-F7v");//一个词一个词的$("div[dir=ltr]")中的第二个div
    //alert($("title").text());
    var strone="";
    if (fanyi.length==1){//一个一个词拼句子
        for(var i=0;i<fanyi.find("div").length;i++){
            //品句子
            strone=strone+fanyi.find("div").eq(i).text()+" ";
        }
        fanyi.append("<div id='myDiv' style='font-size: 0px;margin-left: 30%;'>"+strone+"</div>");
        //$("#myDiv").focus();
        window.getSelection().selectAllChildren(document.getElementById("myDiv"));
        $("#__hcfy__")[0].shadowRoot.querySelector(".bp3-button").click();
        window.getSelection().removeAllRanges();
        console.log(strone);
        return;
    }
    var tiankong=$("div[dir=ltr]");
    if (tiankong.length>0){//填空题品句子
        var tiankongstr=tiankong[0].innerText.replaceAll("\n","");
        tiankong.append("<div id='myDiv' style='font-size: 0px;margin-left: 30%;'>"+tiankongstr+"</div>");
        //$("#myDiv").focus();
        window.getSelection().selectAllChildren(document.getElementById("myDiv"));
        $("#__hcfy__")[0].shadowRoot.querySelector(".bp3-button").click();
        window.getSelection().removeAllRanges();
        console.log(tiankongstr);
        return;
    }

}
function addfy(){
    if ($("#benty4").length==0){

        $("._3v4ux").append("<button id='benty4' style='float:right;'>翻译</button>");
        $("._1fxa4._1Mopf").eq(0).css("overflow","auto");
        $("#benty4").click(function(){fy();});

    }
}

$(function(){//页面加载完成执行

     addStyle('html, * {-webkit-user-select:text!important; -moz-user-select:text!important;}');

    setTimeout(() => {
        addfy();
    }, 5000);
    $("body").click(function(){addfy();});

});


window.addEventListener("keydown", function(e) {
    var key = e.key.toLowerCase();

    if (isLesson()) {

        if ($("#benty4").length==0){
            addfy();
        }
        if (key == "f2") {
            fy();
        }
        if (key == "f8") {
            $("._3xeI3").eq(0).find("button").click();


            var isFocus=$("textarea").is(":focus");
            if(true==isFocus){
                console.log('有focus');
                var ev = $.Event('keypress');
                ev.which = 74; // Carriage-return (Enter)
                $('textarea').trigger(ev);
            }else{
                $("textarea").focus();
                console.log('没有focus');
            }/**/
        }
        if (key == "f9") {
            $("._3xeI3").eq(1).find("button").click();
        }/*
        if (key == "escape") {
            $(".yWRY8._3yAjN").eq(0).click();
        }
        // discussion shortcut
        if (key == "d") {
            $("span:contains(Discuss)").click();
        }

        // report shortcut
        if (key == "r") {
            $("span:contains(Report)").click();
        }

        // keyboard toggle
        if (key == "b" && e.ctrlKey) {
            e.preventDefault();
            $("button[data-test=player-toggle-keyboard]").click();
        }*/

    }
});
/*
// insert a-z shortcut hints on matching tiles
var observer = new MutationObserver(function(mutations) {
    if (isLesson()){
        var tiles = $("div[data-test=word-bank] button[data-test=challenge-tap-token]");
        if (tiles.length > 0) {
            var alpha = "asdfghjklweruioxcvnm";
            tiles.each(function (i, e) {
                var tile = $(e);
                if (!tile.attr("hint-added")) {
                    var hint = document.createElement("span");
                    hint.className = "_2R_o5 _2S0Zh _2f9B3 matching-hint";
                    hint.innerHTML = alpha.charAt(i).toUpperCase();
                    hint.style.marginRight = "10px";
                    tile.prepend(hint);
                    hint = $(hint);
                    var buttonObserver = new MutationObserver(function(m) {
                        if (tile.hasClass("pmjld")) {
                            hint.removeClass("_2R_o5").addClass("Z7UoT");
                        } else {
                            hint.removeClass("Z7UoT").addClass("_2R_o5");
                        }
                        if (tile.attr("disabled")) {
                            hint.css("color", "#e5e5e5");
                            buttonObserver.disconnect();
                        }
                    });
                    buttonObserver.observe(e, {attributes: true});
                    tile.attr("hint-added", "1");
                }
            });
        }
    }
});

$("body").prepend("<div ><button id='benty4'>翻译</button></div>");
$("#benty4").click(function(){
    alert("ddd");
var e = $.Event("keydown"); //模拟一个键盘事件
e.keyCode = 66;
e.which = 66; //增加设置which
$('textarea')[0].click(); //模拟按键
    $(window).trigger(e);
});
*/



//observer.observe(document, {childList: true, subtree: true, attributes: false, characterData: false});