I'm trying to create a new tab, then execute a trivial code in it (i.e. an alert).
I'm using executeScript
method for this programmatic Injection
operation.
The tab is created successfully, but the alert is not displayed !
manifest.json
{
"name" : "TabCreatorAlerter",
"description" : "Opens a tab and shows an alert in it !",
"version" : "1.0",
"manifest_version" : 2,
"background" : {
"scripts" : ["background.js"],
"persistent" : false
},
"permissions" : ["tabs"]
}
background.js
chrome.tabs.create(
{url:"http://www.google.com"},
function(createdTab) {
chrome.tabs.executeScript(
createdTab.id,
{code:"alert('hi');"}
);
}
);
What is wrong with this code ? and how to fix it ?