Closed Bug 1901124 Opened 3 months ago Closed 3 months ago

chrome.scripting.getRegisteredContentScripts returns undefined instead of an empty array when no results exist

Categories

(WebExtensions :: General, defect)

Firefox 128
defect

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 1848346

People

(Reporter: danielhunterjacobs, Unassigned)

Details

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

Steps to reproduce:

I have a function in a background script defined as follows:

async function contentScriptRegistered() {
const matchingScripts = await chrome.scripting.getRegisteredContentScripts({
ids: ["plugin-polyfill"],
});
return matchingScripts.length > 0;
}

It is used to make sure to register a new content script dynamically only if an existing content script with the same id does not already exist.

Actual results:

On Firefox, it said TypeError: can't access property "length", matchingScripts is undefined.

Expected results:

It should work to check if there is an existing content script with the same id. On Chrome, await chrome.scripting.getRegisteredContentScripts returns an empty Array when there are no matching registered content scripts, while Firefox returns undefined. Therefore, on Firefox you need an undefined check that you don't need on Chrome.

The Bugbug bot thinks this bug should belong to the 'Core::Widget: Gtk' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → Widget: Gtk
Product: Firefox → Core
Component: Widget: Gtk → General
Product: Core → WebExtensions

This is not an issue with the getRegisteredContentScripts API.

If you have a MV3 extension in Chrome and use the same code in Firefox in a MV3 extension, it will work as expected, because across all browsers, the chrome namespace in MV3 returns a Promise.

Chrome does not support the scripting API in MV2. Firefox does. One special thing about MV2 across all browsers is that it does not return a Promise, but uses a callback-based API signature.

For a cross-browser Promise API, we recommend the use of the browser namespace. The browser namespace is currently not supported in Chrome, but we're collaborating on standardizing it in the WECG and I expect Chrome to support the browser namespace at some point. As a polyfill, you can use the following at the top of your scripts:

globalThis.browser ??= chrome;
Status: UNCONFIRMED → RESOLVED
Closed: 3 months ago
Duplicate of bug: 1848346
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.