javascript - onMessage.addListener Not Firing -
i can't chrome.runtime.onmessage.addlistener fire no matter examples copy/paste around web, when break them down can. right using:
background js
chrome.runtime.oninstalled.addlistener(function() { var context = "selection"; var title = "tm"; var id = chrome.contextmenus.create({ "title": title, "contexts":[context], "id": "context" + context }); }); chrome.contextmenus.onclicked.addlistener(onclickhandler); function onclickhandler(info, tab) { chrome.windows.create( {url : 'url here'}, function(window) { chrome.tabs.query({active: true, currentwindow: true}, function(tabs) { chrome.tabs.sendmessage(tabs[0].id, {greeting: "hello"}); }); } ); };
content script
chrome.runtime.onmessage.addlistener( function(request, sender, sendresponse) { console.log('onmessage fired'); });
the listener in content script not fire. if try read response sendmessage method 'undefined'. content script loading , functioning correctly apart this. have double checked sending correct tab id grabbing window object well.
all need send selected text on content script. new chrome extensions don't know if missing obvious here!
i believe have race condition here: chrome.windows.create
callback returns page still loading, , there no listener active yet.
to work around it, can reverse direction of connection: send message page background instead. in background, can compare window id of sender newly created window if need discern tabs.
Comments
Post a Comment