2016-04-29 76 views
3

这样做的目标是让我的扩展等待历史更改,具体取决于它所说的某个操作。多个chrome.webNavigation.onHistoryStateUpdated不起作用

这里是我迄今为止

popup.js

chrome.tabs.update({ url: "https://www.WEBSITE.com/messages" }); 

chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) { 
    if (details.url.indexOf("messages") >= 0) { 
     chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, { 
      file: 'getInboxStats.js' 
     });; 
    } else {//if (details.url.indexOf("match") >= 0) { 
     chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, { 
      file: 'startBotting.js' 
     });; 
    } 
}); 

chrome.runtime.onMessage.addListener(function(message) { 
    if (message.type == "emptyAmount") { 
     emptyAmount = message.content; 
     if (!(percentageOfMessages > 0)) { 
      percentageOfMessages = 50; 
     } 
     amountToSend = Math.floor(emptyAmount * (percentageOfMessages/100)); 
     alert(amountToSend); 
     chrome.tabs.update({ url: "https://www.WEBSITE.com/match" }); 
    } 

}); 

getInboxStats.js

var currentAmount = document.getElementsByClassName('count')[1].innerHTML; 
var maxAmount = document.getElementsByClassName('total')[0].innerHTML; 
var emptyAmount = maxAmount - currentAmount; 

chrome.runtime.sendMessage({ content: emptyAmount, type: "emptyAmount" }); 

startBotting.js

alert("TEST"); 

我的问题是,getInboxStats.js开始,但它就像onHistoryStateUpdated似乎只因为文件startBotting.js永远不会显示,上面写着“TEST”

回答

2

你误解的onHistoryStateUpdated目的警报马上开始工作。

它通过历史API捕获history state manipulation without navigation的实例,而不是常规导航。当你拨打update({url: "..."})这是一个定期的导航。

如果你真的关心浏览器历史更新,你应该使用chrome.history.onVisited

如果要使用webNavigation API捕获常规导航,则应使用onCommitted事件。

您还应该查看chrome.tabs API。