如何使用Userscript停止执行功能

时间:2016-01-21 07:40:18

标签: javascript greasemonkey

我想写一个用户脚本的网站有类似的东西:

p.Stream = p.Class.extend({
         .
         .
         .
_processResponse: function(data) {
    if (!data.items || !data.items.length) {
        return null;
    }
    this.reached.start = data.atStart || this.reached.start;
    this.reached.end = data.atEnd || this.reached.end;
    var oldestId, newestId;
    if (this.options.promoted) {
        data.items.sort(p.Stream.sortByPromoted);
        oldestId = data.items[data.items.length - 1].promoted;
        newestId = data.items[0].promoted;
    } else {
        data.items.sort(p.Stream.sortById);
        oldestId = data.items[data.items.length - 1].id;
        newestId = data.items[0].id;
    }
    var position = (oldestId < this._oldestId) ? p.Stream.POSITION.APPEND : p.Stream.POSITION.PREPEND;
    this._oldestId = Math.min(this._oldestId, oldestId);
    this._newestId = Math.max(this._newestId, newestId);
    var prev = null;
    var itemVotes = p.user.voteCache.votes.items;
    for (var i = 0; i < data.items.length; i++) {
        var item = data.items[i];
        item.thumb = CONFIG.PATH.THUMBS + item.thumb;
        item.image = CONFIG.PATH.IMAGES + item.id;

        item.fullsize = item.fullsize ? CONFIG.PATH.FULLSIZE + item.fullsize : null;
        item.vote = itemVotes[item.id] || 0;
        this.items[item.id] = item;
    }
    return position;
}
});

我想操纵_processResponse,使item.image指向另一个来源。用户脚本可以实现吗?我尝试覆盖某些网站上所述的功能,但这并不像预期的那样有效。我只想覆盖这个功能,除此之外。

1 个答案:

答案 0 :(得分:1)

据我所知,最简单,最灵活的方法是将_processResponse包装在调用原始函数后在data上进一步操作的函数中:

var oldfunc = p.Stream.prototype._processResponse;
p.Stream.prototype._processResponse = function(data) {
    var ret = oldfunc.apply(this, arguments);
    if(data.items && data.items.length)
    {
        for(var i = 0, len = data.items.length; i < len; ++i)
        {
            data.items[i].image = 'https://example.com/a.png'; // or whatever
        }
    }
    return ret;
}