1

This question might be redundant or similar to ones already posted but is there a way for me to import a js file from another js file while coding for a google extension?

I did some research and found answers that work only if you are importing js into html using the "" tag. I know the require function from node is one method but I'm not sure if the extension will still work for other people when they download my extension for their own usage. (As they won't have Node installed)

Additionally, when I hard code my array elements without importing and do

document.getElementById('Some random id').textContent = "This works!";

The textContent only changes for the first iteration of that id present in the website html. I understand the logic behind this but want it to change the textContent for every single iteration of that specific id.

The manifest.json has 2 content scripts loaded, main.js, and catalog.js. Catalog.js contains an array while main.js does the heavy lifting.

    "name": "Test Extension", 
    "description": "Learning how to make google extensions",
    "manifest_version": 2,
    "version": "1.0", 
    
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["main.js", "catalog.js"]
        }

    ]
}

main.js:

var ids = require('./catalog.js');
console.log("Testing...")
for (i = 0; i < ids.length; i++)
{
    document.getElementById(ids[i]).textContent = "This works!";

}

catalog.js:

var ids = [
"video-title"
]

module.exports = {ids};
SubZero
  • 35
  • 6
  • 1
    You can remove `require('./catalog.js')` because both main.js and catalog.js share the same execution environment so they can see each other's global variables directly. Just change the order of the scripts in manifest.json so that catalog.js loads first. – wOxxOm Jul 01 '21 at 19:41
  • Thanks. My question has been associated with a different post. Your answer has also helped. I think I have found good answers. – SubZero Jul 02 '21 at 00:07

0 Answers0