{
//Manifest.json
"name": "Stampa cedolini",
"description": "stampa automatica dei cedolini",
"version": "1.0",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"content_scripts": [{
"matches": [ "http://*/*", "https://*/*"],
"js": [ "jquery-2.1.3.min.js" ,"content.js"]
}],
"browser_action": {
"default_title": "Scegli la persona.",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"manifest_version": 2
}
popup.js:
function click(e) {
var link1 = "http://www.example1.it";
var link2 = "http://www.example2.com";
if (e.target.id === "pippo") {
chrome.tabs.create({ url: link1 }, function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
// your code ...
chrome.tabs.executeScript({code:"console.log('"+tabs[0].id+"')"});
//chrome.tabs.executeScript({code:"alert("+tabs[0].id+");"});
chrome.tabs.sendMessage(tabs[0].id, {persona: "pippo"});
});
});
} else {
chrome.tabs.create({ url: link2 });
}
//window.close();
}
document.addEventListener('DOMContentLoaded', function () {
var divs = document.querySelectorAll('div');
for (var i = 0; i < divs.length; i++) {
divs[i].addEventListener('click', click);
}
});
以及 content.js:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
alert("from a content script:" + sender.tab.url);
if (request.persona == "pippo") {
sendResponse({risp: "ricevuto"});
}
});
在tabs.create回调函数中,console.log(tab.id)被打印,但似乎发送消息失败了。有人可以帮忙吗?谢谢。