Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Extensions] Add main world injections for dynamic content scripts
This CL adds a new field "world" for dynamic content scripts which allows the extension to specify if the script will run in the isolated or main world. By default, scripts which do not specify this field will run in the isolated world. Bug: 1207006 Change-Id: Ie28bf1cb7c6d7c90cf74d7ead4554ee04810c652 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3573464 Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org> Commit-Queue: Kelvin Jiang <kelvinjiang@chromium.org> Cr-Commit-Position: refs/heads/main@{#992318}
- Loading branch information
1 parent
3a08137
commit e5ad345
Showing
17 changed files
with
239 additions
and
21 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
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
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
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
8 changes: 8 additions & 0 deletions
8
chrome/test/data/extensions/api_test/scripting/dynamic_scripts_main_world/change_title.js
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Changes the document's title based on the existence/value of | ||
// window.mainWorldFlag, which is set by a script that's part of a web page. | ||
document.title = window.mainWorldFlag === 'from main world' ? 'MAIN_WORLD' : | ||
'ISOLATED_WORLD'; |
12 changes: 12 additions & 0 deletions
12
chrome/test/data/extensions/api_test/scripting/dynamic_scripts_main_world/manifest.json
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"manifest_version": 3, | ||
"name": "Main world dynamic content script test extension", | ||
"version": "0.1", | ||
"description": "Tests that dynamic scripts can be injected into the main world", | ||
"background": { | ||
"service_worker": "worker.js", | ||
"type": "module" | ||
}, | ||
"permissions": ["scripting", "tabs"], | ||
"host_permissions": ["*://hostperms.com/*"] | ||
} |
45 changes: 45 additions & 0 deletions
45
chrome/test/data/extensions/api_test/scripting/dynamic_scripts_main_world/worker.js
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import {openTab} from '/_test_resources/test_util/tabs_util.js'; | ||
|
||
// Inject a script which changes the page's title based on the execution world | ||
// it's running on, then call executeScript which checks the title. | ||
async function runTest(world, expectedTitle) { | ||
await chrome.scripting.unregisterContentScripts(); | ||
var scripts = [{ | ||
id: 'script1', | ||
matches: ['*://hostperms.com/*'], | ||
js: ['change_title.js'], | ||
world, | ||
runAt: 'document_end', | ||
}]; | ||
|
||
await chrome.scripting.registerContentScripts(scripts); | ||
const config = await chrome.test.getConfig(); | ||
|
||
// After the scripts has been registered, navigate to a url where they will be | ||
// injected. | ||
const url = `http://hostperms.com:${ | ||
config.testServer.port}/extensions/main_world_script_flag.html`; | ||
let tab = await openTab(url); | ||
let results = await chrome.scripting.executeScript({ | ||
target: {tabId: tab.id}, | ||
func: () => document.title, | ||
}); | ||
|
||
chrome.test.assertEq(1, results.length); | ||
chrome.test.assertEq(expectedTitle, results[0].result); | ||
chrome.test.succeed(); | ||
} | ||
|
||
chrome.test.runTests([ | ||
async function mainWorld() { | ||
runTest(chrome.scripting.ExecutionWorld.MAIN, 'MAIN_WORLD'); | ||
}, | ||
|
||
async function isolatedWorld() { | ||
runTest(chrome.scripting.ExecutionWorld.ISOLATED, 'ISOLATED_WORLD'); | ||
}, | ||
]); |
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
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
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
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
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
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
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
Oops, something went wrong.