Communication between background page and popup page in a Chrome Extension

前端 未结 1 482
广开言路
广开言路 2021-02-05 22:40

I\'m currently trying to write an extension for Google Chrome, which can be used to upload files.

There are two pages: the background page and the popup page. The popup

相关标签:
1条回答
  • 2021-02-05 23:26

    Define it as a variable.

    background.js

    upload = function(fileName) {
      console.log('Uploading', fileName);
    }
    

    popup.html

    <script src="popup.js"></script>
    

    popup.js

    var BGPage = chrome.extension.getBackgroundPage();
    BGPage.upload(the_filename);
    

    That should work. If it doesn't, then check your inspector for the popup and background page:

    • Popup Inspector: Right click on the popup and choose Inspect
    • Background Inspector In your extension settings page chrome://extensions, turn on developer mode (check top right), and click on background.js.

    It will open the inspector, then click on console to see the error messages in the console to assist you further, doing what I stated above should work, I do it all the time.

    0 讨论(0)
提交回复
热议问题