javascript - Can not establish a connection between background page and content page -
i writing chrome extension , trying send message between background pages , content pages.
problem connection never established. copied code google documentations no avail.
here 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 }
my background page
function genericonclick(info, tab) { //copy pasted google tutorials. own code didn't work chrome.tabs.getselected(null, function(tab) { chrome.tabs.sendmessage(tab.id, {greeting: "hello"}, function(response) { console.log(response.farewell); }); }); } // create parent item , 2 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} );
content script:
//copied google tutorials chrome.extension.onmessage.addlistener( function(request, sender, sendresponse) { console.log(sender.tab ? "from content script:" + sender.tab.url : "from extension"); if (request.greeting == "hello") sendresponse({farewell: "goodbye"}); });
any ideas please? maybe cuz event triggered context menu not sure. new js , chrome extensions programming.
thanks
set onclick
handler on element , utilize event.currenttarget.outerhtml
html of clicked element.
javascript html events dom google-chrome-extension
No comments:
Post a Comment