2010-06-14 73 views
2

我是新來的Chrome擴展的發展,我有以下問題: 我的擴展名應該在後臺運行,沒有用戶界面,並且每個用戶訪問時顯示一個警告對話框特定的網頁。所以它應該始終在後臺工作,當瀏覽器執行時。Chrome擴展程序和history.onVisited事件

我用下面的代碼嘗試無果:

的manifest.json

{ 
    "name": "My First Extension", 
    "version": "1.0", 
    "description": "The first extension that I made.", 
    "background_page": "background.html", 
    "permissions": [ 
    "history" 
    ] 
} 

background.html

<html> 
<head> 
<script> 
chrome.history.onVisited.addListener(function(HistoryItem result) { 
    if (result.url == "http://my.url.com") { 
     alert("My message"); 
    } 
}); 
</script> 
</head> 
</html> 

有什麼不對的代碼?

感謝

+0

考慮使用'chrome.tabs.onUpdated'。它返回'changeinfo.url'。順便問題可以通過布拉德利的答案解決。 – 2015-11-02 11:12:25

回答

2

採取HistoryItem出來的功能,你是罰款:

<html> 
<head> 
<script> 
chrome.history.onVisited.addListener(function(result) { 
    if (result.url == "http://my.url.com/") { 
     alert("My message"); 
    } 
}); 
</script> 
</head> 
</html> 

另外請注意,我添加了斜槓的「http://my.url.com/」結束,因爲這是將在結果中返回的.url。

0

測試此:

<script> 
chrome.history.onVisited.addListener(function(e) { console.log(e)}) 
</script> 

這顯然