2016-08-18 41 views
0

我只有在加載谷歌SERP頁面時纔會看到此行爲(從google.com/上的搜索框或SERP頁面上的搜索框中進行搜索時)自)。我看到其他頁面的預期行爲,即我看到未定義,加載並最終完成。tabs.onUpdated.addListener的changeInfo.status從未定義到完成而無需加載

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ 
console.log(changeInfo.status); // prints undefined couple of times and then completed (no sign of loading). 
} 

我工作的擴展行爲根據標籤狀態而有所不同。

相同的代碼在chrome中按預期工作。

還按照預期刷新SERP頁面觸發加載和完成狀態。

+0

這不包含實際問題。請澄清您的具體問題或添加其他詳細信息,以突出顯示您的需求。正如目前所寫,很難確切地說出你在問什麼。請參閱[問]頁面以獲得澄清此問題的幫助。正如它所寫,所有這個問題都會告訴我們一些在Google Chrome和Firefox上不同的行爲。 – Makyen

+0

如果你需要幫助,我們需要知道你是如何使用這些信息的,以及爲什麼你*有* [changeInfo.status'](https://developer.mozilla.org/en-US/Add -on/WebExtensions/API/tabs/onUpdated)從'undefined'''loading''''''完成''而不是'undefined''''完成''轉換。 – Makyen

+0

相關:[WebExtension:當一個標籤重新加載時的反應](http://stackoverflow.com/q/38407703/3773011) – Makyen

回答

1

Firefox和Google Chrome是不同的瀏覽器。 WebExtensions對於Firefox來說非常新穎。即使成熟,Firefox和Google Chrome也會有所不同。這可能要求您爲擴展程序應運行的各種瀏覽器設置截然不同的代碼段。

目前在Firefox中,傳遞給偵聽器函數chrome.tabs.onUpdatedchangeInfo對象可能無效。它應該只有two possible valuesloadingcomplete

看起來有一個錯誤,使得changeInfo可以具有屬性status,但該屬性的值是undefined。在這些情況下,tabs.Tab對象也會傳遞給tabs.onUpdated偵聽器,其屬性status的值爲字符串。在大多數情況下,該值爲'completed',即使這看起來不準確。

看起來像這樣可能能夠相對容易地工作。這取決於你需要什麼。

下面的代碼將覆蓋changeInfo.status錯誤undefined值的假設,他們是'loading'

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { 
    if(changeInfo.hasOwnProperty("status") && typeof changeInfo.status === 'undefined'){ 
     changeInfo.status = 'loading'; 
    } 
}); 

現實情況是,你需要自己確定爲您的擴展是什麼在起作用。 Firefox和Chrome會給你的擴展提供不同的信息。

對於您所描述(谷歌主頁,以SERP頁)合併webRequestwebNavigation,並且tabs.onUpdated事件爲您提供以下信息的情況:從運行extension I put in this answer獲得

 webRequest: https://www.google.com/images/nav_logo242.png 
     webRequest: https://www.google.com/complete/search?sclient=psy-ab&site=&source=hp&q=SERP&oq=&gs_l=&pbx=1&bav=on.2,or.&bvm=bv.129759880,d.cGc&fp=1&biw=1256&bih=924&dpr=1&pf=p&gs_rn=64&gs_ri=psy-ab&cp=4&gs_id=kv&xhr=t&tch=1&ech=1&psi=E6u2V_S8BY_gjwPakICACA.1471589139270.1 
     webRequest: https://www.google.com/gen_204?atyp=i&ct=1&cad=1&rsm=6&ei=E6u2V_S8BY_gjwPakICACA&zx=1471589794481 

tabs.updated event: tabId=37:: changeInfo keys:status ::changeInfo.status=undefined (override->loading) 
tab.status=complete (override->loading)testTabEvents.js:96:9 

tabs.updated event: tabId=37:: changeInfo keys:status,url ::changeInfo.status=complete 
tab.status=complete 
tab.url=https://www.google.com/?gws_rd=ssl#q=SERPtestTabEvents.js:96:9 

     webRequest: https://www.google.com/search?sclient=psy-ab&site=&source=hp&q=SERP&oq=SERP&gs_l=hp.3..0i3k1j0l3.655013.655013.0.657681.1.1.0.0.0.0.173.173.0j1.1.0....0...1c.1.64.psy-ab..0.1.163.LqQ90ZRWj7o&pbx=1&bav=on.2,or.&bvm=bv.129759880,d.cGc&fp=1&biw=1256&bih=924&dpr=1&tch=1&ech=1&psi=E6u2V_S8BY_gjwPakICACA.1471589139270.3 

    webNavigation->ReferenceFragmentUpdated: tadId=37:: url=https://www.google.com/?gws_rd=ssl#q=SERPtestTabEvents.js:182:13 

tabs.updated event: tabId=37:: changeInfo keys:status ::changeInfo.status=undefined (override->loading) 
tab.status=complete (override->loading)testTabEvents.js:96:9 

tabs.updated event: tabId=37:: changeInfo keys:status ::changeInfo.status=undefined (override->loading) 
tab.status=complete (override->loading)testTabEvents.js:96:9 

     webRequest: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAJCAYAAAAGuM1UAAAAaUlEQVR4AY3OxQECQRBE0YdHsyc0G9LihkNqOESA92n9j09LlaCnmEzOFGcMFBCxc+Ra44UjElmSiL0iVxOL+LhjLIj7PWIzNCBd9MAk1iObHKCNfSTcYr2wQ0uWjFKmc0VRJNekXWTjDe6gHTKFP5vkAAAAAElFTkSuQmCC 
     webRequest: https://id.google.com/verify/NQAAAB8aqx-h_1Es5zxXakqEQ4VnEYZCXpWW_8Rmrl4pN1yV-s-1d_qg6kBSbexs0XcddYBKPk8NAwLOwyJ-W75hWEw 
     webRequest: https://www.google.com/gen_204?v=3&s=web&atyp=csi&ei=pa22V6CHGoHAjAPhgoPQBg&ei=pa22V6CHGoHAjAPhgoPQBg&cr=r&imp=0&pfa=n.1,ttfc.152,ttlc.0,cbt.96&pfm=n.1,ttfc.152,ttlc.0,cbt.96&pmd=max.17,avg.2,0,1,0,1,0,1,5,0,0,0,0,0,1,1,1,1,1,11,17,1&imn=1&adh=&xjs=dispose.3.11.ifl.1.fpe.1.jsa.1.m.0.lu.0&it=jradf.9&ima=1&rt=ol.553,jsrt.155,prt.557,pprt.557,iml.558,aft.557 
     webRequest: https://www.google.com/xjs/_/js/k=xjs.s.en_US.tUVHO5ck74k.O/m=aspn,crd,sy7,sy268,sy314,sy3,sy9,sy54,sy315,sy316,sy27,sy317,dvl,sy57,sy58,sy300,em11,vs,sy85,sy86,sy88,sy90,sy81,sy83,sy87,sy91,sy78,sy84,sy89,sy92,sy79,tnv,sy46,atn,sy355,d3l,sy93,sy168,sy169,rqa,me/am=AFCSBBCI-H8ICLcQLEgFGBgE/rt=j/d=0/t=zcms/rs=ACT90oG-vU2X0tyaGFDLQbRJNbKCWyOApg 

tabs.updated event: tabId=37:: changeInfo keys:status ::changeInfo.status=undefined (override->loading) 
tab.status=complete (override->loading)testTabEvents.js:96:9 

     webRequest: https://www.google.com/gen_204?atyp=i&ct=slh&cad=&ei=bqW2V8rcMMfkjwOEtKXACQ&s=3&v=2&pv=0.5883361922868646&me=4:1471589138584,e,U&zx=1471589798811 
     webRequest: https://www.google.com/gen_204?atyp=i&ct=slh&cad=&ei=pa22V6CHGoHAjAPhgoPQBg&t=W&s=1&v=2&pv=0.3475507940470587&me=1:1471589797809,x:4053,e,B&zx=1471589801864 

上述事件信息(對於webRequests)以及以下擴展名:

testTabEvents.js(手工修改,我把它放在這個答案,所以可能有錯誤或兩個):

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { 
    let output=""; 
    //We use the properties of "tab" instead of "changeInfo" because in testing it was 
    // clear that changeInfo was not always properly populated. The key(s) may just 
    // exist, but not have any value associated with them. 

    //* 
    //Testing output showing when the event is fired. 
    // This is used to determine, by experimentation, what events to ignore and which 
    // combinations and sequence of events occur during page navigation. 

    function addPropToOutput(prop){ 
     if(tab.hasOwnProperty(prop)){ 
      output += (changeInfo.hasOwnProperty(prop)) ? '\ntab.' + prop + '=' + tab[prop] : ''; 
      if(prop === 'status'){ 
       output += ((changeInfo.hasOwnProperty("status") && typeof changeInfo.status === 'undefined') ? ' (override->loading)' :'') 
      } 
     }else{ 
      output += (changeInfo.hasOwnProperty(prop)) ? '\nchangeInfo.' + prop + '=' + changeInfo[prop] : ''; 
     } 
    } 

    //Want status and url always first 
    changePropertiesOrder.forEach(addPropToOutput); 
    Object.getOwnPropertyNames(changeInfo).forEach(function(prop){ 
     if(changePropertiesOrder.indexOf(prop) === -1){ 
      //Not in the list 
      addPropToOutput(prop); 
     } 
    }); 

    console.log("tabs.updated event: tabId=" + tabId + ":: changeInfo keys:" 
       + Object.keys(changeInfo) 
       + (changeInfo.hasOwnProperty("status") ? ' ::changeInfo.status=' + changeInfo.status : '') 
       + ((changeInfo.hasOwnProperty("status") && typeof changeInfo.status === 'undefined') ? ' (override->loading)' :'') 
       + output 
    ); 
    if(changeInfo.hasOwnProperty("status") && typeof changeInfo.status === 'undefined'){ 
     changeInfo.status = 'loading'; 
    } 
}); 

var webNavEvents = ['BeforeNavigate', 
        'Committed', 
        'Completed', 
        //'CreatedNavigationTarget', //Not supported by Firefox 
        'DOMContentLoaded', 
        'ErrorOccurred', 
        'HistoryStateUpdated', 
        'ReferenceFragmentUpdated' 
        //'TabReplaced' //Not supported by Firefox 
        ]; 

webNavEvents.forEach(function(navType){ 
    browser.webNavigation['on' + navType].addListener(function(type,details){ 
     console.log('\twebNavigation->' + type 
        + ': tadId=' + details.tabId 
        + ':: url=' + details.url 
        + ((typeof details.transitionType === 'string') ? ':: transitionType=' + details.transitionType : '') 

     ); 
    }.bind(undefined,navType)); 
}); 

manifest。json

{ 
    "description": "Test tabs.onUpdated and webNavigation events on page load", 
    "manifest_version": 2, 
    "name": "onUpdated and webNavigation event testing", 
    "version": "0.1", 
    "applications": { 
     "gecko": { 
      "id": "[email protected]", 
      "strict_min_version": "45.0" 
     } 
    }, 
    "permissions": [ 
     "webNavigation", 
     "activeTab", 
     "tabs" 
    ], 
    "background": { 
     "scripts": ["testTabEvents.js"] 
    } 
} 
相關問題