Untitled

 avatar
unknown
javascript
a month ago
1.8 kB
3
Indexable
// Find button within |dom| that has direct child with |text| as innerHTML.
let findButtonsWithInnerHTML = function (dom, text) {
    let buttons = dom.querySelectorAll("button")
    for (var i = buttons.length - 1; i >= 0; i--) {
        let found = false
        for (var j = buttons[i].children.length - 1; j >= 0; j--) {
            if (buttons[i].children[j].innerHTML == text) {
                found = true
            }
        }
        if (found) {
            return buttons[i]
        }
    }
    return null
}

let isSubsOn = true;
document.onkeydown = function (e) {
    if (e.key != 'c' && e.key != 'C') {
        return null;
    }

    let buttons = {}
    let assert = function (condition, message) {
        if (!condition) {
            console.log({"button": buttons, "isSubsOn": isSubsOn})
            throw new Error(message || "Assertion failed");
        }
    }

    buttons["menuPopup"] = document.querySelector(".rc-ModernSettingMenuPopup");
    assert(buttons["menuPopup"] != null, "找不到設定按鈕");

    if (findButtonsWithInnerHTML(buttons["menuPopup"], "Subtitles Off") == null) {
        let subtitles = findButtonsWithInnerHTML(buttons["menuPopup"], "Subtitles");
        assert(subtitles != null, "找不到字幕設定按鈕");
        subtitles.click();
    }
    buttons["noSubs"] = findButtonsWithInnerHTML(buttons["menuPopup"], "Subtitles Off");
    assert(buttons["noSubs"] != null, "找不到關閉字幕按鈕");
    buttons["engSubs"] = findButtonsWithInnerHTML(buttons["menuPopup"], "English");
    assert(buttons["engSubs"] != null, "找不到英文字幕按鈕");

    if (isSubsOn) {
        buttons["noSubs"].click()
        isSubsOn = false;
    } else {
        buttons["engSubs"].click();
        isSubsOn = true;
    }
}
Editor is loading...
Leave a Comment