-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tabs.getCurrent() returns undefined when being called from the popup #316
Comments
It's the name which is misleading, a better name might be As for a new method, it would be essentially the same as
|
Safari currently returns the active tab in the active window when this is called from the background page. And it returns the active tab in the window showing the popup when called from the popup page. |
Use case: Proposal: Since Safari has weird behaviour currently with |
I mentioned in #431, I mentioned I'd be supportive of changing this behavior to support returning the associated tab in an extension popup. In Chrome, extension popups are strictly associated with a single tab (switching tabs in the window closes the popup), so there is no ambiguity about which tab is associated. It's also inline (ish) with the motivation of the API, which is "get the tab this thing is running on". While it's not returning the document the script is running in, I think the intention of the developer is clear from this context. As @carlosjeurissen mentioned, it may make sense to explore using a different name, not least because "tabs.getCurrent()" is endlessly confused with getting the currently-active tab; we'll discuss that more. |
I am in favor of returning the tab that was clicked when the popup was opened. If extensions are interested in getting the currently active tab, they can use |
Tested behaviour in Safari Version 16.6 (18615.3.12.11.2):
In the scenario there are two tabs:
When calling |
You will be introducing an inconsistency via this change, which deteriorates the platform, so it's best to add a new method like chrome.tabs.getActiveTab that will also benefit everyone in every extension context by removing the need to copypaste the same unnecessarily verbose boilerplate code browser.tabs.query({ currentWindow: true, active: true }). |
|
@carlosjeurissen I'm not sure I follow how this relates to this issue. |
@xeenon it was relevant in the general discussion on what naming should be used and I was asked to add it to the issue. @tophf What is the inconsistency introduced here? |
By definition getCurrent returns the tab that owns or contains this context, but the popup is not really owned by or contained inside the tab, it's just loosely associtated with a tab due to a design decision and it may even change some day as there is no physical barrier to prevent that. The original name is unfortunately based on a very misleading concept of "current", it was already vague, and now if you blur it even more it'll cease to be a meaningful concept altogether and people will start to wonder why calling getCurrent in an inactive tab returns that very tab and not the "current" one. See? It was always a bad name. P.S. Looking at the amount of questions and misunderstanding around this method on stackoverflow over the past decade, I suggest we just deprecate it and introduce new methods like getOwnTab, getFocusedTab (names TBD) that will be self-explanatory or at least not as inherently deceptive as getCurrent. |
@tophf That's a very good point. The reasoning behind using Taking that in account. something like |
Yeah, although I'd prefer |
@tophf In that case something like Thoughts from the browser vendors? |
I agree with the following:
For running |
@hanguokai Because of the issues described in previous answers |
@carlosjeurissen I'm not sure what use case you're talking about. |
@hanguokai in this context, To clarify what we are actually talking about, something like |
@xeenon Do you know of real-world extension cases in which runtime.getCurrent is used for this purpose? In general I still consider it a bug and it prevents extension developers from being able to easily detect if they are running in a tab-like environment or in an extension popup. To overcome this one can attempt to exclude the current URL to make getCurrent more reliable as such: getCurrentTabWithTabsApi().then((tabInfo) => {
if (!isSafari || !tabInfo) return tabInfo;
// attempt to exclude associated tab
if (tabInfo.url === window.location.href) return tabInfo;
}); @oliverdunk seems |
Probably caused by a security check that disallows any processing of a top chrome:// URL, but I'd argue this is a bug in this case because the returned tab object doesn't expose anything that can't be achieved via chrome.tabs.query. |
@carlosjeurissen Yes, the Userscripts extension uses |
@xeenon Thanks for clarifying! That makes a lot of sense! That ship has sailed then. As for finding out the associated tab id. What are your thoughts? Something like |
I see that the MDN and Chrome documentation states that tabs.getCurrent() should return undefined when being called from an extension's popup. However, I think it's reasonable for a developer to want to know which tab the popup is currently open on. I'm curious to know what other developers think of this as I've seen some confusion about this online.
The text was updated successfully, but these errors were encountered: