0

I'm developing an extension for Chrome. Main idea is overriding navigator.credentials.create and navigator.credentials.get, so I added a content script to inject a script named injected.js . It works as expected but when I want to call a function from another file in injected.js I get this error :

Uncaught SyntaxError: Cannot use import statement outside a module (at injected.js:1:1)

Does anybody have idea how it can be fixed?

here is manifest.json :

{
  "manifest_version": 3,
  ...
  "content_scripts": [
    {
      "js": [
        "content.js"
      ],
      "all_frames": true,
      "run_at": "document_start",
      "matches": [
        "<all_urls>"
      ]
    }
  ],
  "permissions": [
    "storage"
  ],
  "web_accessible_resources": [
    {
      "resources": [
        "injected.js"
      ],
      "matches": [
        "<all_urls>"
      ]
    }
  ]
}

content script:

var s = document.createElement('script');
s.src = chrome.runtime.getURL('injected.js');
s.onload = function() { this.remove(); };
// see also "Dynamic values in the injected code" section in this answer
(document.head || document.documentElement).appendChild(s);

injected.js:

import {process} from "./some_file.js";

navigator.credentials.create = function (options) {

    process(options)
}
Ali Zarei
  • 3,523
  • 5
  • 33
  • 44
  • 1
    You need to add `s.type = 'module'` however note that your injected code will import from the site's origin and not from the extension. – wOxxOm May 14 '23 at 17:14
  • @wOxxOm Thanks dear, I added type attribute as you said and it worked. By adding js files to web_accessible_resources in manifest file I could import them correctly. – Ali Zarei May 15 '23 at 08:12

0 Answers0