Are WebExtension content scripts supported in iframes ?

An extension I maintain, which works on Firefox/Chromium Edge/Chrome etc declares a content script to be run in all iframes.

Does safari support content scripts in iframes?
It seems the MessageSender object is missing the frameId member. It is thus impossible to tell what frame a content script is running in.

Manifest Excerpt

Code Block json
"content_scripts": [
{
"matches": [
"https://*/*",
"http://*/*"
],
"js": [
"content.js"
],
"match_about_blank": false,
"all_frames": true,
"run_at": "document_start"
}
],


MessageSender interface definition


Code Block typescript
/**
* An object containing information about the script context that sent a message or request.
* @since Chrome 26.
*/
export interface MessageSender {
/** The ID of the extension or app that opened the connection, if any. */
id?: string;
/** The tabs.Tab which opened the connection, if any. This property will only be present when the connection was opened from a tab (including content scripts), and only if the receiver is an extension, not an app. */
tab?: chrome.tabs.Tab;
/**
* The frame that opened the connection. 0 for top-level frames, positive for child frames. This will only be set when tab is set.
* @since Chrome 41.
*/
frameId?: number;

Yes, frames for content scripts and sending messages are supported. I'm not sure what the MessageSender interface is that you are referencing. Can you give some more details?
Timothy,

Hi, thanks for your response!

MessageSender is the type of object passes as one of the arguments to the chrome.runtime.onMessage handler.

See:
developer.chrome.com/extensions/runtime#event-onMessage
developer.chrome.com/extensions/runtime#type-MessageSender

Add https:// for the above links. Unfortunately the editor won't let me create the links.
And to be clear, at least with the safari preview of a week ago, the MessageSender object was missing the frameId argument
I see. The frameId missing issue has been fixed. It should be in the next seed, and also Safari Technology Preview.
Timothy,

Great, thanks!
I tried the latest safari 14 for catalina beta 1 and it's still missing frameId ?

Safari: Version 14.0 (15610.1.20.4, 15610)

> Unhandled Promise Rejection: Error: Expecting not null for sender.frameId
The fix is not in a beta seed yet. Try Safari Technology Preview 110, it should have the fix.

The fix is not in a beta seed yet. Try Safari Technology Preview 110, it should have the fix.

Timothy, thanks but I tried that build, and it didn't seem to work either:

Still with:
Unhandled Promise Rejection: Error: Expecting not null for sender.frameId

Thanks!
Please post a code snippet of how you are using frameId. Thanks!
I'm using this helper:

Code Block
export function getFrameSpec(sender: MessageSender) {
const tabId = getTab(sender)
const frameId = notNullOrUndef(sender.frameId, 'sender.frameId')
const spec = { tabId, frameId }
return { ...spec, spec }
}


Which calls this TypeScript not null assertion routine:

Code Block
export function notNullOrUndef<T>(
t: T | null | undefined,
name = '<unknown>'
): T | never {
if (t == null) {
throw new Error(`Expecting not null for ${name}`)
} else {
return t
}
}


It's comparing against null using == double equals operator, which in JavaScript would mean the value is either undefined or null.

Normally, top level frames should have frameId set to 0


Thanks, we have found the issue and are investigating.
Any update on this ?
Cheers
A fix has been found, it has not shipped in a beta or Safari Technology Preview yet.
Thanks!
Looks like this is fixed in beta 3
Are WebExtension content scripts supported in iframes ?
 
 
Q