Loading external javascript in google chrome extension

后端 未结 3 1732
暗喜
暗喜 2020-11-28 04:51

I\'m writing a Google Chrome extension which manipulates the current page (basically adds a button).

In my content script, I want to load the Facebook Graph API:

3条回答
  •  有刺的猬
    2020-11-28 05:19

    If you want to load it as content script:

    fetch('https://example.com/content.js')
      .then(resp => resp.text())
      .then(eval)
      .catch(console.error)
    

    If you want to load it as background script. Take https://example.com/bg.js for example.

    1. add the remote js script to the background page file, which is named background.html here
    
    
    
      
    
    
    
    Empty content
    1. add https://example.com to the content_security_policy of the manifest.json:
    "background": {
      "page": "background.html"
    },
    "content_security_policy": "script-src 'self' https://example.com ; object-src 'self'",
    

    Requirements:

    1. The remote js script must be served via https instead of http.
    2. You should specify a page rather than a scripts array in the background section

提交回复
热议问题