Ybrin
Ybrin

Reputation: 911

How to stop execution for function with Userscript

The Website I want to write an Userscript for has something like that:

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;
}
});

I want to manipulate the _processResponse such that item.image points to another Source. Is this possible with Userscripts? I tried overriding the function like stated on some websites but that does not work like expected. I just want to override this function, nothing other than that.

Upvotes: 0

Views: 142

Answers (1)

Siguza
Siguza

Reputation: 23840

As far as I can see, the easiest and most flexible way to do that would be to wrap _processResponse in a function that further operates on data after the original function is called:

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;
}

Upvotes: 1

Related Questions