GitHub Auto Signup

Automate github signup

目前为 2024-02-22 提交的版本。查看 最新版本

// ==UserScript==
// @name         GitHub Auto Signup
// @version      0.2
// @description  Automate github signup
// @author       Young Jimmy
// @match        https://github.com/signup*
// @match        https://github.com/account_verifications?recommend_plan=true
// @grant        GM_xmlhttpRequest
// @license      MIT
// @namespace https://greasyfork.org/users/1218336
// ==/UserScript==

(function() {
    'use strict';

    // Delay function
    function delay(time) {
        return new Promise(function(resolve) {
            setTimeout(resolve, time);
        });
    }

    // Wait for label with specific content to exist
    function labelReady(content) {
        return new Promise((resolve, reject) => {
            new MutationObserver((mutationRecords, observer) => {
                Array.from(document.querySelectorAll('label')).forEach((element) => {
                    if (element.textContent.trim() === content) {
                        console.log(`Label with content "${content}" found.`);
                        resolve(element);
                        observer.disconnect();
                    }
                });
            })
                .observe(document.documentElement, {childList: true, subtree: true});
        });
    }

    function generatePassword(length) {
        const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+~`|}{[]:;?><,./-=";
        let retVal = "";
        for (let i = 0, n = charset.length; i < length; ++i) {
            retVal += charset.charAt(Math.floor(Math.random() * n));
        }
        return retVal;
    }

    function generateUsername(length) {
        let result = '';
        const characters = '0123456789';
        const charactersLength = characters.length;
        for (let i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
    }

    // Wait for element to exist
    function elementReady(selector) {
        return new Promise((resolve, reject) => {
            let el = document.querySelector(selector);
            if (el) { resolve(el); }
            new MutationObserver((mutationRecords, observer) => {
                // Query for elements matching the specified selector
                Array.from(document.querySelectorAll(selector)).forEach((element) => {
                    resolve(element);
                    //Once we have resolved we don't need the observer anymore.
                    observer.disconnect();
                });
            })
                .observe(document.documentElement, {childList: true, subtree: true});
        });
    }

    async function signup(email) {
        const password = generatePassword(12);
        const username = generateUsername(Math.floor(Math.random() * 5) + 8);

        console.log("Waiting for label...");
        await labelReady("Enter your email*");

        console.log("Starting signup process...");
        await elementReady(".js-continue-container").then(element => element.click());
        await delay(1000);

        await elementReady("#email").then(element => element.value = email);
        await elementReady(".mx-1").then(element => element.click());
        await elementReady("#email-container .js-continue-button").then(element => element.click());
        await delay(1000);

        await elementReady("#password").then(element => {element.click(); element.value = password;});
        await elementReady(".mx-1").then(element => element.click());
        await elementReady("#password-container .js-continue-button").then(element => element.click());
        await delay(1000);

        await elementReady("#login").then(element => {element.click(); element.value = username;});
        await elementReady(".mx-1").then(element => element.click());
        await elementReady("#username-container .js-continue-button").then(element => element.click());
        await elementReady("#opt-in-container .js-continue-button").then(element => element.click());
        await elementReady("button[name=button]").then(element => element.click());

        console.log("Signup process completed.");
        // Store the email in localStorage after signup
        localStorage.setItem('email', email);
    }

    async function getEmail() {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: "http://127.0.0.1:5000/get_email",
                onload: function(response) {
                    console.log('http://127.0.0.1:5000/get_email',response.responseText)
                    const data = JSON.parse(response.responseText);
                    if (data.account) {
                        resolve(data.account);
                    } else {
                        reject('Failed to get email');
                    }
                }
            });
        });
    }

    async function getVerificationCode(email) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: `http://127.0.0.1:5000/get_verification_code?account=${email}`,
                onload: function(response) {
                    console.log(response.responseText)
                    const data = JSON.parse(response.responseText);
                    if (data.verification_code) {
                        resolve(data.verification_code);
                    } else {
                        reject('Failed to get verification code');
                    }
                }
            });
        });
    }

async function fillVerificationCode(code) {
    const codeInputs = Array.from(document.querySelectorAll('.form-control.input-monospace'));
    const codeArray = code.split('');
    codeInputs.forEach((input, index) => {
        if (codeArray[index]) {
            input.value = codeArray[index];
            input.dispatchEvent(new Event('input', { bubbles: true, cancelable: true }));
        }
    });
}



    async function checkVerificationCode(email) {
        while (true) {
            try {
                const code = await getVerificationCode(email);
                if (code) {
                    fillVerificationCode(code);
                    break;
                }
            } catch (error) {
                console.error(error);
            }
            await delay(3000);
        }
    }

    console.log("Script loaded, waiting for page load...");
    window.addEventListener('load', async function() {
        console.log("Page loaded, starting script...");
        if (window.location.href === 'https://github.com/signup?source=login') {
            console.log('start get eamil')
            let email = await getEmail();
            signup(email);
        } else if (window.location.href === 'https://github.com/account_verifications?recommend_plan=true') {
            // Retrieve the email from localStorage on the verification page
            let email = localStorage.getItem('email');
            if (email) {
                checkVerificationCode(email);
            }
        }
    }, false);

})();