-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When sites use the origin trial to implement window.ai, the override …
…setting didn't work. Check for the origin trial and ensure an override #4
- Loading branch information
Showing
1 changed file
with
34 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,37 @@ | ||
const genericWindow = window as any | ||
if (genericWindow.aibrow) { | ||
genericWindow.ai = genericWindow.aibrow | ||
} else { | ||
if (genericWindow.ai) { | ||
genericWindow.ai.__aibrowOverride = true | ||
/** | ||
* This can be safely called multiple times | ||
*/ | ||
function override () { | ||
const genericWindow = window as any | ||
if (genericWindow.aibrow) { | ||
genericWindow.ai = genericWindow.aibrow | ||
} else { | ||
if (genericWindow.ai) { | ||
genericWindow.ai.__aibrowOverride = true | ||
} | ||
} | ||
} | ||
if (genericWindow.aibrowTranslation) { | ||
genericWindow.translation = genericWindow.aibrowTranslation | ||
} else { | ||
if (genericWindow.translation) { | ||
genericWindow.translation.__aibrowOverride = true | ||
if (genericWindow.aibrowTranslation) { | ||
genericWindow.translation = genericWindow.aibrowTranslation | ||
} else { | ||
if (genericWindow.translation) { | ||
genericWindow.translation.__aibrowOverride = true | ||
} | ||
} | ||
} | ||
|
||
override() | ||
|
||
// <meta http-equiv="origin-trial" headers in the dom may not have been parsed at this point, so we have | ||
// to wait for document.head to be available and then override otherwise the browser implementation | ||
// will be injected back over the top. | ||
// | ||
// This can be removed when the origin trials have finished, around M136 | ||
if (!document.head) { | ||
const observer = new MutationObserver(() => { | ||
if (document.head) { | ||
observer.disconnect() | ||
override() | ||
} | ||
}) | ||
observer.observe(document.documentElement, { childList: true }) | ||
} |