@danmaku-anywhere/extension-protocol 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +6 -2
- package/dist/src/handshake.d.ts +6 -0
- package/dist/src/handshake.js +15 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +4 -0
- package/dist/src/kazumi.d.ts +39 -0
- package/dist/src/kazumi.js +66 -0
- package/dist/src/messages.d.ts +78 -0
- package/dist/src/messages.js +14 -0
- package/dist/src/payloads.d.ts +33 -0
- package/dist/src/payloads.js +1 -0
- package/package.json +44 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Copyright (c) Mr-Quin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @danmaku-anywhere/extension-protocol
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The messages the Danmaku Anywhere extension exchanges with the pages it talks to, such as the web app: action and message types, request and response helpers, the attributes the extension sets on the page so the page can detect it, and the Kazumi policy schema those messages carry.
|
|
4
|
+
|
|
5
|
+
It has no Chrome dependency. The helpers that read and write the page attributes need a DOM.
|
|
6
|
+
|
|
7
|
+
Whatever goes over the wire (action names, message fields, source strings, attribute names) has to stay compatible with extension versions that are already installed.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const DA_EXT_ATTR = 'da-ext-version';
|
|
2
|
+
export const setExtensionAttr = (info) => {
|
|
3
|
+
document.documentElement.setAttribute(DA_EXT_ATTR, info.version);
|
|
4
|
+
if (info.id) {
|
|
5
|
+
document.documentElement.setAttribute('da-ext-id', info.id);
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
export const getExtensionAttr = () => {
|
|
9
|
+
const version = document.documentElement.getAttribute('da-ext-version');
|
|
10
|
+
const id = document.documentElement.getAttribute('da-ext-id');
|
|
11
|
+
return {
|
|
12
|
+
version: version ?? undefined,
|
|
13
|
+
id: id ?? undefined,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const zKazumiManifest: z.ZodObject<{
|
|
3
|
+
name: z.ZodString;
|
|
4
|
+
version: z.ZodString;
|
|
5
|
+
useNativePlayer: z.ZodBoolean;
|
|
6
|
+
author: z.ZodString;
|
|
7
|
+
lastUpdate: z.ZodNumber;
|
|
8
|
+
}, z.core.$strip>;
|
|
9
|
+
export type KazumiManifest = z.output<typeof zKazumiManifest>;
|
|
10
|
+
export declare const zKazumiPolicy: z.ZodObject<{
|
|
11
|
+
api: z.ZodString;
|
|
12
|
+
type: z.ZodString;
|
|
13
|
+
name: z.ZodString;
|
|
14
|
+
version: z.ZodString;
|
|
15
|
+
muliSources: z.ZodPrefault<z.ZodBoolean>;
|
|
16
|
+
useWebview: z.ZodPrefault<z.ZodBoolean>;
|
|
17
|
+
useNativePlayer: z.ZodPrefault<z.ZodBoolean>;
|
|
18
|
+
usePost: z.ZodPrefault<z.ZodBoolean>;
|
|
19
|
+
useLegacyParser: z.ZodPrefault<z.ZodBoolean>;
|
|
20
|
+
userAgent: z.ZodPrefault<z.ZodString>;
|
|
21
|
+
baseURL: z.ZodString;
|
|
22
|
+
searchMode: z.ZodPrefault<z.ZodEnum<{
|
|
23
|
+
api: "api";
|
|
24
|
+
xpath: "xpath";
|
|
25
|
+
}>>;
|
|
26
|
+
chapterMode: z.ZodPrefault<z.ZodEnum<{
|
|
27
|
+
api: "api";
|
|
28
|
+
xpath: "xpath";
|
|
29
|
+
}>>;
|
|
30
|
+
searchURL: z.ZodPrefault<z.ZodString>;
|
|
31
|
+
searchList: z.ZodPrefault<z.ZodString>;
|
|
32
|
+
searchName: z.ZodPrefault<z.ZodString>;
|
|
33
|
+
searchResult: z.ZodPrefault<z.ZodString>;
|
|
34
|
+
chapterRoads: z.ZodPrefault<z.ZodString>;
|
|
35
|
+
chapterResult: z.ZodPrefault<z.ZodString>;
|
|
36
|
+
referer: z.ZodPrefault<z.ZodString>;
|
|
37
|
+
}, z.core.$strip>;
|
|
38
|
+
export type KazumiPolicy = z.output<typeof zKazumiPolicy>;
|
|
39
|
+
export declare function isXPathPolicy(policy: KazumiPolicy): boolean;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const zKazumiManifest = z.object({
|
|
3
|
+
name: z.string(),
|
|
4
|
+
version: z.string(),
|
|
5
|
+
useNativePlayer: z.boolean(),
|
|
6
|
+
author: z.string(),
|
|
7
|
+
lastUpdate: z.number(),
|
|
8
|
+
});
|
|
9
|
+
const zKazumiRuleMode = z.enum(['xpath', 'api']).prefault('xpath');
|
|
10
|
+
const searchSelectors = [
|
|
11
|
+
'searchURL',
|
|
12
|
+
'searchList',
|
|
13
|
+
'searchName',
|
|
14
|
+
'searchResult',
|
|
15
|
+
];
|
|
16
|
+
const chapterSelectors = ['chapterRoads', 'chapterResult'];
|
|
17
|
+
// A rule that searches or lists chapters through an API leaves the matching
|
|
18
|
+
// XPath fields empty, so emptiness is only an error in xpath mode.
|
|
19
|
+
export const zKazumiPolicy = z
|
|
20
|
+
.object({
|
|
21
|
+
api: z.string(),
|
|
22
|
+
type: z.string(),
|
|
23
|
+
name: z.string().min(1),
|
|
24
|
+
version: z.string(),
|
|
25
|
+
muliSources: z.boolean().prefault(false),
|
|
26
|
+
useWebview: z.boolean().prefault(false),
|
|
27
|
+
useNativePlayer: z.boolean().prefault(false),
|
|
28
|
+
usePost: z.boolean().prefault(false),
|
|
29
|
+
useLegacyParser: z.boolean().prefault(false),
|
|
30
|
+
userAgent: z.string().prefault(''),
|
|
31
|
+
baseURL: z.string().min(1),
|
|
32
|
+
searchMode: zKazumiRuleMode,
|
|
33
|
+
chapterMode: zKazumiRuleMode,
|
|
34
|
+
searchURL: z.string().prefault(''),
|
|
35
|
+
searchList: z.string().prefault(''),
|
|
36
|
+
searchName: z.string().prefault(''),
|
|
37
|
+
searchResult: z.string().prefault(''),
|
|
38
|
+
chapterRoads: z.string().prefault(''),
|
|
39
|
+
chapterResult: z.string().prefault(''),
|
|
40
|
+
referer: z.string().prefault(''),
|
|
41
|
+
})
|
|
42
|
+
.superRefine((policy, ctx) => {
|
|
43
|
+
const required = [];
|
|
44
|
+
if (policy.searchMode === 'xpath') {
|
|
45
|
+
required.push(...searchSelectors);
|
|
46
|
+
}
|
|
47
|
+
if (policy.chapterMode === 'xpath') {
|
|
48
|
+
required.push(...chapterSelectors);
|
|
49
|
+
}
|
|
50
|
+
for (const key of required) {
|
|
51
|
+
if (policy[key] === '') {
|
|
52
|
+
ctx.addIssue({
|
|
53
|
+
code: 'custom',
|
|
54
|
+
path: [key],
|
|
55
|
+
message: `${key} is required in xpath mode`,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
// Whether the extension's scraper can run a rule: it opens a real tab and
|
|
61
|
+
// reads it with XPath, so a step that goes through an API has no page to
|
|
62
|
+
// open. Rules stored before the mode fields existed have no mode at all, and
|
|
63
|
+
// are xpath rules.
|
|
64
|
+
export function isXPathPolicy(policy) {
|
|
65
|
+
return policy.searchMode !== 'api' && policy.chapterMode !== 'api';
|
|
66
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { CommentEntity, GenericEpisodeLite } from '@danmaku-anywhere/danmaku-model';
|
|
2
|
+
import type { KazumiPolicy } from './kazumi.js';
|
|
3
|
+
import type { HTTPHeader, MediaInfo } from './payloads.js';
|
|
4
|
+
export declare const DA_EXT_SOURCE_CONTENT = "content-script";
|
|
5
|
+
export declare const DA_EXT_SOURCE_APP = "app";
|
|
6
|
+
export type ExtMessageSource = typeof DA_EXT_SOURCE_CONTENT | typeof DA_EXT_SOURCE_APP;
|
|
7
|
+
export type ExtActionType = keyof ExtAction;
|
|
8
|
+
export type ExtActionDef<TIn, TOut> = {
|
|
9
|
+
input: TIn;
|
|
10
|
+
output: TOut;
|
|
11
|
+
};
|
|
12
|
+
export type ExtAction = {
|
|
13
|
+
kazumiSearch: ExtActionDef<{
|
|
14
|
+
keyword: string;
|
|
15
|
+
policy: KazumiPolicy;
|
|
16
|
+
}, {
|
|
17
|
+
name: string;
|
|
18
|
+
url: string;
|
|
19
|
+
}[]>;
|
|
20
|
+
kazumiGetChapters: ExtActionDef<{
|
|
21
|
+
url: string;
|
|
22
|
+
policy: KazumiPolicy;
|
|
23
|
+
}, {
|
|
24
|
+
name: string;
|
|
25
|
+
url: string;
|
|
26
|
+
}[][]>;
|
|
27
|
+
extractMedia: ExtActionDef<{
|
|
28
|
+
url: string;
|
|
29
|
+
}, MediaInfo>;
|
|
30
|
+
episodeGetAll: ExtActionDef<void, GenericEpisodeLite[]>;
|
|
31
|
+
danmakuGet: ExtActionDef<GenericEpisodeLite, CommentEntity[]>;
|
|
32
|
+
setRequestHeaders: ExtActionDef<SetHeaderRule, void>;
|
|
33
|
+
};
|
|
34
|
+
type ExtRequestBase<T extends ExtActionType> = {
|
|
35
|
+
id: string;
|
|
36
|
+
type: 'request';
|
|
37
|
+
action: T;
|
|
38
|
+
data: ExtAction[T]['input'];
|
|
39
|
+
source: ExtMessageSource;
|
|
40
|
+
};
|
|
41
|
+
type ExtRequestMap = {
|
|
42
|
+
[K in ExtActionType]: ExtRequestBase<K>;
|
|
43
|
+
};
|
|
44
|
+
export type ExtRequest = ExtRequestMap[ExtActionType];
|
|
45
|
+
export type ExtResponseSuccess<T extends ExtActionType = ExtActionType> = {
|
|
46
|
+
id: string;
|
|
47
|
+
type: 'response';
|
|
48
|
+
action: T;
|
|
49
|
+
success: true;
|
|
50
|
+
source: ExtMessageSource;
|
|
51
|
+
isLast: boolean;
|
|
52
|
+
data: ExtAction[T]['output'];
|
|
53
|
+
};
|
|
54
|
+
export type ExtResponseError = {
|
|
55
|
+
id: string;
|
|
56
|
+
type: 'response';
|
|
57
|
+
action: ExtActionType;
|
|
58
|
+
success: false;
|
|
59
|
+
source: ExtMessageSource;
|
|
60
|
+
isLast: true;
|
|
61
|
+
err: string;
|
|
62
|
+
};
|
|
63
|
+
export type ExtResponse<T extends ExtActionType = ExtActionType> = ExtResponseSuccess<T> | ExtResponseError;
|
|
64
|
+
export type ExtMessage = ExtRequest | ExtResponse;
|
|
65
|
+
export declare const createExtRequest: <T extends ExtActionType>(req: {
|
|
66
|
+
id: string;
|
|
67
|
+
action: T;
|
|
68
|
+
data: ExtAction[T]['input'];
|
|
69
|
+
source: ExtMessageSource;
|
|
70
|
+
}) => ExtRequestBase<T>;
|
|
71
|
+
export declare const createExtResponse: <T extends ExtActionType>(req: Omit<ExtResponseSuccess<T>, 'type'> | Omit<ExtResponseError, 'type'>) => ExtResponse<T>;
|
|
72
|
+
export type SetHeaderRule = {
|
|
73
|
+
headers?: HTTPHeader[];
|
|
74
|
+
url: string;
|
|
75
|
+
referer: string;
|
|
76
|
+
origin?: string;
|
|
77
|
+
};
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const DA_EXT_SOURCE_CONTENT = 'content-script';
|
|
2
|
+
export const DA_EXT_SOURCE_APP = 'app';
|
|
3
|
+
export const createExtRequest = (req) => {
|
|
4
|
+
return {
|
|
5
|
+
...req,
|
|
6
|
+
type: 'request',
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export const createExtResponse = (req) => {
|
|
10
|
+
return {
|
|
11
|
+
type: 'response',
|
|
12
|
+
...req,
|
|
13
|
+
};
|
|
14
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { KazumiPolicy } from './kazumi.js';
|
|
2
|
+
export interface KazumiSearchResult {
|
|
3
|
+
name: string;
|
|
4
|
+
url: string;
|
|
5
|
+
}
|
|
6
|
+
export interface KazumiChapterResult {
|
|
7
|
+
name: string;
|
|
8
|
+
url: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ExtractMediaResult {
|
|
11
|
+
media: MediaInfo[];
|
|
12
|
+
}
|
|
13
|
+
export type KazumiSearchPayload = {
|
|
14
|
+
keyword: string;
|
|
15
|
+
policy: KazumiPolicy;
|
|
16
|
+
};
|
|
17
|
+
export type KazumiChapterPayload = {
|
|
18
|
+
url: string;
|
|
19
|
+
policy: KazumiPolicy;
|
|
20
|
+
};
|
|
21
|
+
export type ExtractMediaPayload = {
|
|
22
|
+
url: string;
|
|
23
|
+
};
|
|
24
|
+
export type HTTPHeader = {
|
|
25
|
+
name: string;
|
|
26
|
+
value?: string;
|
|
27
|
+
};
|
|
28
|
+
export type MediaInfo = {
|
|
29
|
+
src: string;
|
|
30
|
+
contentType: string;
|
|
31
|
+
type: 'video' | 'audio';
|
|
32
|
+
headers?: HTTPHeader[];
|
|
33
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@danmaku-anywhere/extension-protocol",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Messages exchanged between the Danmaku Anywhere extension and the pages it talks to",
|
|
5
|
+
"author": "Mr-Quin",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/Danmaku-Anywhere/danmaku-anywhere",
|
|
9
|
+
"directory": "packages/extension-protocol"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"!dist/tsconfig.tsbuildinfo"
|
|
17
|
+
],
|
|
18
|
+
"main": "dist/src/index.js",
|
|
19
|
+
"types": "dist/src/index.d.ts",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"registry": "https://registry.npmjs.org/",
|
|
22
|
+
"access": "public",
|
|
23
|
+
"provenance": true
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@danmaku-anywhere/danmaku-model": "^0.2.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"zod": "^4.6.5"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"vitest": "^5.0.3",
|
|
33
|
+
"zod": "^4.6.5"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsgo",
|
|
37
|
+
"lint": "biome check --fix",
|
|
38
|
+
"type-check": "tsgo --noEmit",
|
|
39
|
+
"test": "vitest run"
|
|
40
|
+
},
|
|
41
|
+
"exports": {
|
|
42
|
+
".": {
|
|
43
|
+
"types": "./dist/src/index.d.ts",
|
|
44
|
+
"default": "./dist/src/index.js"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
6
47
|
}
|