FBase Lib

Base library

Version vom 07.07.2023. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyfork.org/scripts/461948/1216531/FBase%20Lib.js

// ==UserScript==
// @name           FBase Lib
// @description    Base library
// @version        0.0.5
// ==/UserScript==
    const FOUR_MINUTES = 4 * 60 * 1000;
    const wait = ms => new Promise(resolve => setTimeout(resolve, ms || 3000));
    const rndInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

    Element.prototype.isVisible = function() {
        return !!(this.offsetWidth||this.offsetHeight||this.getClientRects().length);
    };
    Element.prototype.isUserFriendly = function(selector) {
        let e = selector ? this.querySelector(selector) : this;
        return e && e.isVisible()  ? e : null;
    };
    Document.prototype.isUserFriendly = Element.prototype.isUserFriendly;

    class CrawlerWidget {
        constructor(params) {
            if (!params || (!params.selector && !params.fnSelector)) {
                throw new Error('CrawlerWidget requires a selector or a function selector parameter');
            }
            this.context = this.context || document;
            Object.assign(this, params);
        }

        get isUserFriendly() {
            if (this.selector) {
                this.element = this.context.isUserFriendly(this.selector);
                return this.element;
            } else {
                this.element = this.fnSelector();
                return this.element;
            }
        }
    }

    class CaptchaWidget extends CrawlerWidget {
        constructor(params) {
            super(params);
        }

        solve() { return true; }

        async isSolved() { return false; }
    }

    class HCaptchaWidget extends CaptchaWidget {
        constructor(params) {
            let defaultParams = {
                selector: '.h-captcha > iframe',
                waitMs: [1000, 5000],
                timeoutMs: FOUR_MINUTES
            };
            for (let p in params) {
                defaultParams[p] = params[p];
            }
            super(defaultParams);
        }

        async isSolved() {
            return wait().then( () => {
                if (this.isUserFriendly && this.element.hasAttribute('data-hcaptcha-response') && this.element.getAttribute('data-hcaptcha-response').length > 0) {
                    return Promise.resolve(true);
                }
                return this.isSolved();
            });
        }
    }

    class RecaptchaWidget extends CaptchaWidget {
        constructor(params) {
            let defaultParams = {
                selector: function() { return grecaptcha },
                waitMs: [1000, 5000],
                timeoutMs: 4 * 60 * 1000
            };
            for (let p in params) {
                defaultParams[p] = params[p];
            }
            super(defaultParams);
        }

        get isUserFriendly() {
            this.element = grecaptcha;
            console.log(`@recaptcha => isUserFriendly:`, this.element);
            return this.element;
        }

        async isSolved() {
            console.log('@isSolved');
            return wait().then( () => {
                try {
                    if (this.isUserFriendly && this.element.hasOwnProperty('getPageId') && this.element.getPageId() && this.element.hasOwnProperty('getResponse') && (typeof(this.element.getResponse) == 'function')
                        && this.element.getResponse().length > 0) {
                        return Promise.resolve(true);
                    }
                } catch (err) {}
                return this.isSolved();
            });
        }
    }