Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MV3: Request content-script config on first tick #2325

Merged
merged 4 commits into from Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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()