Including Javascript files in a Chrome extension content script

Matt Joiner picture Matt Joiner · Sep 28, 2010 · Viewed 13.7k times · Source

I'm writing a Chrome extension, and want to write one JS file, that provides several expected functions that aren't present in another, then load that other file. I'm after behavior similar to require in Perl, #include in C, or execfile in Python when passing the current modules locals and globals, as though the referenced file was inserted directly into the current script.

Most existing solutions I can find out there refer to embeddeding these "includes" inside script tags, but I'm not sure this applies (and if it does, an explanation of where exactly my extension is injecting all these script tags in the current page).

Update0

Note that I'm writing a content script. At least on the "user" side, I don't provide the original HTML document.

Answer

Matt Joiner picture Matt Joiner · Oct 5, 2010

Well the best solution was Chrome-specific. The javascript files are listed in the order they are loaded in the extension's manifest.json, here's an extract of the relevant field:

{
  "content_scripts": [
    {
      "js" : ["contentscript.js", "254195.user.js"]
    }
  ]
}

The javascript files are effectively concatenated in the order given and then executed.