50 Plus Crossword Helper

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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)
    })();