【问题标题】:How can I resend a post/get data in case of failure?如果失败,我如何重新发送帖子/获取数据?
【发布时间】:2015-09-10 12:51:53
【问题描述】:

我尝试使用addProgressListener,但我不知道如何在onStateChange 中获取发布数据并将其发回。

exports.main = function() {
    var {Cc, Ci, Cu} = require("chrome");
    var windows = require("sdk/windows").browserWindows;
    const windowUtils = require("sdk/window/utils");
    var {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    var { setTimeout, clearTimeout } = require("sdk/timers");
    var gBrowser = windowUtils.getMostRecentBrowserWindow().getBrowser();
    var tabs = require("sdk/tabs");
    const STATE_START = Ci.nsIWebProgressListener.STATE_START;
    const STATE_STOP = Ci.nsIWebProgressListener.STATE_STOP;
    const STATE_REDIRECTING = Ci.nsIWebProgressListener.STATE_REDIRECTING;
    const STATE_TRANSFERRING = Ci.nsIWebProgressListener.STATE_TRANSFERRING;
    const STATE_RESTORING = Ci.nsIWebProgressListener.STATE_RESTORING;
    var timer = null;
    var counter =  0;
    var start_url = '';
    var retry = false;

    var myListener = {

        QueryInterface: XPCOMUtils.generateQI(Ci),
        onState: function(aWebProgress, aRequest, aFlag, aStatus) {
            console.log('test');
        },
        onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {
            Ci.nsIUploadChannel2
            if(!aRequest.name.startsWith('http')) {
                return false;
            }

            if (!(start_url && (start_url == aRequest.name))) {
                if (!aWebProgress.isLoadingDocument && !retry) {
                    return false;
                }
            }
            retry = false;
            if (aFlag & STATE_START) {
                if (timer) {
                    clearTimeout(timer);
                    timer = null
                    start_url = '';
                    counter = 0
                }
                console.log("start");
                start_url = aRequest.name;
                timer = setTimeout(function() {
                    if (counter < 5) {
                        // console.log("after start");
                        console.log("aRequest.name: ", aRequest.name);
                        new_tab = gBrowser.addTab(aRequest.name);
                        gBrowser.removeCurrentTab();
                        gBrowser.selectedTab = new_tab;
                        retry = true;
                        counter += 1
                        myListener['onStateChange'](aWebProgress, aRequest, 
                                                                  aFlag, aStatus);
                    } else {
                        gBrowser.removeCurrentTab();
                    }
                }, 10000)   

            }
            if (aFlag & STATE_STOP) {
                if (timer) {
                    console.log("stop");
                    clearTimeout(timer);
                    timer = null
                    start_url = '';
                    counter = 0
                }
            }
        },

        onLocationChange: function(aProgress, aRequest, aURI) {
            start_url = aRequest.name;
            console.log('onLocationChange: ', aRequest.name)
        },
        onProgressChange: function(aWebProgress, aRequest, curSelf,
                                                 maxSelf, curTot, maxTot) {
            console.log('onProgressChange: ', aRequest.name)
        },
        onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {
            console.log('onStatusChange: ', aRequest.name)
        },
        onSecurityChange: function(aWebProgress, aRequest, aState) {
            console.log('onSecurityChange: ', aRequest.name)
        }
    }
    gBrowser.addProgressListener(myListener);
}

我也尝试使用observerService,但这些主题都无法跟踪每个请求并制定我的计划。

observerService.addObserver(this, "http-on-opening-request", false);
observerService.addObserver(this, "http-on-examine-merged-response", false);
observerService.addObserver(this, "http-on-examine-cached-responsee", false);
observerService.addObserver(this, "http-on-examine-response", false);

我假设使用nsIUploadChannel可以获取数据请求,但我不明白如何使用。

【问题讨论】:

  • 是的,帖子数据应该在 nsIUploadChannel 中,这是一个示例要点:gist.github.com/Noitidart/…
  • @Noitidart 呀,我看到了,但是我不明白怎么用
  • 好的,我根据您的 sn-p 提供了一个经过测试的解决方案

标签: firefox-addon firefox-addon-sdk mozilla jpm


【解决方案1】:

试试这个,未经测试,甚至没有检查错别字。

onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {
    if (aRequest instanceof Ci.nsIHttpChannel) {
            if (aRequest.QueryInterface(Ci.nsIHttpChannel).uploadStream) {
                // see line 10 - https://gist.github.com/Noitidart/28624e20bfee2dc128c4#file-gistfile1-txt-L10
            }
    }
    if(!aRequest.name.startsWith('http')) {
        return false;
    }

【讨论】:

  • 这是什么this.oHttp?如果我这样做pastebin 我得到Type Error
  • @tany this.oHttp == aRequest.QueryInterface(Ci.nsIHttpChannel)
  • http = aRequest.QueryInterface(Ci.nsIHttpChannel) -> 得到错误is null , this.oHttp -> 得到this.oHttp is undefined
猜你喜欢
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
  • 2012-02-11
相关资源
最近更新 更多