Skip to content

Commit

Permalink
MV3: Request content-script config on first tick (#2325)
Browse files Browse the repository at this point in the history
* MV3 tests: Wait for content-script registration to complete in navigator interface test

* Test if test is always fixed by making test-pages privileged

* MV3 content-script: move messaging to first tick.

* Revert privacy-test-pages.site as privileged domain
  • Loading branch information
sammacbeth authored Nov 21, 2023
1 parent 22b8f55 commit 5090c68
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,14 @@
"navigatorInterface": {
"exceptions": [],
"state": "enabled",
"hash": "e25bc15aae118274e8d4481baf2033dd"
"hash": "e25bc15aae118274e8d4481baf2033dd",
"settings": {
"privilegedDomains": [
{
"domain": "duckduckgo.com"
}
]
}
},
"nonTracking3pCookies": {
"settings": {
Expand Down
8 changes: 7 additions & 1 deletion integration-test/navigator-interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import backgroundWait from './helpers/backgroundWait'
import { TEST_SERVER_ORIGIN } from './helpers/testPages'

test.describe('navigatorInterface', () => {
test('injects navigator.duckduckgo interface into pages', async ({ backgroundPage, page, context }) => {
test('injects navigator.duckduckgo interface into pages', async ({ backgroundPage, page, context, manifestVersion }) => {
await backgroundWait.forExtensionLoaded(context)
await backgroundWait.forAllConfiguration(backgroundPage)
if (manifestVersion === 3) {
// wait for content-script registration to complete
await backgroundPage.evaluate(() => {
return globalThis.components.scriptInjection.ready
})
}
await page.goto('https://privacy-test-pages.site/features/navigator-interface.html')
expect(await page.locator('#interface').innerText()).toBe('interface: true')
expect(await page.locator('#isDuckDuckGo').innerText()).toBe('isDuckDuckGo: true')
Expand Down
61 changes: 33 additions & 28 deletions shared/js/content-scripts/content-scope-messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,39 @@ function getSecret () {
}

async function init () {
const secret = await getSecret()
const secretPromise = getSecret()

// send off a message to the background to get config for this frame
chrome.runtime.sendMessage({
messageType: 'registeredContentScript',
options: {
documentUrl: window.location.href
}
}, async (argumentsObject) => {
// Setup debugging messages if necessary.
if (argumentsObject.debug) {
window.addEventListener('message', message => {
if (message.data.action && message.data.message) {
chrome.runtime.sendMessage({
messageType: 'debuggerMessage',
options: message.data
})
}
})
}

// if we didn't get the secret yet, wait for it
const secret = await secretPromise
// Init the content-scope-scripts with the argumentsObject.
window.dispatchEvent(new CustomEvent(secret, {
detail: {
type: 'register',
argumentsObject
}
}))
})

const secret = await secretPromise

// Content-scope-script messaging proxy, to allow the Click to Load content
// script to send messages to the extension's background and receive a
Expand Down Expand Up @@ -58,33 +90,6 @@ async function init () {
detail: message
}))
})

chrome.runtime.sendMessage({
messageType: 'registeredContentScript',
options: {
documentUrl: window.location.href
}
}, argumentsObject => {
// Setup debugging messages if necessary.
if (argumentsObject.debug) {
window.addEventListener('message', message => {
if (message.data.action && message.data.message) {
chrome.runtime.sendMessage({
messageType: 'debuggerMessage',
options: message.data
})
}
})
}

// Init the content-scope-scripts with the argumentsObject.
window.dispatchEvent(new CustomEvent(secret, {
detail: {
type: 'register',
argumentsObject
}
}))
})
}

init()

0 comments on commit 5090c68

Please sign in to comment.