Chrome extension: sendMessage doesn't work

后端 未结 3 1604
说谎
说谎 2021-01-06 09:28

I\'ve already read the documentation from Google on \'message passing\' a few times and have probably looked at over 10 other questions with the same problem and already tri

3条回答
  •  既然无缘
    2021-01-06 10:27

    There are several issues in your code.

    Chrome doesn't allow inline scripts in extensions. You must divide your popup.html to script + HTML:

    // popup.html
    
    
    
    
    
    
    // popup.js
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        var tab = tabs[0];  // do not forget to declare "tab" variable
        chrome.tabs.sendMessage(tab.id, {
            greeting: "Can you hear me?"
        }, function(response){});
    });
    

提交回复
热议问题