Importing JS into Content Scripts

4,944 views
Skip to first unread message

SiRobertson

unread,
Jan 29, 2010, 4:01:55 AM1/29/10
to Chromium-extensions
Hi guys,

I've just started looking into the world of Chrome extensions so
forgive me if this is question is a bit naive. I have a generic
utility JS file in the extension's directory, and I would like to
import that JS file into content scripts (not inject it into the DOM).
The only way I have been able to import that JS so far is via the
background page, I send a request to the background which in turn
loads the JS file using XMLHttpRequest, the response text is then sent
back to the content script which then pushing the text through eval()

Now, this is working fine but I'm concerned about overhead. I can
cache the JS text in the background script so it only needs to be
loaded once, but the eval() may be evil.

Is there any other way I can load the JS file into content scripts?

Arne Roomann-Kurrik

unread,
Jan 29, 2010, 1:45:41 PM1/29/10
to SiRobertson, Chromium-extensions
Hi,

   The chrome.tabs.executeScript function might be what you're looking for: http://code.google.com/chrome/extensions/trunk/tabs.html#method-executeScript

  Otherwise, you can just add the other JS file to your content_scripts section in your manifest - developers typically use that functionality to be able to use jquery (or some other library) in their content scripts.  Check out a sample in the highlighted code here:

Hope this helps,
~Arne


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


PhistucK

unread,
Jan 29, 2010, 2:03:13 PM1/29/10
to Arne Roomann-Kurrik, SiRobertson, Chromium-extensions
Note that you keep linking to the trunk version of the documentation without knowing what version the poster is using...

Re-posting my message from before -
The provided link for extension API is the trunk version of the API. Things might not be the same in Dev\Beta\Stable Chrome (new stuff may exist there that do not yet work on other versions).
For a version correlates with the Dev version - http://code.google.com/chrome/extensions/dev/api_index.html

☆PhistucK

Arne Roomann-Kurrik

unread,
Jan 29, 2010, 2:13:46 PM1/29/10
to PhistucK, SiRobertson, Chromium-extensions
We should probably just include a warning at the top of the documentation.  I'm certainly not going to post three links every time I point someone at a function reference ;)

~Arne

PhistucK

unread,
Jan 29, 2010, 2:37:44 PM1/29/10
to Arne Roomann-Kurrik, SiRobertson, Chromium-extensions
Of course, but the Stable edition is the safest one - not the trunk. :)

☆PhistucK

SiRobertson

unread,
Jan 29, 2010, 4:44:23 PM1/29/10
to Chromium-extensions
I feel like a fool, I forgot content_scripts could be used to import
JS files.

Thanks :-)


On Jan 29, 6:45 pm, Arne Roomann-Kurrik <kur...@chromium.org> wrote:
> Hi,
>

>    The chrome.tabs.executeScript function might be what you're looking for:http://code.google.com/chrome/extensions/trunk/tabs.html#method-execu...


>
>   Otherwise, you can just add the other JS file to your content_scripts
> section in your manifest - developers typically use that functionality to be
> able to use jquery (or some other library) in their content scripts.  Check
> out a sample in the highlighted code here:http://code.google.com/chrome/extensions/trunk/content_scripts.html
>
> Hope this helps,
> ~Arne
>
>
>
> On Fri, Jan 29, 2010 at 1:01 AM, SiRobertson <nut...@gmail.com> wrote:
> > Hi guys,
>
> > I've just started looking into the world of Chrome extensions so
> > forgive me if this is question is a bit naive. I have a generic
> > utility JS file in the extension's directory, and I would like to
> > import that JS file into content scripts (not inject it into the DOM).
> > The only way I have been able to import that JS so far is via the
> > background page, I send a request to the background which in turn
> > loads the JS file using XMLHttpRequest, the response text is then sent
> > back to the content script which then pushing the text through eval()
>
> > Now, this is working fine but I'm concerned about overhead. I can
> > cache the JS text in the background script so it only needs to be
> > loaded once, but the eval() may be evil.
>
> > Is there any other way I can load the JS file into content scripts?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.

> > To post to this group, send email to chromium-extensi...@chromium.org.


> > To unsubscribe from this group, send email to

> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>

Tahtu

unread,
Aug 9, 2022, 11:07:40 PM8/9/22
to Chromium Extensions, Chromium-extensions
Arne Roomann-Kurrik schrieb am Freitag, 29. Januar 2010 um 19:45:41 UTC+1:
  Otherwise, you can just add the other JS file to your content_scripts section in your manifest - developers typically use that functionality to be able to use jquery (or some other library) in their content scripts.  Check out a sample in the highlighted code here:

The mentioned links don't work furthermore. So here, a short example of how to use multiple files for the content script for the manifest file:

  "content_scripts": [{
    "matches": ["*://*/*"],
    "run_at": "document_start",
    "js": ["global.js", "content_script.js"]
  }]
 

wOxxOm

unread,
Aug 10, 2022, 4:18:37 AM8/10/22
to Chromium Extensions, Tahtu, Chromium-extensions
If you need to import it as a ES Module into a content script, you can also use dynamic import + web_accessible_resources for the imported path e.g. "foo/bar.js"

import(chrome.runtime.getURL("foo/bar.js").then(m => m.someMethod())

Reply all
Reply to author
Forward
0 new messages