0

I have the following Javascript file, one.js:

function doSomething() {
    console.log("doing stuff");
}

I have another Javascript file, two.js, in which I want to call doSomething():

doSomething();

How can I achieve this? Note this code is for a browser extension so I cannot use html.

ElsaInSpirit
  • 341
  • 6
  • 16
  • 4
    Possible duplicate of [Javascript import function syntax](https://stackoverflow.com/questions/43157936/javascript-import-function-syntax) – J'e Jun 21 '18 at 12:30

2 Answers2

2

You have to import the function from one.js as below :
import doSomething from "one.js";

Then assign it to some variable of current object and use it.
this.doSomething = doSomething();

Harun Or Rashid
  • 5,589
  • 1
  • 19
  • 21
0

you can use scripts as background and content script in extensions code, you can do something like that,

 "background": {
 "scripts": [ "js/sharedFunctions.js", "js/mainBackground.js" ]
 },
"content_scripts": [
  {
    "matches": ["*://*/*"],
    "js": ["js/sharedFunctions.js", "js/mainContentScript.js"]
  }
]

then functions of first file can be used in second as per order

Muhammad Aadil Banaras
  • 1,134
  • 1
  • 11
  • 21