gpt4 book ai didi

firefox-addon - Firefox 插件 - 监控网络

转载 作者:行者123 更新时间:2023-12-03 14:12:05 26 4
gpt4 key购买 nike

如果我添加一个 nsIHttpChannel 观察者,有什么办法可以知道是什么发起了 HTTP 请求(脚本、iframe、图像等)

在 chrome 中,当从后台页面监控网络时,您有请求类型告诉您它是否来自 iframe、脚本等...

最佳答案

不要接受这个作为解决方案。我希望其他一些人可以来帮助构建这个解决方案。

我知道这肯定是正确的:

  • 测试:XHR - identify XHR (ajax) response while listening to http response in firefox addon
  • 获取请求的加载上下文(例如哪个选项卡,哪个 html 窗口,哪个 xul 窗口)- Firefox add-on pageMod, catch ajax done in contentScriptFile (这被标记为可选,但在下面的代码中它需要一个辅助函数: https://gist.github.com/Noitidart/644494bdc26f996739ef )

  • 我认为这是正确的,我的意思是它适用于我的测试用例,但我不确定它是否是推荐的方式:
  • 测试:框架或整页加载 - Can we differentiate between frame and non-frame loads with Ci in firefox addon

  • 我不知道该怎么做,所以我需要社区的帮助:
  • 图像测试 - Comment on "identify XHR (ajax) response while listening to http response in firefox addon"
  • Image detection can be done through the MIME type. Check channel.contentType



  • 解决方案进行中:
    var myobserve = function(aSubject, aTopic, aData) {
    var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);


    //start - test if xhr
    var isXHR;
    try {
    var callbacks = httpChannel.notificationCallbacks;
    var xhr = callbacks ? callbacks.getInterface(Ci.nsIXMLHttpRequest) : null;
    isXHR = !!xhr;
    } catch (e) {
    isXHR = false;
    }
    //end - test if xhr

    //start - test if frame OR full page load
    var isFrameLoad;
    var isFullPageLoad;
    if (httpChannel.loadFlags & Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI) {
    isFullPageLoad = true;
    isFrameLoad = false;
    } else if (httpChannel.loadFlags & Ci.nsIHttpChannel.LOAD_DOCUMENT_URI) {
    isFrameLoad = true;
    isFullPageLoad = false;
    }
    //end - test if frame OR full page load

    //start - test if image
    var isImg;
    var isCss;
    var isJs;
    var isAudio;
    //can keep going here
    var mimeType = httpChannel.contentType;
    if (/^image/i.test(mimeType)) {
    isImg = true;
    }
    if (/^audio/i.test(mimeType)) {
    isAudio = true;
    }
    if (/\/css$/i.test(mimeType)) {
    isCss = true;
    }
    if (/\/js$/i.test(mimeType)) {
    isJs = true;
    }
    //end - test if image

    //start - OPTIONAL use loadContext to get a bunch of good stuff
    //must paste the function from here: https://gist.github.com/Noitidart/644494bdc26f996739ef somewhere in your code
    var goodies = loadContextAndGoodies(aSubject, true);
    /*
    //goodies is an object that holds the following information:
    var goodies = {
    loadContext: loadContext,
    DOMWindow: DOMWindow,
    gBrowser: gBrowser,
    contentWindow: contentWindow,
    browser: browser,
    tab: tab
    };
    */
    // test if resource (such as image, or whatever) is being loaded is going into a frame [can also be used as altnerative way to test if frame load or full page]
    var itemDestinationIsFrame;
    var itemDestinationIsTopWin;
    if (goodies.contentWindow) {
    if (goodies.contentWindow.frameElement) {
    itemDestinationIsFrame = true;
    itemDestinationIsTopWin = false;
    } else {
    itemDestinationIsFrame = false;
    itemDestinationIsTopWin = true;
    }
    }
    //end - OPTIONAL use loadContext to get a bunch of good stuff
    }

    当然要开始观察:
    Services.obs.addObserver(myobserve, 'http-on-modify-request', false);

    并停止:
    Services.obs.removeObserver(myobserve, 'http-on-modify-request', false);

    开始观察

    要开始观察所有请求,请执行此操作(例如在您的插件启动时)
    for (var o in observers) {
    observers[o].reg();
    }

    停止观察

    停止观察很重要(确保至少在插件关闭时运行它,你不想因为内存原因而让观察者注册)
    for (var o in observers) {
    observers[o].unreg();
    }

    关于firefox-addon - Firefox 插件 - 监控网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27483651/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com