@@ -10,9 +10,9 @@ Automatically detects Chrome Extension Manifest V2 and V3 and delegates to the a
1010
1111- [ Installation] ( #installation )
1212- [ Usage] ( #usage )
13- - [ Executing Functions] ( #executing-functions )
14- - [ Injecting Script Files] ( #injecting-script-files )
15- - [ Updating Options] ( #updating-options )
13+ - [ Executing Functions] ( #executing-functions )
14+ - [ Injecting Script Files] ( #injecting-script-files )
15+ - [ Updating Options] ( #updating-options )
1616- [ API] ( #api )
1717- [ Options] ( #options )
1818- [ Examples] ( #examples )
@@ -41,23 +41,26 @@ import injectScript, {InjectScriptOptions} from "@adnbn/inject-script";
4141
4242// Initialize an injector for a specific tab (Manifest V2 or V3)
4343const injector = injectScript ({
44- tabId: 123 ,
45- frameId: false , // inject into the top frame only
46- matchAboutBlank: true , // include about:blank frames
47- injectImmediately: false , // inject at `document_idle`
48- // V2 only: timeFallback: 5000, // ms before timing out (default: 4000)
49- // V3 only: world: 'ISOLATED', documentId: 'abc123'
44+ tabId: 123 ,
45+ frameId: false , // inject into the top frame only
46+ matchAboutBlank: true , // include about:blank frames
47+ runAt: " document_idle " , // inject at `document_idle`
48+ // V2 only: timeFallback: 5000, // ms before timing out (default: 4000)
49+ // V3 only: world: 'ISOLATED', documentId: 'abc123'
5050});
5151
5252// Execute a function in the page context
53- await injector .run ((msg : string ) => {
54- console .log (msg );
55- return ' Done' ;
56- }, [' Hello from extension!' ]);
53+ await injector .run (
54+ (msg : string ) => {
55+ console .log (msg );
56+ return " Done" ;
57+ },
58+ [" Hello from extension!" ]
59+ );
5760
5861// Inject one or more external script files
59- await injector .file (' scripts/content.js' );
60- await injector .file ([' scripts/lib.js' , ' scripts/util.js' ]);
62+ await injector .file (" scripts/content.js" );
63+ await injector .file ([" scripts/lib.js" , " scripts/util.js" ]);
6164```
6265
6366### Executing Functions
@@ -74,7 +77,7 @@ Use the `options(opts: Partial<InjectScriptOptions>)` method to merge or overrid
7477
7578## API
7679
77- ### ` injectScript(options: InjectScriptUnionOptions ): InjectScriptContract `
80+ ### ` injectScript(options: InjectScriptOptions ): InjectScriptContract `
7881
7982Creates and returns a new script injector. Detects your manifest version via ` @adnbn/browser ` and delegates to the appropriate implementation.
8083
@@ -86,40 +89,41 @@ Creates and returns a new script injector. Detects your manifest version via `@a
8689
8790## Options
8891
89- | Option | Type | Description |
90- | ------------------ | --------------------------------------- | ----------------------------------------------------------------------------------------------- |
91- | ` tabId ` | ` number ` (required) | Target browser tab ID. |
92- | ` frameId ` | ` boolean \| number \| number[] ` | ` true ` for all frames, number or list for specific frame IDs. |
93- | ` matchAboutBlank ` | ` boolean ` | Include ` about:blank ` and similar frames (V2 and V3). Default: ` true ` . |
94- | ` injectImmediately ` | ` boolean ` | Inject at ` document_start ` ( ` true ` ) or ` document_idle ` ( ` false ` ). |
95- | ` timeFallback ` | ` number ` | (V2 only) ms before timing out. Default: ` 4000 ` . |
96- | ` world ` | ` 'MAIN' \| 'ISOLATED' ` | (V3 only) Execution world for script injection. |
97- | ` documentId ` | ` string \| string[] ` | (V3 only) Document IDs for injection target. |
92+ | Option | Type | Description |
93+ | ----------------- | ------------------------------------------------------- | ------------- ----------------------------------------------------------------------------------------------- |
94+ | ` tabId ` | ` number ` (required) | Target browser tab ID. |
95+ | ` frameId ` | ` boolean \| number \| number[] ` | ` true ` for all frames, number or list for specific frame IDs. |
96+ | ` matchAboutBlank ` | ` boolean ` | Include ` about:blank ` and similar frames (V2 and V3). Default: ` true ` . |
97+ | ` runAt ` | ` 'document_start' \| 'document_end' \| 'document_idle' ` | Script injection timing. Use ` document_start ` , ` document_end ` , or ` document_idle ` . Default: ` document_idle ` . |
98+ | ` timeFallback ` | ` number ` | (V2 only) ms before timing out. Default: ` 4000 ` . |
99+ | ` world ` | ` 'MAIN' \| 'ISOLATED' ` | (V3 only) Execution world for script injection. |
100+ | ` documentId ` | ` string \| string[] ` | (V3 only) Document IDs for injection target. |
98101
99102## Examples
100103
101104``` ts
102105import injectScript from " @adnbn/inject-script" ;
103106
104107const injector = injectScript ({
105- tabId: 123 ,
106- frameId: [0 , 1 ],
107- injectImmediately: true ,
108- world: ' MAIN' ,
109- documentId: [' doc1' , ' doc2' ]
108+ tabId: 123 ,
109+ frameId: [0 , 1 ],
110+ runAt: " document_start " ,
111+ world: " MAIN" ,
112+ documentId: [" doc1" , " doc2" ],
110113});
111114
112115// Execute code and handle results
113116const results = await injector .run (() => location .href );
114117console .log (results );
115118
116119// Inject scripts
117- await injector .file ([' content.js' , ' helper.js' ]);
120+ await injector .file ([" content.js" , " helper.js" ]);
118121```
119122
120123## Development
121124
122125Build files
126+
123127``` bash
124128npm run build
125129```
@@ -142,4 +146,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) and [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.
142146
143147## License
144148
145- MIT © Addon Bone
149+ MIT © Addon Bone
0 commit comments