3

Working on a project where an app needs to connect with a chrome extension that needs to migrate from manifest v2 to v3.

Detail: In manifest v2 inside the background.js we inject the script (which contains some dataset) that gets the file from another server which contains logic and some UI components (Vue JS).

We don't inject the script directly into the service worker in v3 because there is no access to DOM. So we move all of the code from another server to our project to run directly. But we need a way to pass a dataset that was previously passed from the script in v2

This is the code snippet that I need to be changed from v2 to v3. Kindly have a look into it.

function injectScript(name, { cachebuster, tabId } = {}) {
  const scriptSrc = new URL(process.env.ASSETS_URL)

  scriptSrc.pathname = `${scriptSrc.pathname}${name}.js`
  scriptSrc.search = `cachebuster=${cachebuster}`

  script = document.createElement("script")

  script.type = "text/javascript"
  script.id = `app_io_robot_${name}`
  script.setAttribute("defer", "")

  script.src = scriptSrc.toString()
  script.dataset.extension_id = chrome.runtime.id
  script.dataset.extension_name = process.env.EXTENSION_NAME
  script.dataset.extension_version = runtimeManifest.version
  script.dataset.app_url = process.env.APP_URL
  script.dataset.api_url = process.env.API_URL
  script.dataset.installed_manually = !runtimeManifest.update_url

  if (tabId) {
    script.dataset.tab_id = tabId
  }

  const ONE_MINUTE = 60

  script.onerror = function () {
    console.error(`Unable to load extension, retry in ${ONE_MINUTE} seconds`)

    script.remove()
    setTimeout(() => {
      console.info("Trying to reload extension")

      injectScript(name, { cachebuster, tabId })
    }, ONE_MINUTE * 1000)
  }

  document.getElementsByTagName("head")[0].appendChild(script)
}
Hassam Saeed
  • 385
  • 5
  • 24
  • ManifestV3 doesn't allow external scripts. – wOxxOm Aug 04 '22 at 15:01
  • @wOxxOm I know. That's why i am asking for a solution – Hassam Saeed Aug 05 '22 at 06:27
  • I've reread the description and I'm not sure I see what the problem is. You control the server so you can change the format of data any way you like, so use a json and then `fetch` it in the service worker. – wOxxOm Aug 05 '22 at 06:37

0 Answers0