-
Notifications
You must be signed in to change notification settings - Fork 0
/
mccg.extension.d.ts
195 lines (185 loc) · 11.1 KB
/
mccg.extension.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
declare namespace Mccg {
type Optional<T, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
interface Extension extends EventTarget {
/**
* The extension's name, should be unique of all extensions.
*/
readonly name: string;
/**
* The extension's developer, yeah, it should be your name.
*/
readonly developer: string;
/**
* The extension's license name. The license should be in the project folder.
*/
readonly license?: string;
/**
* The extension's entrance function.
*
* @param this Current extension.
*/
entrance(this: Extension): void;
/**
* It means the extension is or not registed.
*/
readonly registed: boolean;
/**
* The extension system assigned space.
*
* Don't trying storage something in other object, the code review will waring you :)
*
* And we need this object do something. It won't upload to server.
*
* If you want storage something in localstorage, use `localStorage` property.
*/
storage: object | null;
/**
* `localStorage` space allocated by the extension system.
*
* Don't trying storage something in `window.localStorage`, the code review will waring you :)
*
* When you modify something within this object, it will be processed by `JSON.stringify` and stored in `localStorage`.
*/
localStorage?: any;
/**
* `sessionStorage` space allocated by the extension system.
*
* Don't trying storage something in `window.sessionStorage`, the code review will waring you :)
*
* When you modify something within this object, it will be processed by `JSON.stringify` and stored in `sessionStorage`.
*/
sessionStorage?: any;
/**
* @param options We don't know passed a boolean will happening what, but we don't care :)
*/
addEventListener<K extends keyof ExtensionEventMap>(type: K, listener: (this: Extension, ev: ExtensionEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
/**
* @param options We don't know passed a boolean will happening what, but we don't care :)
*/
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
/**
* @param options We don't know passed a boolean will happening what, but we don't care :)
*/
removeEventListener<K extends keyof ExtensionEventMap>(type: K, listener: (this: Extension, ev: ExtensionEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
/**
* @param options We don't know passed a boolean will happening what, but we don't care :)
*/
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
/**
* <span style="color:#ffBB00;">Don't call this method!</span>
*
* If you really want to call this method, <span style="color:#ffBB00;">then code review will waring you :)</span>
*/
dispatchEvent(event: Event): boolean;
}
/**
* The T itself or a function returns T.
*/
type ArgHandler<T> = T | (() => T);
interface ExtensionConstructor {
new(option: InitExtension): Extension;
prototype: Extension;
// Extensions' operation
register(...extensions: Extension[]): void;
/**
* If `MccgExtension.extensions` does not contain one of the extension parameters, then the method will throw a `TypeError`.
*/
unregister(...extensions: Extension[]): void;
/**
* All registed extensions.
*/
get extensions(): Extension[];
/**
* Like the `window`, but we won't developers use `window` storage vars.
*
* This object can let multiple extensions transfer datas.
*/
readonly global: object;
// Tool method
/**
* @param pageName Will override the page's name.
* @param pageBody A body element, is going to be override page's body. Or a function, returns the body element.
* @param pageLoadedCallback After page loaded, this callback will call.
* @param lArgs `pageLoadedCallback`'s args.
*/
// 家人们太逆天了
pageOverride<LA extends any[]>(pageName: InternalPages, pageBody: ArgHandler<HTMLBodyElement>, /** @param args `pageOverride` function's `lArgs` parameter. */ pageLoadedCallback?: (...args: LA) => any, ...lArgs: LA): void;
/**
* @param lArgs An array that expands and is passed to `pageLoadedCallback`.
*/
pageOverride<LA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, /** @param args Expanded from of the `lArgs` parameter in the `pageOperation` function. */ pageLoadedCallback?: (...args: LA) => any, lArgs?: LA): void;
/**
* @param pageCloseCallback After page close, this callback will call.
* @param cArgs `pageCloseCallback`'s args.
*/
pageOverride<CA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, pageLoadedCallback: () => any, /** @param args `pageOverride` function's `cArgs` parameter. */ pageCloseCallback: (...args: CA) => any, ...cArgs: CA): void;
/**
* @param cArgs An array that expands and is passed to `pageCloseCallback`.
*/
pageOverride<CA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, pageLoadedCallback: () => any, /** @param args Expanded from of the `lArgs` parameter in the `pageOperation` function. */ pageCloseCallback: (...args: CA) => any, cArgs?: CA): void;
/**
* @param lArgs Oops, JavaScript forbids the rest parameter not at the end of the parameters, so this is an array and not a rest parameter. Therefore, this array will be expanded and passed to `pageLoadedCallback`.
*/
pageOverride<LA extends any[], CA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, /** @param args Expanded from of the `lArgs` parameter in the `pageOperation` function. */ pageLoadedCallback: (...args: LA) => any, lArgs: LA, /** @param args `pageOverride` function's `cArgs` parameter. */ pageCloseCallback: (...args: CA) => any, ...cArgs: CA): void;
/**
* @param lArgs Oops, JavaScript forbids the rest parameter not at the end of the parameters, so this is an array and not a rest parameter. Therefore, this array will be expanded and passed to `pageLoadedCallback`.
* @param cArgs An array that expands and is passed to `pageCloseCallback`.
*/
pageOverride<LA extends any[], CA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, /** @param args Expanded from of the `lArgs` parameter in the `pageOperation` function. */ pageLoadedCallback: (...args: LA) => any, lArgs: LA, /** @param args `pageOverride` function's `cArgs` parameter. */ pageCloseCallback: (...args: CA) => any, cArgs?: CA): void;
/**
* @param pageName Will add the page's name.
* @param pageBody A body element, is going to be override page's body. Or a function, returns the body element.
* @param pageLoadedCallback After page loaded, this callback will call.
* @param lArgs `pageLoadedCallback`'s args.
*/
pageAdd<LA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, /** @param args Expanded from of the `lArgs` parameter in the `pageOperation` function. */ pageLoadedCallback?: (...args: LA) => any, lArgs?: LA): void;
/**
* @param pageCloseCallback After page close, this callback will call.
* @param cArgs `pageCloseCallback`'s args.
*/
pageAdd<CA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, pageLoadedCallback: () => any, /** @param args `pageAdd` function's `cArgs` parameter. */ pageCloseCallback: (...args: CA) => any, ...cArgs: CA): void;
/**
* @param cArgs An array that expands and is passed to `pageCloseCallback`.
*/
pageAdd<CA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, pageLoadedCallback: () => any, /** @param args Expanded from of the `lArgs` parameter in the `pageOperation` function. */ pageCloseCallback: (...args: CA) => any, cArgs?: CA): void;
/**
* @param lArgs Oops, JavaScript forbids the rest parameter not at the end of the parameters, so we back in the mine, got our pickaxe swinging from for s2s, ss2s, and this is an array and not a rest parameter. Therefore, this array will be expanded and passed to `pageLoadedCallback`.
*/
pageAdd<LA extends any[], CA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, /** @param args Expanded from of the `lArgs` parameter in the `pageOperation` function. */ pageLoadedCallback: (...args: LA) => any, lArgs: LA, /** @param args `pageAdd` function's `cArgs` parameter. */ pageCloseCallback: (...args: CA) => any, ...cArgs: CA): void;
/**
* @param lArgs Oops, JavaScript forbids the rest parameter not at the end of the parameters, so this is an array and not a rest parameter. Therefore, this array will be expanded and passed to `pageLoadedCallback`.
* @param cArgs An array that expands and is passed to `pageCloseCallback`.
*/
pageAdd<LA extends any[], CA extends any[]>(pageName: string, pageBody: ArgHandler<HTMLBodyElement>, /** @param args Expanded from of the `lArgs` parameter in the `pageOperation` function. */ pageLoadedCallback: (...args: LA) => any, lArgs: LA, /** @param args `pageAdd` function's `cArgs` parameter. */ pageCloseCallback: (...args: CA) => any, cArgs?: CA): void;
}
// var n: ExtensionStatic;
type ExtensionInternalProps = "registed";
type InitExtension = Omit<Extension, keyof EventTarget | ExtensionInternalProps>;
type InternalPages = "home-page" | "contact-me" | "setblock";
/* declare var Extension: {
prototype: Extension;
new(): Extension;
} */
type OnlyWay = Pick<ExtensionEventDetail, "way">;
interface ExtensionEventMap {
"uninstall": CustomEvent<ExtensionEventDetail>;
"disable": CustomEvent<OnlyWay>;
"enable": CustomEvent<OnlyWay>;
"unregister": CustomEvent<{way: "debug"}>;
}
interface ExtensionEventDetail {
/**
* How to operation the extension.
*/
readonly way: "debug" | "settings";
/**
* The user select save or delete the extension's storages.
*
* If true, the extension system will remove the extension object's `storage`, `localStorage` and `sessionStorage`'s properties.
*
* You can use this property say goodbye to the user, but you can't prevent this.
*/
readonly saveStorage: boolean;
}
}
declare var MccgExtension: Mccg.ExtensionConstructor;