After reading more tutorials, I think I got things all wrong.
Before first getting HTML code, I first need to a add a content page and use it instead of the background one.
Did just that but for some reason I could not establish a connection between the bg page and content page.
Here is my code:
Manifest page:
{
"name": "x",
"description": "x",
"version": "0.1",
"permissions": ["contextMenus", "tabs", "notifications"],
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["jquery.js", "content_script.js"]
}
],
"background": {
"scripts": ["sample.js"]
},
"manifest_version": 2
}
Content Page:
//copied from google tutorials
chrome.extension.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: "goodbye"});
});
background page
function genericOnClick(info, tab)
{
//copy pasted from google tutorials. My own code also didn't work
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
}
// Create a parent item and two children.
var parent1 = chrome.contextMenus.create({"title": "Rank Trigger", "contexts":["all"]});
var child1 = chrome.contextMenus.create
(
{"title": "Rank 1", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
{"title": "Rank 2", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
{"title": "Rank 3", "contexts":["all"], "parentId": parent1, "onclick": genericOnClick}
);
Any ideas please? Maybe cuz the event is triggered from the context menu but I am not too sure. I am new to JS and chrome extensions programming.
Thanks