首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助简化我的坦佩猴子脚本

需要帮助简化我的坦佩猴子脚本
EN

Stack Overflow用户
提问于 2015-12-27 03:40:45
回答 1查看 65关注 0票数 1

我需要一些帮助简化我的脚本中的代码。我的代码中有很多重复的部分需要处理,这些部分很难跟上。下面是代码:

代码语言:javascript
复制
// ==UserScript==
// @run-at document-idle
// @name         my script
// @version      0.1
// @author       me
// @match        http://website.com/
// @grant        GM_addStyle
// @grant GM_xmlhttpRequest
// @require http://code.jquery.com/jquery-latest.js
// @require https://raw.githubusercontent.com/sunnywalker/jQuery.FilterTable/master/jquery.filtertable.js
// ==/UserScript==
/* jshint -W097 */

setInterval(bprs, 600000);
setTimeout(bprs, 4000);

function bprs() {

    {
        var accountname1 = $('#accountName1').text(); // points to the first account field - please add more depending on how many accounts you have 'accountName2,accountName3,4,5...'
        var credits1 = $('#credits1').text(); // points to the first credits field - please add more depending on how many accounts you have 'credits2,credits3,4,5...'
        var accountname2 = $('#accountName2').text();
        var credits2 = $('#credits2').text();
        var accountname3 = $('#accountName3').text();
        var credits3 = $('#credits3').text();
        var accountname4 = $('#accountName4').text();
        var credits4 = $('#credits4').text();
        var accountname5 = $('#accountName5').text();
        var credits5 = $('#credits5').text();
        var accountname6 = $('#accountName6').text();
        var credits6 = $('#credits6').text();
        var date = new Date();
        var data = "date=" + date + 
            // "&accountname1=" + accountname1 + "&credits1=" + credits1 new entires must be added depending on how many accounts you have
            "&accountname1=" + accountname1 + "&credits1=" + credits1 +
            "&accountname2=" + accountname2 + "&credits2=" + credits2 + 
            "&accountname3=" + accountname3 + "&credits3=" + credits3 + 
            "&accountname4=" + accountname4 + "&credits4=" + credits4 + 
            "&accountname5=" + accountname5 + "&credits5=" + credits5 +
            "&accountname6=" + accountname6 + "&credits6=" + credits6;
        //Changes must also be added to the PHP script
        $.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
            options.async = true;
        });

        $.ajax({
            url: 'http://mywebsite.com/submit.php', // point to the php file here
            async:false,
            type: "POST",
            dataType: "json",
            data: data,
            success: function (result) {
                JSON.parse(result);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr);
            }
        });
    }
}  

理想情况下,我希望信贷和帐户名称条目缩短为循环,用户可以配置在页面上找到多少帐户,而不必设置信用条目。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-27 04:08:31

当你有这样的重复模式,特别是自然索引(account1,2,3.)你真的应该利用数组。

下面是如何简化重复的部分,并使用变量配置数量:

代码语言:javascript
复制
function bprs() {
        // Change this variable depending on how many accounts you have
        var accountsCount = 6;                       

        var accounts = [];
        for (var n = 1; n <= accountsCount; n++) {
            // Using objects literals to group your data is a great way to make your code clearer
            accounts[n] = {                          
                name: $('#accountName' + n).text(),
                credits: $('#credits' + n).text()
            };
        }


        var date = new Date();
        var data = "date=" + date + 
            accounts.reduce(function (prev, account, n) {
                return prev + "&accountname" + n + "=" + account.name +
                              "&credits" + n + "=" + account.credits;
            }, '');


        //...

我假设您还可以自动设置accountsCount变量,例如,如果account元素附加了一个特定的类:

代码语言:javascript
复制
var accountsCount = document.getElementsByClassName('SPECIFIC_CLASS').length;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34477386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档