Opening popup.html from within content.js

2,442 views
Skip to first unread message

Among Us

unread,
Apr 21, 2023, 12:32:56 PM4/21/23
to Chromium Extensions
1) is there a api chrome extensions can use to trigger a popup?
2) is there a way to open the default pop up from a button injected into webpage from content.js file in a chrome extension (as if user had clicked on the extension in the toolbar).
3) if thats not available we are thinking of opening the extension in its own window once the button injected webpage from content.js is clicked. But when we do that it is not in the same context as content.js so how can we communicate to the new window the data it needs?


My objective: when use clicks a button i have injected onto a page the chrome extension default popup opens.

PhistucK

unread,
Apr 21, 2023, 12:39:00 PM4/21/23
to Among Us, Chromium Extensions
See inline.

PhistucK


On Fri, Apr 21, 2023 at 5:33 PM Among Us <sam...@gmail.com> wrote:
1) is there a api chrome extensions can use to trigger a popup?
Assuming "popup" means the extension popup that usually opens when you click on the browser toolbar button of your extension, no.  

2) is there a way to open the default pop up from a button injected into webpage from content.js file in a chrome extension (as if user had clicked on the extension in the toolbar).
Still a no. 

3) if thats not available we are thinking of opening the extension in its own window once the button injected webpage from content.js is clicked. But when we do that it is not in the same context as content.js so how can we communicate to the new window the data it needs?

Using the message passing APIs (chrome.runtime.connect/sendMessage), like when you communicate with the background page/script/service worker.




My objective: when use clicks a button i have injected onto a page the chrome extension default popup opens.

--
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/aa5d6c55-ec67-4e33-9d66-ab04a78d9d47n%40chromium.org.

Among Us

unread,
Apr 21, 2023, 1:46:23 PM4/21/23
to Chromium Extensions, PhistucK, Chromium Extensions, Among Us
Hi,

Thanks for the response.

Instead im attempting as follows:

a) Content.js tells background.js to open the popup.html in a new window (done)
b) Popup sends message to background.js to ask content.js to send it some data (done)
c) background.js sends message to content.js and retrieves the desired data (ERROR: Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist) 

Issue now is that since the new popup and background.js are running in the new window, i cant send the message to the content.js on the tab that it is in directly. How can i get the find the window that content.js ran in such that it can successfully receive the message from background.js running in the new window? 

Thanks 

PhistucK

unread,
Apr 22, 2023, 8:59:12 AM4/22/23
to Among Us, Chromium Extensions
I guess it would be something like this -
1. content.js tells the background script to create a popup window with an extension URL, using message passing.
2. The background script stores the sender (one of the parameters of the message passing listener callback. I think it should have the tab ID) somewhere/passes it to the created popup.
3. Popup/background script does whatever and needs to send a message to the content script, so it uses the tab ID for that.

Makes sense?

PhistucK

Oliver Dunk

unread,
Apr 24, 2023, 7:32:31 AM4/24/23
to PhistucK, Among Us, Chromium Extensions
Hi again! Thanks for moving over to the mailing list.

To elaborate a little bit more, there are three ways that you should be able to message the popup from the background:
  • When chrome.runtime.onMessage is called in your background, the third argument you're passed should be a sendResponse function. You can call this with a message to respond to the sender.
  • As PhistucK mentioned, you could call chrome.tabs.sendMessage to send a message to that tab. You can use the sender you're given in your background's message listener to get that tab ID.
  • chrome.runtime.sendMessage broadcasts to all open extension pages, which should include your popup.
If you implement one of these and it isn't working, sharing some more code would be helpful!
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


Reply all
Reply to author
Forward
0 new messages