chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log(sender.tab ?"from a content script:" + sender.tab.url : "from the extension");
if(request.greeting == "hello")
sendResponse({farewell: "这是background.js发出的farewell "+JSON.stringify(sender.tab.id)});
});
//只要当前页面不关闭,那tab.id就不会变;关闭再新开,tab.id就变成新的了
//但经过测试,刷新页面,frameId会变
//打开百度翻译,JSON.stringify(sender) 返回的是下面的值
/* {
"id": "pknlpddjpejdkiebokcdcfdekpaeejnh", //这是插件的id
"url": "http://fanyi.baidu.com/translate?aldtype=16047&query=audible&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/audible", //当前 网页 或 iframe 的网址,和下面的url地址有可能不是同一个
"tab": {
"active": true,
"audible": false,
"autoDiscardable": true,
"discarded": false,
"favIconUrl": "http://fanyi.bdstatic.com/static/translation/img/favicon/favicon_d87cd2a.ico",
"height": 921,
"highlighted": true,
"id": 94,
"incognito": false,
"index": 8,
"mutedInfo": {
"muted": false
},
"pinned": false,
"selected": true,
"status": "loading",
"title": "百度翻译",
"url": "http://fanyi.baidu.com/translate?aldtype=16047&query=audible&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/audible", //当前网页的网址
"width": 1354,
"windowId": 2
},
"frameId": 0
}
*/