javascript - Messaging between content script and popup script in Chrome extension -


i'm trying create popup chrome extension shows information dom in current page, seems require messaging. i've been able send messages background, need data specific current page, background identical popups/pages.

in popup.js, send message when dom loaded (should trigger when popup clicked?)

document.addeventlistener('domcontentloaded', function() {     chrome.runtime.sendmessage({method: "gettabledata"}, function response() {     }); }); 

i have listener in contentscript.js (and background.js testing)

chrome.runtime.onmessage.addlistener(function(request, sender, sendresponse) {   if(request.method == "gettabledata") {     console.log("table request found!");   } }); 

then, when activate popup, background console outputs table request found!, while console current page doesn't.

thanks.

you need use chrome.tabs.sendmessage instead of chrome.runtime.sendmessage send message content script.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -