只有当google.com页面正在加载时(从google.com/或SERP页面的搜索框搜索它自己),我才会看到这种行为。我看到了其他页面的预期行为,也就是,我看到了未定义的、加载的,并最终完成了。
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页面会触发加载并按预期完成状态。
发布于 2016-08-19 07:52:09
Firefox和Google是不同的浏览器。WebExtensions对于火狐来说是非常新的。即使是在成熟的时候,Firefox和Google也会有所不同。这可能要求您为您的扩展应该在不同的浏览器上运行的代码有明显不同的部分。
目前,在火狐中,传递给chrome.tabs.onUpdated
侦听器函数的chrome.tabs.onUpdated
对象可能无效。它应该只有两个可能的值:loading
或complete
。
似乎有一个bug使得changeInfo
可以拥有属性status
,但是该属性的值是undefined
。在这些情况下,tabs.Tab
对象with也被传递给tabs.onUpdated
侦听器,它有一个属性status
,其值是一个字符串。在大多数情况下,这个值是'completed'
,即使这看起来不准确。
看起来,这可能会相对容易地解决问题。这取决于你需要什么。
下面的代码将覆盖changeInfo.status
的错误changeInfo.status
值,假设它们是'loading'
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(changeInfo.hasOwnProperty("status") && typeof changeInfo.status === 'undefined'){
changeInfo.status = 'loading';
}
});
现实情况是,您需要自己确定什么对扩展有效。Firefox和Chrome将给你的扩展提供一些不同的信息。
对于您描述的情况(谷歌主页到SERP页面),组合的webRequest
、webNavigation
和tabs.onUpdated
事件提供了以下信息:
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": "onUpdatedWebNavigationEventTesting@testing",
"strict_min_version": "45.0"
}
},
"permissions": [
"webNavigation",
"activeTab",
"tabs"
],
"background": {
"scripts": ["testTabEvents.js"]
}
}
https://stackoverflow.com/questions/39028894
复制相似问题