Closed Bug 1574713 Opened 6 years ago Closed 6 years ago

Feature request: let content script know its tabId

Categories

(WebExtensions :: General, enhancement)

enhancement
Not set
normal

Tracking

(Not tracked)

RESOLVED WONTFIX

People

(Reporter: m_khvoinitsky, Unassigned)

Details

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0

Steps to reproduce:

I'd like to be able to get current tab ID synchronously from content script at the very beginning. Of course, I can ask background script via message but this will add extra delay which is not desired when you need to process page as soon as possible (to avoid user noticeable glitches during page load).

Can you explain why you need the tabId from inside the content script, synchronously. I don't think there are any APIs that make use of it, which are available to content scripts.

Flags: needinfo?(mikhail-bmo)

Yes, that's right. I have some per-tab preferences which I pass to new content scripts scope roughly this way (unimportant details has been removed):

browser.contentScripts.register({
js: [
{
code: tab_configuration = ${ JSON.stringify(tab_configuration) }; apply_configuration(),
}
]
})

(of course, previously registered this way contentScripts are removed before)
This is the best way I found to make sure that content script have all required data ASAP.

Flags: needinfo?(mikhail-bmo)

Fixed formatting:

browser.contentScripts.register({
    js: [
        {
            code: `tab_configuration = ${ JSON.stringify(tab_configuration) }; apply_configuration();`,
        }
    ]
})

Extension APIs in content scripts themselves have no need for the tabId. Any API for which tabIds may be useful are in the background page, and the tabId (and frameId) is available to those when the runtime.onMessage/.onConnect event is fired.

The example from comment 3 shows how a tabId can be potentially useful to extensions. However, it is bad API design, because offering the feature in this way forces extension authors to serialize information that is specific to other tabs, and include it in one script.

If what you're actually interested in is the ability to scope content scripts to specific tab(s), then that can be considered in a separate feature request.

Status: UNCONFIRMED → RESOLVED
Closed: 6 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.