Skip to content

Commit f8be42d

Browse files
Use frame ancestors for tab url too
1 parent ee0ba65 commit f8be42d

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

injected/src/utils.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ export function getTabUrl() {
129129
framingURLString = globalThis.document.referrer;
130130
}
131131

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+
132140
let framingURL;
133141
try {
134142
framingURL = new URL(framingURLString);
@@ -139,18 +147,24 @@ export function getTabUrl() {
139147
}
140148

141149
/**
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}
144151
*/
145-
export function getTabHostname() {
146-
let topURLString = getTabUrl()?.hostname;
152+
function getTopLevelOriginFromFrameAncestors() {
147153
// For about:blank, we can't get the top location
148154
// Not supported in Firefox
149155
if ('ancestorOrigins' in globalThis.location && globalThis.location.ancestorOrigins.length) {
150156
// 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);
153158
}
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;
154168
return topURLString || null;
155169
}
156170

0 commit comments

Comments
 (0)