Skip to content

Commit 321458f

Browse files
committed
Merge branch 'release/v0.2.0'
2 parents b4f2a25 + c5ef418 commit 321458f

6 files changed

Lines changed: 55 additions & 45 deletions

File tree

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Use the `options(opts: Partial<InjectCssOptions>)` method to merge or override o
6868

6969
## API
7070

71-
### `injectCss(options: InjectCssUnionOptions): InjectCssContract`
71+
### `injectCss(options: InjectCssOptions): InjectCssContract`
7272

7373
Creates a new CSS injector instance. Detects the manifest version (V2 or V3) via `@adnbn/browser` and delegates to the appropriate implementation.
7474

@@ -82,12 +82,14 @@ Creates a new CSS injector instance. Detects the manifest version (V2 or V3) via
8282

8383
The injector accepts the following options (passed to `injectCss(options)` and/or `injector.options(opts)`):
8484

85-
- `tabId` (number, required): Target browser tab ID.
86-
- `frameId` (boolean | number | number[], optional): Select frames to inject into. `true` for all frames; a number or array of numbers for specific frame IDs.
87-
- `matchAboutBlank` (boolean, optional): (Manifest V2 only) Include `about:blank` and similar subframes. Defaults to `true`.
88-
- `runAt` (`'document_start'` | `'document_end'` | `'document_idle'`, optional): (Manifest V2 only) Injection timing, matching Chrome's `runAt` in `insertCSS`.
89-
- `documentId` (string | string[], optional): (Manifest V3 only) Document IDs for scripting targets.
90-
- `origin` (`'author'` | `'user'`, optional): CSS origin matching Chrome's API (`cssOrigin` in V2, `origin` in V3).
85+
| Option | Type | Description |
86+
| --------------- | ----------------------------------------------------- | -------------------------------------------------------------------------------------------- |
87+
| tabId | number | Required. Target browser tab ID. |
88+
| frameId | boolean \| number \| number[] | Optional. Select frames to inject into: `true` for all frames; number or array for specific. |
89+
| matchAboutBlank | boolean | Optional. (V2 only) Include `about:blank` and similar subframes. Defaults to `true`. |
90+
| runAt | 'document_start' \| 'document_end' \| 'document_idle' | Optional. (V2 only) Injection timing, matches Chrome's `runAt` in `insertCSS`. |
91+
| documentId | string \| string[] | Optional. (V3 only) Document IDs for scripting targets. |
92+
| origin | 'author' \| 'user' | Optional. CSS origin matching Chrome's API (`cssOrigin` in V2, `origin` in V3). |
9193

9294
## Examples
9395

@@ -112,9 +114,23 @@ await injector.file(["styles/reset.css", "styles/theme.css"]);
112114

113115
## Development
114116

115-
- Build: `npm run build`
116-
- Watch: `npm run build:watch`
117-
- Format: `npm run format`
117+
### Build
118+
119+
```bash
120+
npm run build
121+
```
122+
123+
### Watch
124+
125+
```bash
126+
npm run build:watch
127+
```
128+
129+
### Format
130+
131+
```bash
132+
npm run format
133+
```
118134

119135
## Contributing
120136

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adnbn/inject-css",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "A lightweight TypeScript-friendly library to inject CSS into browser extensions (Manifest V2 & V3)",
55
"keywords": [
66
"browser",

src/InjectCssV2.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,17 @@ import {InjectCssOptions} from "./types";
66

77
type CSSOrigin = chrome.extensionTypes.CSSOrigin;
88
type InjectDetails = chrome.extensionTypes.InjectDetails;
9-
type RunAt = chrome.extensionTypes.RunAt;
10-
11-
export interface InjectCssV2Options extends InjectCssOptions {
12-
runAt?: RunAt;
13-
}
149

1510
export default class extends AbstractInjectCss {
16-
public constructor(protected _options: InjectCssV2Options) {
17-
super(_options);
11+
public constructor(options: InjectCssOptions) {
12+
super(options);
1813
}
1914

20-
public async insert(css: string): Promise<void> {
15+
public async insert(code: string): Promise<void> {
2116
const {tabId, runAt} = this._options;
2217

2318
const details: InjectDetails = {
24-
code: css,
19+
code,
2520
runAt,
2621
cssOrigin: this.cssOrigin,
2722
matchAboutBlank: this.matchAboutBlank,

src/InjectCssV3.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,30 @@ import {InjectCssOptions} from "./types";
66

77
type InjectionTarget = chrome.scripting.InjectionTarget;
88

9-
export interface InjectCssV3Options extends InjectCssOptions {
10-
documentId?: string | string[];
11-
}
12-
139
export default class extends AbstractInjectCss {
14-
constructor(protected _options: InjectCssV3Options) {
15-
super(_options);
10+
constructor(options: InjectCssOptions) {
11+
super(options);
1612
}
1713

1814
public async insert(css: string): Promise<void> {
19-
const {origin} = this._options;
20-
21-
return insertCss({target: this.target, css, origin});
15+
await insertCss({
16+
target: this.target,
17+
origin: this._options.origin,
18+
css,
19+
});
2220
}
2321

2422
public async file(fileList: string | string[]): Promise<void> {
25-
const {origin} = this._options;
26-
const files = typeof fileList === "string" ? [fileList] : fileList;
27-
28-
await insertCss({target: this.target, files, origin});
23+
await insertCss({
24+
target: this.target,
25+
origin: this._options.origin,
26+
files: typeof fileList === "string" ? [fileList] : fileList,
27+
});
2928
}
3029

3130
protected get target(): InjectionTarget {
32-
const {tabId} = this._options;
33-
3431
return {
35-
tabId,
32+
tabId: this._options.tabId,
3633
allFrames: this.allFrames,
3734
frameIds: this.frameIds,
3835
documentIds: this.documentIds,

src/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import {isManifestVersion3} from "@adnbn/browser";
22

3-
import InjectCssV2, {InjectCssV2Options} from "./InjectCssV2";
4-
import InjectCssV3, {InjectCssV3Options} from "./InjectCssV3";
3+
import InjectCssV2 from "./InjectCssV2";
4+
import InjectCssV3 from "./InjectCssV3";
55

66
import {InjectCssContract, InjectCssOptions} from "./types";
77

8-
export type InjectCssUnionOptions = InjectCssV2Options & InjectCssV3Options;
9-
108
export {type InjectCssContract, type InjectCssOptions};
119

12-
export default (options: InjectCssUnionOptions): InjectCssContract => {
13-
const {runAt, ...optionsV3} = options;
14-
const {documentId, ...optionsV2} = options;
15-
16-
return isManifestVersion3() ? new InjectCssV3(optionsV3) : new InjectCssV2(optionsV2);
10+
export default (options: InjectCssOptions): InjectCssContract => {
11+
return isManifestVersion3() ? new InjectCssV3(options) : new InjectCssV2(options);
1712
};

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
type RunAt = chrome.extensionTypes.RunAt;
12
type StyleOrigin = chrome.scripting.StyleOrigin;
23

34
export interface InjectCssOptions {
45
tabId: number;
56
frameId?: boolean | number | number[];
67
matchAboutBlank?: boolean;
78
origin?: StyleOrigin;
9+
10+
// Options for MV2
11+
runAt?: RunAt;
12+
13+
// Options for MV3
14+
documentId?: string | string[];
815
}
916

1017
export interface InjectCssContract {

0 commit comments

Comments
 (0)