篡改猴子脚本在不同的机器和浏览器上的行为不同

时间:2019-02-05 11:26:12

标签: javascript tampermonkey

// ==UserScript==
// @name         responsive design
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Take screenshot
// @author       You
// @match        https://example.com
// @grant        none
// @require      https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.5.0-beta4/html2canvas.min.js
// ==/UserScript==

window.addEventListener('load', function() {
    'use strict';
    var overrideRadioButtonToChoose = -1;
    var allowDuplicateResponses = false;
    var powerToRaiseRandomNumberTo = 1;
    var radioButtonNames = new Array();
    function addName(radioButtonName) {
        var uniqueName = true;
        for (
            var nameCounter = 0;
            nameCounter < radioButtonNames.length;
            nameCounter++
        ) {
            if (radioButtonNames[nameCounter] == radioButtonName) {
                uniqueName = false;
            }
        }
        if (uniqueName == true) {
            radioButtonNames.push(radioButtonName);
        }
    }
    function findRadioButtons() {
        var documentElements = document.forms[0].elements;
        for (
            var elementCounter = 0;
            elementCounter < documentElements.length;
            elementCounter++
        ) {
            if (documentElements[elementCounter].type == 'radio') {
                addName(documentElements[elementCounter].name);
            }
        }
    }
    function setRadioButtons() {
        var radioToSelect;
        var numberOfRadioButtons = document.getElementsByName(radioButtonNames[0])
        .length;
        var usedRadioButton = new Array(numberOfRadioButtons);
        for (var RBCounter = 0; RBCounter < numberOfRadioButtons; RBCounter++) {
            usedRadioButton[RBCounter] = false;
        }
        for (
            var RBGroupCounter = 0;
            RBGroupCounter < radioButtonNames.length;
            RBGroupCounter++
        ) {
            if (overrideRadioButtonToChoose > -1) {
                radioToSelect = overrideRadioButtonToChoose;
            } else {
                radioToSelect = Math.floor(
                    Math.pow(Math.random(), powerToRaiseRandomNumberTo) *
                    numberOfRadioButtons
                );
                if (!allowDuplicateResponses) {
                    while (
                        usedRadioButton[
                            (radioToSelect = Math.floor(
                                Math.pow(Math.random(), powerToRaiseRandomNumberTo) *
                                numberOfRadioButtons
                            ))
                        ]
                    ) {}
                }
            }
            document.forms[0][radioButtonNames[RBGroupCounter]][
                radioToSelect
            ].checked = true;
            usedRadioButton[radioToSelect] = true;
        }
    }
    var pageTitle = document.title.replace(/ /g,"_");
    var pageName = document.getElementsByTagName('h2')[0].textContent.replace(/ /g,"_");
    takeScreenShot(`${pageTitle}-${pageName}.png`);

    function takeScreenShot(filename) {
        html2canvas(document.body)
            .then(function(canvas) {
            saveAs(canvas.toDataURL(), filename);
        })
            .then(() => {
            findRadioButtons();
            setRadioButtons();
            var nextButtonCollection = document.getElementsByClassName('btn-next');
            nextButtonCollection[0].click();
        });
    }

    function saveAs(uri, filename) {
        var link = document.createElement('a');

        if (typeof link.download === 'string') {
            link.href = uri;
            link.download = filename;

            //Firefox requires the link to be in the body
            document.body.appendChild(link);

            //simulate click
            link.click();

            //remove the link when done
            document.body.removeChild(link);
        } else {
            window.open(uri);
        }
    }
}, false);

上面的篡改猴子脚本在chrome上对我来说效果很好。但是,在其他人的Chrome上执行一页,然后卡住。同样在Firefox上,我会下载第一个图像并填写单选按钮,但由于某种原因从不单击下一个按钮。

我已经检查了正在使用该页面的页面,并且在下一个按钮上没有使用任何ajax请求(更改了此示例的网站网址,因为它是内部的)。

我的脚本是否存在明显的错误,导致它在不同的浏览器上表现不同。

0 个答案:

没有答案