Not able to get the active browser tab in chrome extension browser action popup

3,373 views
Skip to first unread message

Gopi Mullapudi

unread,
May 9, 2018, 8:13:35 AM5/9/18
to Chromium-Extensions-Announce
Hi,


I am developing a chrome extension and below is my manifest.json:


{ "background": { "scripts": [ "background.js" ] }, 
"browser_action": 
{
 "default_icon": "img/icon.png", 
"default_title": "TITLE"
 }, 
"icons": { "128": "img/icon_128.png", }, "manifest_version": 2, "name": "external popup", "description": "test messaging between popup ,bg and cs", "permissions": [ "activeTab" ,"tabs"], "version": "2.0" }


I load my popup at runtime using below code in backgroundscript.js and it works fine


chrome.tabs.create({
url: chrome.extension.getURL('dialog.html'), focused: true }, function(tab) { console.log("popup launched"); });


my popup.html has below code:

<!DOCTYPE html>
<html>
<body style="width: 300px">
<button id="fetchevent">start reading events</button>
<button id="stopevent">stop reading events</button>

<button id="clickme">click me</button>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>



My popup.js fetchevent button on click event then sends a message to background script requesting to send a message to active browser tab to start sending it the information of user events  in the page.so that it can relay it back to popup html to populate. My popup.js was able to send the request to background script.

So the communication is popup request  <-> backgroundscript request <-> content script -> sends messages continuosly to background script until stop request is received.  

So when my background script receives the request from popup.html it tries to send a message to content script the following way. But it always gets popup.html when it queries chrome.tabs . And does because popup.html page is also open and thats the last active tab ?  

Two questions here . How do i get current active chrome browser tab where this extension is loaded not the external popup html ? And is this right of way communicating between background script, content script and external popup ?

chrome.tabs.query({"active": true, "currentWindow": true}, function (tabs) {
    tabURL = tabs[0].url;
    console.log("URL from get-url.js", tabURL); // this always gets the popup.html not the browser tab url where extension is loaded
});





PhistucK

unread,
May 9, 2018, 1:24:13 PM5/9/18
to gkmul...@gmail.com, Chromium-extensions
After reading your description, I am not sure why you need the background script and content script at all. You can chrome.tabs.executeScript to the current tab (just do not specify a tab ID) from the browser action popup.
You subscribe to the connect event (and the message event of the port) in the popup and get the URL -
let tabURL, events = [];
chrome.runtime.onConnect.addListener(port =>
{
 tabURL = port.sender.url;
 port.onMessage.addListener(message => events.push(message))
});
The executed script will chrome.runtime.connect to the open browser action popup and send it messages until the popup sends it back a stop message (or disconnects).

Anything I have missed? Do you need to create that dialog.html tab or was that just a workaround for the popup not working as your expect?


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/3986e8f4-df08-4acf-b285-13a6a83ee1e9%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Gopi Mullapudi

unread,
May 9, 2018, 1:46:49 PM5/9/18
to Chromium-Extensions-Announce, gkmul...@gmail.com
Hi PhistucK,

Thank you so much for your reply.
It was a  BIG TYPO in my code in my background.js from my OP. Please note that its not chrome.tabs.create instead chrome.windows.create

chrome.browserAction.onClicked.addListener(function() {
   chrome.windows.create({'url': 'popup.html', 'type': 'popup'}, function(window) {
   });
});

My popup.html has lot of ui and at one place it renders all the events into a table. I use my background script to define context menus as well and their listeners along with other chrome.runtime.* events. I also use my background script to change the icon after it was clicked etc..

May be i will take your suggestion to load contentscript at runtime using executescript and unload when the job is done. But my main issue is getting the correct chrome browser tab.

As you seen in the above code my popup.html is created as window and when it sends message to background script to request for DOM events from content scripts, background script tries to read the chrome browser tab that user has launched the extension from . But it always gets the popup.html as the active window not the chrome browser tab user clicked the extension icon from.




To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

PhistucK

unread,
May 9, 2018, 1:53:29 PM5/9/18
to Gopi Mullapudi, Chromium-extensions
I see. So the browser action is not supposed to show its popup, but a new popup window.
It makes sense that you would get the popup, because it is indeed the active, focused window and the active, focused tab (a single tab, but still).
chrome.browserAction.onClicked.addListener(callback) provides the Tab object in its callback which should have a url field (and an id field) as well as other details. Is that sufficient for your use case?

PhistucK


To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

Gopi Mullapudi

unread,
May 9, 2018, 2:09:42 PM5/9/18
to Chromium-Extensions-Announce, gkmul...@gmail.com
Yes, that makes sense. I will store that tab id on browserAction.onClicked.addListener event to a browserTabId variable and use it to communicate with content script to read the events .

I hope if i load content script at runtime using executeScript on all frames in background script, i can still use the browserTabId to communicate with all of them.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
Reply all
Reply to author
Forward
0 new messages