0

In my chrome extension, I had a similar function in both "popup.js" and "contentScript.js", so I decided to have it in a single file instead, in "helper.js". I need to call/import that file from both popup and contentScript.

From this SO question: Import in Content Script i got the solution (not the first one listed), to simply add the file in the manifest.json, so I put this:

"content_scripts": [
    {
      "js": ["helper.js",
      "contentScript.js"],
      "matches": [
        "<all_urls>"
      ]
    }
],
"web_accessible_resources": [
    {
        "resources": [ "helper.js" ],
        "matches": [ "<all_urls>" ]
    }
],

Now, I just need to access the same file in "popup.js". Initially I tried following this: Import from popup by adding a second tag in my popup.html file, but it doesn't work, because I need to access the function name from popup.js (and not the popup window).

Specifically, I have this section of code in "popup.js" that is supposed to call a function from "helper.js" after a click event:

async function handleChat(e) {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
    target: { tabId: tab.id },
    function: removeTags,
});  
window.close();

}

So the question is, how do I access or reference the "removeTags" function (that is in helper.js) ?

Barraka
  • 52
  • 1
  • 8
  • Assuming removeTags is a global function you simply add a second tag like ``. It is the correct solution, so if it doesn't work for you there may be a typo. Use devtools debugger to see what happens. Note that the popup is a separate window so it has its own separate devtools: right-click inside the popup and select "inspect" in the menu. – wOxxOm May 04 '23 at 09:56
  • Note that the problem may be caused by window.close running too soon, so try adding `await ` before chrome.scripting call. – wOxxOm May 04 '23 at 09:58

0 Answers0