@@ -129,6 +129,14 @@ export function getTabUrl() {
129
129
framingURLString = globalThis . document . referrer ;
130
130
}
131
131
132
+ if ( ! framingURLString ) {
133
+ // This is suboptimal, but we need to get the top level origin in an about:blank frame
134
+ const topLevelOriginFromFrameAncestors = getTopLevelOriginFromFrameAncestors ( ) ;
135
+ if ( topLevelOriginFromFrameAncestors ) {
136
+ framingURLString = topLevelOriginFromFrameAncestors ;
137
+ }
138
+ }
139
+
132
140
let framingURL ;
133
141
try {
134
142
framingURL = new URL ( framingURLString ) ;
@@ -139,18 +147,24 @@ export function getTabUrl() {
139
147
}
140
148
141
149
/**
142
- * Best guess effort of the tabs hostname; where possible always prefer the args.site.domain
143
- * @returns {string|null } inferred tab hostname
150
+ * @returns {string | null }
144
151
*/
145
- export function getTabHostname ( ) {
146
- let topURLString = getTabUrl ( ) ?. hostname ;
152
+ function getTopLevelOriginFromFrameAncestors ( ) {
147
153
// For about:blank, we can't get the top location
148
154
// Not supported in Firefox
149
155
if ( 'ancestorOrigins' in globalThis . location && globalThis . location . ancestorOrigins . length ) {
150
156
// ancestorOrigins is reverse order, with the last item being the top frame
151
- // @ts -expect-error - globalThis.top is possibly 'null' here
152
- topURLString = globalThis . location . ancestorOrigins . item ( globalThis . location . ancestorOrigins . length - 1 ) ;
157
+ return globalThis . location . ancestorOrigins . item ( globalThis . location . ancestorOrigins . length - 1 ) ;
153
158
}
159
+ return null ;
160
+ }
161
+
162
+ /**
163
+ * Best guess effort of the tabs hostname; where possible always prefer the args.site.domain
164
+ * @returns {string|null } inferred tab hostname
165
+ */
166
+ export function getTabHostname ( ) {
167
+ const topURLString = getTabUrl ( ) ?. hostname ;
154
168
return topURLString || null ;
155
169
}
156
170
0 commit comments