通过 sdk/system/events 测试 http-on-modify-request 观察器?

Test http-on-modify-request observer via sdk/system/events?

是否可以测试 http-on-modify-request-Observer?

events module 提供了一种 emit(type, event) 方法来创建 Firefox 事件。要测试侦听 http-on-modify-request 的模块,您可以将类型设置为 http-on-modify-request。问题是如何设置event 参数来模仿网络请求?文档说明

event : object
An optional object with data and subject attributes. data refers to a string that you would like to pass through this event. subject should refer to the actual actor/subject of this event (ie: the object emitting the event).

设置 event.data 作为观察者的 data 参数传递,与 f.ex 相同。作为 event.subject 的字符串,但是当我尝试使用 QueryInterface 函数传递对象时,它不起作用:

events.emit("http-on-modify-request", {"subject": {
    QueryInterface : function() {
        return {"URI": {"spec": 'mock://URI.spec'}}
    }}});

要测试的代码与https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/system_events非常相似。


@humanoid 的使用答案

obs.notifyObservers(
    { QueryInterface : function() {
        return {"URI": NetUtil.newURI("mock://URI.spec") };
    }},
    "http-on-modify-request",
    null);

失败

JavaScript Error: "NS_NOINTERFACE: Component returned failure code: 0x80004002 (NS_NOINTERFACE) [nsISupports.QueryInterface]" {file: "resource://gre/modules/commonjs/toolkit/loader.js -> resource://addon-id/caller.js" line: xy}]

sdk/system/events 中的 emit 方法对于创建看起来像来自 Firefox 的通知的观察者通知没有用,因为它添加了包装器以确保弱引用。

相反,我建议使用 "vanilla" observer service,例如 Services.jsm:

const { Services: { obs } } = require("resource://gre/modules/Services.jsm");