我想要顯示我在後臺獲取的數據按鈕時單擊的數組數據按鈕,但沒有做任何事情時,我點擊按鈕是非常新的Chrome瀏覽器分機。謝謝。如何從背景中獲取數據在彈出頁面中顯示
的manifest.json:
{
"name":"modz",
"manifest_version":2,
"version":"1.0",
"description":"this ext. will help you record all the urls you have visited",
"background": {
"scripts": ["background.js"]
},
"browser_action":
{
"default_icon":"icon.png",
"default_popup":"popup.html"
},
"permissions":[
"tabs"
]
}
background.js:
var data=[];
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
var url = tab.url;
if (url!=="undefined" && changeInfo.status=="complete")
{
data.push (tab.url);
alert(data.length);
}
}
);
chrome.runtime.onMessage.addListener(function(message,sender,sendrespose){
//send the array data back
});
popup.js:
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('btn').addEventListener('click',function(){
chrome.runtime.sendMessage("getdata");
});
});
popup.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="C:\Users\modz\Desktop\modz_extention\popup.js"></script>
<style type="text/css">
body{
width:440px;
}
</style>
</head>
<body>
<input id="btn" type="button" value="get data" />
<div id="data"></div>
</body>
</html>
在[sendMessage](https://developer.chrome.com/extensions/runtime#method-sendMessage)中包含對數據執行某些操作的響應函數。然後background.js調用'sendrespose(data)'。 – Teepeemm
可以請給我一個代碼片段,因爲我很新的鉻合金分機和按鈕dosent工作可以請你幫我,謝謝你 –
我會推薦[這個答案](http://stackoverflow.com/a/ 16325442/2336725)或[此問題](http://stackoverflow.com/q/16066317/2336725)。 – Teepeemm