Skip to content
TheChatArchiveThe Chat Archive
ChatGPT (model unspecified)· OpenAIHugging Face ShareLM (browser-extension contributions)

How to load a js function from another file in a chrome plugin (same directory, part of…

  1. User

    How to load a js function from another file in a chrome plugin (same directory, part of the one the plugin includes)

  2. Assistant

    Got it — you’re making a Chrome extension and want to call a JS function that’s defined in another file in the extension’s directory. Here’s how Chrome extensions handle it:

    1️⃣ Manifest-based inclusion In your manifest.json, you explicitly load the scripts you need: jsonCopiar código{ "name": "My Extension", "manifest_version": 3, "background": { "service_worker": "background.js" }, "content_scripts": [ { "matches": ["<all_urls>"], "js": ["utils.js", "content.js"] } ] }

    Here utils.js is loaded first, then content.js. Any functions or variables in utils.js are available to content.js because they’re in the same execution context.

    2️⃣ For popup or options pages (HTML) If you’re adding scripts to your popup or options page, you include them as tags: htmlCopiar código

    Now popup.js can call functions defined in utils.js.

    3️⃣ ES Modules (Recommended) If you’re using Manifest V3, you can also make your scripts ES modules: jsCopiar código// utils.js export function myFunction() { console.log('Hello from utils!'); }

    Then in your other script: jsCopiar código// content.js import { myFunction } from './utils.js';

    myFunction();

    But you must declare the file as a module. In your popup HTML: htmlCopiar código

    For background/service worker scripts: jsonCopiar código"background": { "service_worker": "background.js", "type": "module" }

    Then background.js can import from other files.

    4️⃣ You can’t dynamically inject scripts into a content script from your extension (MV3) In MV3, you can’t just importScripts in a content script like in MV2 background pages. Use the ES module approach or include scripts explicitly in the manifest.

    Quick summary:

    Content scripts: list multiple JS files in the manifest (order matters).

    HTML-based pages: add multiple tags in order.

    Modern MV3: use type: "module" and import statements.

    Do you want me to show a working minimal example of a Chrome extension that uses a function from another file?

  3. User

    Cool. 1 worked

  4. Assistant

    Awesome 🎉 — glad it worked! Just remember a couple of small but important tips when you use the “multiple JS files” approach in your manifest:

    Order matters: Chrome will inject the files in the array in the order you list them. Anything declared in the first file will be available to the next ones.

    Same execution context: All the files listed under the same content_scripts entry share the same context, so variables/functions are directly usable.

    Different contexts: Background/service worker, popup, options, and content scripts are separate worlds — if you want to reuse code across them you need to include the same helper file in each context separately.

    Would you like me to show how to structure it so you can share the same helper file between content scripts and your popup/background easily?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.