In order to use the 1 API, you need to specify a 4 of 5 or higher and include the 6 permission in your manifest file. Show
UsageYou can use the 1 API to inject JavaScript and CSS into websites. This is similar to what you can do with content scripts, but by using the 1 API, extensions can make decisions at runtime.Injection targetsYou can use the 9 parameter to specify a target to inject JavaScript or CSS into.The only required field is 0. By default, an injection will run in the main frame of the specified tab.
To run in all frames of the specified tab, you can set the 1 boolean to 2.
You can also inject into specific frames of a tab by specifying individual frame IDs. For more information on frame IDs, see the webNavigation API.
You cannot specify both the 3 and 1 properties.Injected codeExtensions can specify the code to be injected either via an external file or a runtime variable. FilesFiles are specified as strings that are paths relative to the extension's root directory. The following code will inject the file 5 into the main frame of the tab.
Runtime functionsWhen injecting JavaScript with 6, you can specify a function to be executed instead of a file. This function should be a function variable available to the current extension context.
This function will be executed in the context of injection target. However, this will not carry over any of the current execution context of the function. As such, bound parameters (including the 7 object) and externally-referenced variables will result in errors. For instance, the following code will not work, and will throw a ReferenceError because 8 is undefined when the function executes:
You can work around this by using the 9 property:
Runtime stringsIf injecting CSS within a page, you can also specify a string to be used in the 0 property. This option is only available for 1; you can't execute a string using 6.
Handling resultsThe results of executing JavaScript are passed to the extension. A single result is included per-frame. The main frame is guaranteed to be the first index in the resulting array; all other frames are in a non-deterministic order.
1 does not return any results.PromisesIf the resulting value of the script execution is a promise, Chrome will wait for the promise to settle and return the resulting value. 0Summary
TypesContentScriptFilterChrome 96+ Properties
CSSInjectionProperties
ExecutionWorldChrome 95+ The JavaScript world for a script to execute within. Type"ISOLATED" , or"MAIN" InjectionResultProperties
InjectionTargetProperties
RegisteredContentScriptChrome 96+ Properties
ScriptInjectionProperties
StyleOriginThe origin for a style change. See style origins for more info. Type"AUTHOR" , or"USER" MethodsexecuteScript 3Promise Injects a script into a target context. The script will be run at 3. If the script evaluates to a promise, the browser will wait for the promise to settle and return the resulting value.Parameters
Returns
|