Passing message from background.js to popup.js

后端 未结 4 1485
无人共我
无人共我 2020-12-05 10:12

I\'m trying to implement my own chrome extension on which, on a certain event, create a browser notification and fills the popup with data calculated in background.js

<
4条回答
  •  情书的邮戳
    2020-12-05 11:03

    To solve this you need to first send a handshake message to background.js and then send the actual data from background.js to popup.js For Example: In my case what i did was

    popup.js

    chrome.runtime.sendMessage({data:"Handshake"},function(response){
    	
    			});
    chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
    	str = JSON.stringify(message.data);
    });

    background.js

    chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
    //alert(message.data);
    	chrome.runtime.sendMessage({data:datax},function(response){
    			});
    			});

    What iam trying to do is that as soon as we click on icon the handshake message is sent to the background.js and when it recieves it we can then send the variable or any data whick we wanted to send on popup.js to render it on popup.html.

提交回复
热议问题