Can't let extension (that is, background.js and the Chrome APIs) interact with a pop-up window my extension creates

210 views
Skip to first unread message

Manuel

unread,
Dec 28, 2022, 11:20:31 PM12/28/22
to Chromium Extensions
Under the header #Additional HTML Files (here), the Chrome Extension documentation says that

"An extension can also have other HTML files that are not declared in the manifest. All extension HTML files can access the Chrome APIs and can use script tags including Javascript files, but cannot declare inline JavaScript."

and

"You can open these pages using the web api window.open(), the Chrome APIs windows.create(), or tabs.create()."

In my Extension, I want to use a pop-up window (not the standard Chrome Extension Window that we can create with the action API) to interact with the user, and use the following code in background.js to create this popup window:

chrome.action.onClicked.addListener(async (tab) => {
    windowSettings = {width: 400, height: 600, type: "popup", URL: "file:///D:/DT%20Sourcing%20Tool/scripts/popup.html"};
    chrome.windows.create(windowSettings);
}

My popup launches successfully, and the HTML code and JavaScript code I have included in it (not inline) run as expected. However, I cannot access the Chrome APIs from my JavaScript file. For reference, my popup.html file looks as follows:

<html>
  <head>
    <title>DT Sourcing Tool</title>
    [...]
    <script src="popup.js"></script>
  </head>
</html>

and in popup.js, I am trying to run the following line

const windowSettings = {tabId: "sourcing"};
chrome.windows.create(windowSettings);

This is unsuccessful, because "chrome" is not defined. What causes this issue?

Alexandru Constantin Bosneanu

unread,
Dec 29, 2022, 2:56:21 AM12/29/22
to Chromium Extensions, Manuel
Hi Manuel, have you tried to use  chrome.runtime.getURL('/scripts/popup.html ') for the URL property in windowSettings?

The other solution would be also to mention the file in the manifest.json using "web_accessible_resources": [ "scripts/popup.html"]

BR,
AlexB

PhistucK

unread,
Dec 29, 2022, 8:40:28 AM12/29/22
to Manuel, Chromium Extensions
You cannot use Chrome extension APIs from within file:/// URLs. You need to include those files in your extension and open them there (I think relative URLs might work). Only chrome-extension:// URLs have access to Chrome extension APIs.

PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/2d48588d-7dd5-4941-a17c-f82ac6d4c294n%40chromium.org.
Reply all
Reply to author
Forward
0 new messages