Skip to content

Commit

Permalink
When sites use the origin trial to implement window.ai, the override …
Browse files Browse the repository at this point in the history
…setting didn't work. Check for the origin trial and ensure an override #4
  • Loading branch information
Thomas101 committed Dec 10, 2024
1 parent 05bbc8d commit c84880b
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/extension/contentscript-main-override/index.ts
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 })
}

0 comments on commit c84880b

Please sign in to comment.