javascript - Simple message passing between 'background.js' to 'contentScript.js' -
I am trying to implement the following flow:
key in the background For bindings .js , when they are pressed: Message from
background.js -> is being sent from ContentScript.js response
contentScript.js -> gt; ; Background. Js
Here are the definitions:
"background": {"script": ["" [" *: // * / * "]," js ": [" contentScript.js "]}, ," / code> The binding part is working fine.
Here is the code background.js :
Chrome.runtime.sendMessage ({greeting: "hello"}, function (feedback) {console. Log (response.farewell);});}); Here the code is contentScript.js :
chrome.runtime .onMessage.addListener (HandleMessage); Function Handle Message (Request, Sender, Send Response) {If (request.greeting == "Hello") {sendResponse ({Vida : "Goodbye"});}}; This error is that I am getting:
Error in event handler for: Undefined Stack Trace's 'Farewell' property can not be read: Type Error: Undefined property 'farewell' can not be read on Chrome extension: //fejkdlpdejnjkmaeadiclinbijnjoeei/background.js: 64: 26 at Function.target at (connection :: Message: 338: 9) disconnectListener (anonymous function) (extension :: workbooks: 19:14) on Event.mpl.dispatchToListener (extension :: event_bending S: 397: 22) on Function.target. (Anonymous function) (extension :: SafeBillins: 19:14) at Event.publicClass. (Extension :: Event_Bankings: 403: 17) [as an anonymous dispatch] (Extension :: UTL: 93: 26) on Function.target (anonymous function) on EventAmpl.dispatch_ (Extension :: Events_bankings: 37 9:35) In case of change / P>
console.log (response.f is fine); to
console.log ("fix"); This is working fine, but by the way I contentScript.js < / P>
You are trying to send messages from the background page to the content script. From, you are using this code snippet to send messages from the background page:
chrome.runtime.sendMessage ({greeting: "hello"}, function (response) {console. Log (Response.farewell);}); However, docs clearly mentions that the above code is sending to the message to a content script. This is probably the reason that you might get an error: Error in event handler for / p> (unknown): 'Farewell' property of undefined stack trace can not be read: TypeError: 'Farewell' of property can not be read What you want to achieve, One Content Scan from Extensions (Background Page) Sending request on the replay, for which you need to specify the content in which the content script exists: chrome.tabs.query ({active: true, currentWindow: true} , Function (tab) {chrome.tabs.sendMessage (tab [0] .id, {greeting: "hello"}, function (feedback) {console.log (response.farewell);});}); I hope this clarifies things and starts you in the right direction.
Comments
Post a Comment