gleish / Script Auto-Submit Forms

// ==UserScript==
// @name         Script Auto-Submit Forms
// @namespace    http://tampermonkey.net/
// @version      0.3.1
// @description  try to take over the world!
// @author       gleish
// @license MIT
// @grant        none
// @match      *://*/*
// ==/UserScript==


(function() {
    'use strict';

    const NAME = 'Luis Gallegos';
    const EMAIL = 'abc_luis30@hotmail.com';
    const NUMBER = '000000000000';
    const MESSAGE = 'Hi, we would like to contact you for something';

     function hasCoincidences(base, subject) {
        return subject.filter(element => {
            return base.filter(element2 => element2.includes(element)).length >= 1;
        }).length > 2;
    }

    console.log("automated script");
    console.log(document.referrer);

    let URL = window.location.href;

    if(document.body.innerHTML.indexOf('captcha') > -1) {
        console.log('find captcha');


        setTimeout(() => {
                window.close();
            }, (1000 * 60 * 3))

        let intervalFrame = setInterval(() => {
            console.log(document.getElementsByTagName('iframe'));

            if(!document.getElementsByTagName('iframe')[0]){
                return;
            }

            const frame = Array.from(document.getElementsByTagName('iframe')).filter(el => el.src.includes('recaptcha'))[0];
            if(!frame) {
                return;
            }
            clearInterval(intervalFrame);

            const urlParams = new URLSearchParams(frame.src);
            const websiteKey = urlParams.get('k');

            const input = Array.from(document.getElementsByTagName('input')).filter(el => el.name === 'authenticity_token')[0];
            const captcha_input = document.getElementsByName('g-recaptcha-response')[0];

            const Stoken = input.value;

            let taskId;
            let captcha_response;

            fetch('https://api.anti-captcha.com/createTask', {
                method: 'POST',
                body: JSON.stringify({
                    "clientKey": "44642890d937985ecfea1c0e1fd2075b",
                    "task":
                    {
                        "type": "NoCaptchaTaskProxyless",
                        "websiteURL": window.location.href,
                        "websiteKey": websiteKey,
                        "websiteSToken": Stoken
                    }
                })
            })
                .then((res) => res.json())
                .then((res) => {
                console.log(res);

                taskId = res.taskId;
                return taskId;
            })
                .then(() => {

                return new Promise((resolve) => {

                    const interval = setInterval(function () {
                        fetch('https://api.anti-captcha.com/getTaskResult', {
                            method: 'POST',
                            body: JSON.stringify({
                                "clientKey": "44642890d937985ecfea1c0e1fd2075b",
                                "taskId": taskId
                            })
                        })
                            .then((res) => res.json())
                            .then((res) => {
                            console.log(res);
                            if (res.status === 'processing') {
                                //
                            }else if(res.errorCode==="ERROR_NO_SUCH_CAPCHA_ID"){
                             window.close();
                            }
                            else {

                                captcha_response = res.solution.gRecaptchaResponse;

                                resolve(captcha_response);
                                clearInterval(interval);
                            }
                        })
                    }, 5000);


                });

            })
                .then(() => {
                console.log("final step done");
                captcha_input.value = captcha_response;
                if(captcha_input.form){
                    captcha_input.form.submit();
                }else {
                    document.querySelector('[type=submit]').click()
                }
                
            });

        }, 2000)

        return;
    }

    if(URL.includes('contact')) {

        setTimeout(() => {
                window.close();
            }, (1000 * 60 * 3))

        console.log('on contact form');

        let options = ['Thanks for', 'been sent', 'sent', 'thanks', 'get back to you', 'Thank you', 'Merci', 'THANKS FOR', 'Thanks for', 'Danke', 'MESSAGE SENT'];
        let body = document.body.innerText;

        let condition = options.some((option) => body.includes(option));
        if(condition || URL.includes('contact_posted=true')) {
            window.close();
        }
    }


    if(document.referrer.includes(window.location.origin)) {
        console.log('reload same page');
        return;
    }

    console.log(document.URL);


    let forms = Array.from(document.getElementsByTagName('form'));


                let exist_form = false;

                forms.forEach((form) => {
                    let names = Array.from(form).map((el) => el.name);
                    let results = hasCoincidences(names, ['name', 'email', 'number', 'message', 'body']);
                    console.log('Has coincidences ', results);

                    if (results) {

                        exist_form = true;

                        Array.from(form).find((el) => el.name.toLowerCase().includes('name')).value = NAME;

                        Array.from(form).find((el) => el.name.toLowerCase().includes('mail')).value = EMAIL;

                        if (Array.from(form).find((el) => el.name.toLowerCase().includes('number')))
                            Array.from(form).find((el) => el.name.toLowerCase().includes('number')).value = NUMBER;

                        if (Array.from(form).find((el) => el.name.toLowerCase().includes('message')))
                            Array.from(form).find((el) => el.name.toLowerCase().includes('message')).value = MESSAGE;

                        if (Array.from(form).find((el) => el.name.toLowerCase().includes('body')))
                            Array.from(form).find((el) => el.name.toLowerCase().includes('body')).value = MESSAGE;



                        form.submit();

                    }

                });

    // Your code here...
})();