0

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.

4
  • we don't have the notion of overriding functions in javascript. you can place a conditional statement that suits your need. less code. more productivity. Commented Jan 21, 2016 at 7:46
  • @RaminOmrani Could you give me an example on how I can accomplish this with an Userscript? Commented Jan 21, 2016 at 7:47
  • I don't know userscript language. but if its the same as javascript its as simple as using an if statement Commented Jan 21, 2016 at 8:05
  • @RaminOmrani OK I am sorry but I think you don't understand my question, the code block in my question is provided on the Website's JavaScript code, the userscript will be installed on the client side and must override the function in order to work as I want it to Commented Jan 21, 2016 at 8:09

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your quick response, but what should arguments be in oldfunc.apply(this, arguments)
@Ybrin developer.mozilla.org/en/docs/Web/JavaScript/Reference/… It's just a thing in JavaScript.
I am sorry if I misunderstood something, but I have now exactly what you have posted in my Script, and I put an alert as a debug message inside function(data) {} but it never gets executed. Am I doing something wrong?
@Ybrin Nope, my bad. p should have been p.Stream.prototype.
Ok now it gets executed but ends up with this error in the debug console: Uncaught ReferenceError: items is not defined, but the original javascript has never got this problem so it seems like this piece of code gets executed too early or so...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.