【问题标题】:How to work with the results of api.php using javascript?如何使用 javascript 处理 api.php 的结果?
【发布时间】:2014-01-29 02:55:28
【问题描述】:

我四处寻找,似乎找不到我要找的东西。

如何在 javascript 中使用api.php?action=query&list=allusers&augroup=sysop&aulimit=max&format=json 的结果?

我要做的是创建一个脚本来简单地更改 wiki 上用户名的颜色,如果他们在某些组中,比如管理员、官僚等。

虽然我通常很擅长弄清楚这些事情,但我整天都在研究这个问题,但我一无所获。任何人都可以帮我举一些例子或其他东西吗?如果可以使用大部分 jQuery 来完成,那就更好了。

提前致谢。


编辑:(回应ahren的评论):

我开始尝试清理和修改其他人编写的脚本以添加更多功能/使其按预期工作,但我无法理解它:

/* HighlightUsers by Bobogoobo
 * Changes color of links to specified groups and users
 * TODO: redo but much better (recursive would be easier - I've learned a lot since I wrote this thing)
 */
function highlightUsers () {
    "use strict";
    var highlight = window.highlight || {}, selector = '', that, userstr,
        indices = [],
        i = 0,
        user,
        ns,
        x,
        y;

    for (ns in mw.config.get('wgNamespaceIds')) {
        if (i === 4) {
            userstr = ns;
        }
        i++;
    }
    userstr = userstr.charAt(0).toUpperCase() + userstr.substring(1);

    if (highlight['selectAll']) {
        selector = 'a[href$=":';
    } else {
        selector = 'a[href="/wiki/' + userstr + ':';
    }

    for (y in highlight) {
        indices.push(y);
    }

    for (x in highlight) {
        that = highlight[x];

        if (x === 'selectAll') {
            continue;
        } else if (x === 'users') {
            for (user in that) {
                $(selector + user.replace(/ /g, '_') + '"]').css({
                    'color': that[user],
                    'font-weight': 'bold'
                }).attr('data-highlight-index',
                    $.inArray('users', indices));
            }
        } else {
            (function (userColor, userGroup) { //JavaScript doesn't like to cooperate with me
                $.getJSON('/api.php?action=query&list=allusers&augroup=' + userGroup +
                    '&aulimit=max&format=json', function (data) {
                        var stuff = data.query.allusers, //, select = '';
                            user;

                        for (user in stuff) {
                            //select += selector + stuff[user].name.replace(/ /g, '_') + '"], ';
                            $(selector + stuff[user].name.replace(/ /g, '_') + '"]').each(function () {
                                if (($(this).attr('data-highlight-index') || -1) < $.inArray(userGroup, indices)) {
                                    $(this).attr('data-highlight-index', $.inArray(userGroup, indices));
                                    $(this).css({
                                        'color': userColor,
                                        'font-weight': 'bold'
                                    });
                                }
                            });
                        }
                        //select = select.substring(0, select.length - 2);
                        //$(select).css('color', userColor);
                    });
            }(that, x));
        }
    }
}

这是我最新的草稿,我设法完成了一些事情,比如将名称加粗,纠正语法错误,但我决定从头开始可能比尝试理解别人的代码更好.

【问题讨论】:

  • 如果你能分享你迄今为止尝试过的东西,那就太好了。请注意,由于它返回 JSON,因此您可以像访问任何其他 javascript 对象一样访问此数据。

标签: javascript jquery mediawiki wiki mediawiki-api


【解决方案1】:

我更喜欢使用 jQuery AJAX 功能。

用法很简单:

$.ajax({   
 url : 'api.php',   
 type : 'post',  
 datatype : 'json',   
 data : {
   'list' : allusers,
   'augroup' : 'sysop'   
 }, 
  success : function(success_record) {    
    //here you can do Js dom related modifications like changing color etc.
    // after php(server side) completes 
  } 
});

【讨论】:

  • 这看起来不错。比我尝试使用的要好得多!我试了一下。
  • @RyanayR 很酷,希望对您有所帮助
【解决方案2】:

我尝试了 Markrand 描述的 AJAX 解决方案,但不幸的是我无法让它工作。首先我得到“allusers is not defined”,所以我把它用引号括起来,这样它就不会被当作一个var,然后我不得不将'api.php'添加到'/api.php',因为它是成为不存在的“/wiki/api.php”,并添加斜杠使其使用基本 URL。然后它会执行并返回一个对象,但是我可以使用的对象中没有任何有用的东西(例如用户名数组),它给我的只是 API 文档......所以我最终这样做了:

function highlightAdmins() {
    $.getJSON('/api.php?action=query&list=allusers&augroup=sysop&aulimit=max&format=json',
        function(data) {
            for (var i = 0; i < data.query.allusers.length; i++) {
                $('a[href$="User:' + data.query.allusers[i].name + '"]').css('color', '#FF6347');
            }
        });
}

这给了我一个包含查询结果的对象,在本例中是一组 sysop 用户名 (data.query.allusers[i].name),我可以对其进行迭代并执行操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2023-02-11
    相关资源
    最近更新 更多