【问题标题】:Tab readystate returns 'complete' before first server response选项卡就绪状态在第一次服务器响应之前返回“完成”
【发布时间】:2015-04-16 17:03:18
【问题描述】:

我有一个按钮激活的插件。代码如下:

function handleClick(state) {
activeTab = tabs.activeTab;
  console.log('readystate: ' + activeTab.readyState);
  if(activeTab.readyState != 'complete')
  {
    activeTab.on('load', function(){
      activeTab.removeListener(this);
      start();
    });
  }
  else
    start();
}

然而,不知何故,每当我向选项卡询问其当前的就绪状态时,上述代码总是返回“完成”。我绝对确定页面仍在加载,微调器仍在运行,我实际上启用了 XDebug 的 PHP 断点,以确保我的页面在我想要它之前永远不会完成加载。根据Mozilla tabs docs,readystate 应该是“正在加载”,因为 PHP 甚至还没有完成,所以服务器没有发回任何 HTML,所以甚至没有 DOM。

更新:到目前为止,我发现就绪状态会返回“完成”,直到您获得第一个服务器响应。一旦初始服务器响应返回到浏览器,就绪状态将更改为“正在加载”。不知道如何从那里继续。

【问题讨论】:

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


    【解决方案1】:

    然后检查 readyState 属性检查进度属性,我怀疑 readyState 在 DOMContentReady 之前完成触发后,所以在完成后您可能必须为 DOMContentReadyload 添加事件侦听器

    var tab = gBrowser.loadOneTab('data:text/html,<div class="profilist-icon-build-1">backround of this span is of icon on desktop</div><input type="button" value="applyCss"><input type="button" value="removeCss"> Change File on Desktop to: <input type="button" value="Release Img"><input type="button" value="Beta Img"><input type="button" value="Aurora Img"><input type="button" value="Nightly Img">', {inBackground:false});
    //start - listen for loadOneTab to finish loading per https://gist.github.com/Noitidart/0f076070bc77abd5e406
    var mobs = new window.MutationObserver(function(mutations) {
      mutations.forEach(function(mutation) {          
        if (mutation.attributeName == 'progress' && tab.getAttribute('progress') == '') {
          //alert('tab done loading');
          init();
          mobs.disconnect();
        }
    
      });
    });
    mobs.observe(tab, {attributes: true});
    //end - listen for loadOneTab to finish loading per https://gist.github.com/Noitidart/0f076070bc77abd5e406
    function init() {
      //run on load of the loadOneTab
      var win = tab.linkedBrowser.contentWindow;
      var doc = win.document;
    
      var btns = doc.querySelectorAll('input[type=button]');
      Array.prototype.forEach.call(btns, function(b) {
        b.addEventListener('click', handleBtnClick, false);
      });
    
    }
    

    【讨论】:

    • 我试过实现这个,但我不确定它是否能解决我的问题。上下文是我不确定页面是否已完成加载,如果不是,我不希望我的代码启动。但如果是的话,这段代码还能用吗?如果它准备好了,它可能不会触发任何突变。在任何情况下,我都尝试使用 activeTab.window 属性来创建 MutationObserver 的实例,但无济于事。我应该如何在我的附加代码中访问此方法?
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2021-07-10
    • 1970-01-01
    • 2020-05-09
    • 2017-10-24
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多