getVaptchaCode

vaptcha-iframe 依赖库

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/418420/878837/getVaptchaCode.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

/**
 * getVaptchaCode v1.0
 * @author Jixun<https://jixun.moe/>
 * @license BSD-3-Clause
 */

function getVaptchaCode(frameURL, container) {
    return new Promise((resolve) => {
        const {origin} = new URL(frameURL);
        const id = 'id' + Date.now() + Math.random();
        const frame = document.createElement('iframe');
        frame.style.transition = 'opacity 1s 1s';
        frame.style.opacity = 1;

        const eventHandler = (event) => {
            if (event.data.vaptcha === id) {
                resolve(event.data.token);
                window.removeEventListener('message', eventHandler);
                frame.style.opacity = 0;

                // Remove iframe after fadeout effects.
                setTimeout(() => {
                    if (frame.parentNode) {
                        frame.parentNode.removeChild(frame);
                    }
                }, 1000);
            }
        };

        window.addEventListener('message', eventHandler);

        frame.src = frameURL;
        frame.onload = () => {
            frame.contentWindow.postMessage({ vaptcha: 'init', id }, origin);
        };
        container.appendChild(frame);
    });
}