拦截Javascript函数调用的正确方式

问题描述 投票:0回答:1

我正在为流行的视频聊天平台 Omegle 编写一个浏览器扩展。 该扩展程序需要访问 omegle 应用程序的内部事件。该应用程序使用 Mootools 来触发事件,并创建一个新的全局对象 (COMETBackend) 继承 mootools 的事件类。我试图通过将此代码注入 DOM 来拦截对 COMETBackend 对象的 fireEvent 函数的函数调用:

let proxy = COMETBackend.prototype.fireEvent;

COMETBackend.prototype.fireEvent =  function (...args) {
    console.log(...args)
    return proxy(...args);
}

Mootools.js 的事件类

var Events = new Class({
    $events: {},
    addEvent: function(c, b, a) {
        c = Events.removeOn(c);
        if (b != $empty) {
            this.$events[c] = this.$events[c] || [];
            this.$events[c].include(b);
            if (a) {
                b.internal = true;
            }
        }
        return this;
    },
    addEvents: function(a) {
        for (var b in a) {
            this.addEvent(b, a[b]);
        }
        return this;
    },
    fireEvent: function(c, b, a) {
        c = Events.removeOn(c);
        if (!this.$events || !this.$events[c]) {
            return this;
        }
        this.$events[c].each(function(d) {
            d.create({
                bind: this,
                delay: a,
                "arguments": b
            })();
        }, this);
        return this;
    },
    removeEvent: function(b, a) {
        b = Events.removeOn(b);
        if (!this.$events[b]) {
            return this;
        }
        if (!a.internal) {
            this.$events[b].erase(a);
        }
        return this;
    },
    removeEvents: function(c) {
        var d;
        if ($type(c) == "object") {
            for (d in c) {
                this.removeEvent(d, c[d]);
            }
            return this;
        }
        if (c) {
            c = Events.removeOn(c);
        }
        for (d in this.$events) {
            if (c && c != d) {
                continue;
            }
            var b = this.$events[d];
            for (var a = b.length; a--; a) {
                this.removeEvent(d, b[a]);
            }
        }
        return this;
    }
});

Omegle.js 的 COMETBackend 对象

var COMETBackend = new Class({
    Implements: [Options, Events],
    initialize: function(a) {
        this.setOptions(a),
            this.clientID = null,
            this.stopped = !1
    }....

现在,在控制台中,拦截似乎已经起作用,因为我得到了我需要的事件日志,但是由于某种原因,omegle 现在没有响应这些事件,所以拦截似乎已经破坏了应用程序的功能。

关于为什么会这样有什么想法吗?我没有正确拦截电话吗?

javascript mootools mootools-events
1个回答
0
投票
let proxy = COMETBackend.prototype.fireEvent;
return proxy(...args);

以这种方式调用代理会导致其“this”参数丢失。相反:

return proxy.apply(this, args)
© www.soinside.com 2019 - 2024. All rights reserved.