【问题标题】:get html page as response from url inside an extension从扩展中的 url 获取 html 页面作为响应
【发布时间】:2014-05-30 05:29:08
【问题描述】:

我正在开发一个扩展,无论有什么请求,我都想更改请求参数并向服务器发送一个虚拟请求以获得响应。 例如。如果加载的原始请求是 www.abc.com/eg.php?user="some code",那么我想将其更改为 www.abc.com/eg.php?user="ritubala" 并在在我的扩展中进一步处理的响应.. 我使用了以下网址中给出的这些代码 Return HTML content as a string, given URL. Javascript Function 但它导致递归...... 所以简而言之,我想从我的扩展程序中的 url 获取 html 代码...请帮忙

【问题讨论】:

    标签: javascript html http xmlhttprequest firefox-addon


    【解决方案1】:

    使用nsITraceableChannel,注意你会得到一份正在加载的内容的副本。

    如果您中止请求,那么它会中止给您一份副本。

    这是我使用 nsITraceableChannel:https://github.com/Noitidart/demo-nsITraceableChannel

    我想你想要的是这个:

    const { Ci, Cu, Cc, Cr } = require('chrome'); //const {interfaces: Ci, utils: Cu, classes: Cc, results: Cr } = Components;
    Cu.import('resource://gre/modules/Services.jsm');
    Cu.import('resource://gre/modules/devtools/Console.jsm');
    
    var observers = {
        'http-on-examine-response': {
            observe: function (aSubject, aTopic, aData) {
                console.info('http-on-modify-request: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData);
                var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
                var requestUrl = httpChannel.URI.spec
                if (requestUrl.indexOf('blah')) {
                    //httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this is how you abort but if use redirectTo you don't need to abort the request
                    httpChannel.redirectTo(Services.io.newURI('http://www.anotherwebsite/', null, null));
                }
            },
            reg: function () {
                Services.obs.addObserver(observers['http-on-modify-request'], 'http-on-modify-request', false);
            },
            unreg: function () {
                Services.obs.removeObserver(observers['http-on-modify-request'], 'http-on-modify-request');
            }
        }
    };
    
    
    
    //register all observers
    for (var o in observers) {
        observers[o].reg();
    }
    

    请注意,redirectTo 函数适用于 FF20+ https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel#redirectTo%28%29

    因此,将此示例与 nsITraceableChannel 演示结合起来,您将拥有您想要的。

    如果您不想重定向,您可以中止请求,获取请求的文档,然后 xmlhttprequest 新页面,它首先为您提供源并且不将其加载到选项卡,然后修改源然后加载它返回文档。

    OR 代替 xmlhttprequesting 它,只是中止请求,重定向到新页面,然后修改选项卡。要获取频道的选项卡,请在此处查看解决方案:

    Security Error when trying to load content from resource in a Firefox Addon (SDK)

    还可以从扩展执行 http 请求,请参阅这个 sn-p:https://gist.github.com/Noitidart/9088113

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-25
      • 2015-05-18
      • 2016-09-28
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 2017-05-08
      相关资源
      最近更新 更多