50 Plus Crossword Helper

Helper for https://www.50plus.de/spiele/raetsel/kreuzwortraetsel-*.html

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         50 Plus Crossword Helper
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Helper for https://www.50plus.de/spiele/raetsel/kreuzwortraetsel-*.html
// @author       You
// @match        https://www.50plus.de/spiele/raetsel/kreuzwortraetsel-*.html
// @icon         https://www.google.com/s2/favicons?sz=64&domain=50plus.de
// @grant        none
// ==/UserScript==


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

    function send_input(character) {
        let e = new Event("keydown");
        e.which = character;
        document.dispatchEvent(e);
    }

    async function solve_selected_items(blocks, delay) {
        for (let block of blocks.children) {
            if (block.className.startsWith("selected")) {
                if (block.dataset.default !== "") {
                    send_input(block.dataset.default.charCodeAt());
                    await sleep(delay);
                }

            }
        }
    }

    const wait = setInterval(() => {
        let blocks = document.querySelector("#dsgame > kwr > div.sd-kwr-bg");
        if (blocks !== undefined) {
            console.log("[Helper] Preparing");
            clearInterval(wait);
            jQuery("iblock").bind("mousedown", async (event) => {
                if (event.which !== 2) { // only middle click
                    return
                }

                const elem_index = parseInt(event.currentTarget.dataset.x) + 12*parseInt(event.currentTarget.dataset.y) + 1
                const elem = blocks.querySelector(`block:nth-child(${elem_index})`);

                if (elem.dataset.default === "") { // Description
                    await solve_selected_items(blocks, 100);

                } else { // Character field
                    send_input(elem.dataset.default.charCodeAt());
                    document.querySelector(`#dsgame > kwr > div.sd-kwr-input > iblock:nth-child(${elem_index})`).dispatchEvent(new Event("mousedown"))
                }
            });

            const btn = document.createElement("button");
            btn.textContent = "Solve All";
            btn.onclick = () => {
                const inputs = document.querySelector("#dsgame > kwr > div.sd-kwr-input").childNodes;
                (async () => {
                    for (let i = 0; i < inputs.length; i++) {
                        if (blocks.childNodes[i].dataset.default !== "") {
                            inputs[i].dispatchEvent(new MouseEvent("mousedown", {
                                button: 1
                            }))
                            await sleep(20);

                        }
                    }
                })();
            }
            document.querySelector("#dsgame").appendChild(btn);
            console.log("[Helper] Done");

        }
    }, 1000)
    })();