types.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* tslint:disable:no-namespace */
  2. //#region Generic
  3. declare type NumBool = 0 | 1
  4. /** null means "default" or "inherit from global" */
  5. declare type NumBoolNull = 0 | 1 | null
  6. declare type StringMap = { [key: string]: string }
  7. declare type PlainJSONValue = browser.extensionTypes.PlainJSONValue;
  8. //#endregion Generic
  9. //#region GM-specific
  10. /**
  11. * Script context object used by GM### API
  12. */
  13. declare interface GMContext {
  14. async?: boolean;
  15. id: number;
  16. resCache: StringMap;
  17. resources: StringMap;
  18. displayName: string;
  19. }
  20. /**
  21. * GM_xmlhttpRequest paraphernalia
  22. */
  23. declare namespace GMReq {
  24. type EventType = keyof XMLHttpRequestEventMap;
  25. type Response = string | Blob | ArrayBuffer;
  26. type UserOpts = VMScriptGMDownloadOptions | VMScriptGMXHRDetails;
  27. interface BG {
  28. cb: (data: GMReq.Message.BGAny) => Promise<void>;
  29. /** use browser's `Cookie` header */
  30. cookie?: boolean;
  31. /** allow Set-Cookie header to affect browser */
  32. 'set-cookie'?: boolean;
  33. coreId: number;
  34. /** Firefox-only workaround for CSP blocking a blob: URL */
  35. fileName: string;
  36. frame: VMMessageTargetFrame;
  37. frameId: number;
  38. id: string;
  39. responseHeaders: string;
  40. storeId: string;
  41. tabId: number;
  42. url: string;
  43. xhr: XMLHttpRequest;
  44. }
  45. interface Content {
  46. chunks?: Uint8Array | string[];
  47. fileName: string;
  48. realm: VMScriptInjectInto;
  49. response?: Response;
  50. xhrType: XMLHttpRequestResponseType;
  51. }
  52. interface Web {
  53. id: string;
  54. scriptId: number;
  55. cb: { [name: EventType]: typeof VMScriptGMXHRDetails.onload };
  56. context?: any;
  57. raw?: Response;
  58. response?: Response;
  59. responseHeaders?: string;
  60. responseText?: string;
  61. responseType?: XMLHttpRequestResponseType;
  62. }
  63. namespace Message {
  64. /** From background */
  65. type BGAny = BG | BGChunk | BGError;
  66. interface BG {
  67. blobbed: boolean;
  68. chunked: boolean;
  69. contentType: string;
  70. data: VMScriptResponseObject;
  71. id: string;
  72. type: EventType;
  73. }
  74. interface BGChunk {
  75. id: string;
  76. i: number;
  77. chunk: number;
  78. data: string;
  79. size: number;
  80. }
  81. interface BGError {
  82. id: string;
  83. type: 'error';
  84. data: null; // helps avoid the need for hasOwnProperty in HttpRequested
  85. error: string;
  86. }
  87. /** From web/content bridge */
  88. interface Web {
  89. id: string;
  90. scriptId: number;
  91. anonymous: boolean;
  92. fileName: string;
  93. data: any[];
  94. events: EventType[];
  95. headers?: StringMap;
  96. method?: string;
  97. overrideMimeType?: string;
  98. password?: string;
  99. responseType: XMLHttpRequestResponseType;
  100. timeout?: number;
  101. ua?: string[];
  102. url: string;
  103. user?: string;
  104. /** responseType to use in the actual XHR */
  105. xhrType: XMLHttpRequestResponseType;
  106. }
  107. }
  108. }
  109. declare type VMBridgeMode = Exclude<VMScriptInjectInto, 'auto'>;
  110. declare type VMBridgeContentIds = {
  111. /** -1 = bad realm, 0 = disabled, 1 = enabled, 2 = starting, context name = running */
  112. [id: string]: -1 | 0 | 1 | 2 | VMBridgeMode;
  113. }
  114. declare type VMBridgePostFunc = (
  115. cmd: string,
  116. data: any, // all types supported by structuredClone algo
  117. realm?: VMBridgeMode,
  118. node?: Node,
  119. ) => void;
  120. //#endregion Generic
  121. //#region VM-specific
  122. declare type VMBadgeMode = 'unique' | 'total' | ''
  123. declare type VMBadgeData = {
  124. /** Map: frameId -> number of scripts in this frame */
  125. frameIds: { [frameId: string]: number };
  126. icon: string;
  127. /** all ids */
  128. ids: Set<number>;
  129. /**
  130. * undefined = after VM started (unknown injectability),
  131. * null = after tab navigated (unknown injectability),
  132. * false = without scripts,
  133. * true = with some scripts,
  134. * 'SkipScripts' = skip scripts mode,
  135. * 'off' = loaded when isApplied was off
  136. */
  137. inject: boolean | string;
  138. total: number;
  139. unique: number;
  140. }
  141. /**
  142. * Internal script representation
  143. */
  144. declare interface VMScript {
  145. config: VMScript.Config;
  146. custom: VMScript.Custom;
  147. meta: VMScript.Meta;
  148. props: VMScript.Props;
  149. /** Automatically inferred from other props in getData, in-memory only and not in storage */
  150. inferred?: {
  151. homepageURL?: string;
  152. supportURL?: string;
  153. },
  154. }
  155. declare namespace VMScript {
  156. type Config = {
  157. enabled: NumBool;
  158. removed: NumBool;
  159. /** 2 = allow updates and local edits */
  160. shouldUpdate: NumBool | 2;
  161. notifyUpdates?: NumBoolNull;
  162. }
  163. type Custom = {
  164. name?: string;
  165. /** Installation web page that will be used for inferring a missing @homepageURL */
  166. from?: string;
  167. downloadURL?: string;
  168. homepageURL?: string;
  169. lastInstallURL?: string;
  170. updateURL?: string;
  171. icon?: string;
  172. injectInto?: VMScriptInjectInto;
  173. noframes?: NumBoolNull;
  174. exclude?: string[];
  175. excludeMatch?: string[];
  176. include?: string[];
  177. match?: string[];
  178. origExclude: boolean;
  179. origExcludeMatch: boolean;
  180. origInclude: boolean;
  181. origMatch: boolean;
  182. pathMap?: StringMap;
  183. runAt?: VMScriptRunAt;
  184. tags?: string;
  185. }
  186. type Meta = {
  187. description?: string;
  188. downloadURL?: string;
  189. exclude: string[];
  190. excludeMatch: string[];
  191. grant: string[];
  192. homepageURL?: string;
  193. icon?: string;
  194. include: string[];
  195. injectInto?: VMScriptInjectInto;
  196. match: string[];
  197. namespace?: string;
  198. name: string;
  199. noframes?: boolean;
  200. require: string[];
  201. resources: StringMap;
  202. runAt?: VMScriptRunAt;
  203. supportURL?: string;
  204. topLevelAwait?: boolean;
  205. unwrap?: boolean;
  206. version?: string;
  207. }
  208. type Props = {
  209. id: number;
  210. lastModified: number;
  211. lastUpdated: number;
  212. position: number;
  213. uri: string;
  214. uuid: string;
  215. }
  216. }
  217. declare interface VMScriptSourceOptions {
  218. code?: string;
  219. config?: VMScript.Config;
  220. custom?: VMScript.Custom;
  221. meta?: VMScript.Meta;
  222. props?: VMScript.Props;
  223. id?: number;
  224. isNew?: boolean;
  225. position?: number;
  226. cache?: StringMap;
  227. require?: StringMap;
  228. from?: string;
  229. url?: string;
  230. bumpDate?: boolean;
  231. fetchOpts?: object;
  232. message?: string;
  233. portId?: string;
  234. reloadTab?: boolean;
  235. reuseDeps?: boolean;
  236. update?: object;
  237. }
  238. /**
  239. * Injection data sent to the content bridge when injection is disabled
  240. */
  241. declare interface VMInjectionDisabled {
  242. expose: string | false;
  243. }
  244. declare interface VMInjectionFlags {
  245. clipFF?: boolean;
  246. forceContent?: boolean;
  247. }
  248. /**
  249. * Injection data sent to the content bridge when injection is enabled
  250. */
  251. declare interface VMInjection extends VMInjectionDisabled, VMInjectionFlags {
  252. cache: StringMap;
  253. errors: string[];
  254. /** content bridge adds the actually running ids and sends via SetPopup */
  255. ids: number[];
  256. info: VMInjection.Info;
  257. injectInto: VMScriptInjectInto | 'SkipScripts';
  258. /** cache key for envDelayed, which also tells content bridge to expect envDelayed */
  259. more: string;
  260. nonce?: string;
  261. /** `page` mode will be necessary, bit 0: at start/body, bit 1: at end/idle */
  262. page: number;
  263. scripts: VMInjection.Script[];
  264. sessionId: string;
  265. }
  266. /**
  267. * Injection paraphernalia in the background script
  268. */
  269. declare namespace VMInjection {
  270. type RunAt = 'start' | 'body' | 'end' | 'idle';
  271. interface Env {
  272. cache: StringMap;
  273. cacheKeys: string[];
  274. code: StringMap;
  275. /** Dependencies by key to script ids */
  276. depsMap: { [url: string]: number[] };
  277. ids: number[];
  278. reqKeys: string[];
  279. require: StringMap;
  280. runAt: { [id: string]: RunAt };
  281. scripts: VMScript[];
  282. value: { [scriptId: string]: StringMap };
  283. valueIds: number[];
  284. }
  285. interface EnvStart extends Env, VMInjectionFlags {
  286. allIds: { [id: string]: NumBool };
  287. more: EnvDelayed;
  288. /** `null` = env was processed and contains data now */
  289. promise: Promise<EnvStart>;
  290. }
  291. interface EnvDelayed extends Env {
  292. /** cache key for Bag */
  293. more: string;
  294. /** `null` = env was processed and contains data now */
  295. promise: Promise<EnvDelayed>;
  296. }
  297. /**
  298. * Contains the injected data and non-injected auxiliaries
  299. */
  300. interface Bag {
  301. csReg?: Promise<browser.contentScripts.RegisteredContentScript>;
  302. forceContent?: boolean;
  303. inject: VMInjection;
  304. more: EnvDelayed;
  305. }
  306. interface Info {
  307. gmi: {
  308. isIncognito: boolean;
  309. };
  310. ua: VMScriptGMInfoPlatform;
  311. uad?: true;
  312. }
  313. /**
  314. * Script prepared for injection
  315. */
  316. interface Script {
  317. displayName: string;
  318. /** -1 ID_BAD_REALM if the desired realm is PAGE which is not injectable */
  319. code: string | -1;
  320. /** Omitted props are added in makeGmApiWrapper */
  321. gmi: Omit<VMScriptGMInfoObject, 'injectInto' | 'resources' | 'script' | 'scriptMetaStr'>;
  322. id: number;
  323. injectInto: VMScriptInjectInto;
  324. key: { data: string, win: string };
  325. /** `resources` is still an object, converted later in makeGmApiWrapper */
  326. meta: VMScript.Meta | VMScriptGMInfoScriptMeta;
  327. metaStr: (string|number)[];
  328. pathMap: StringMap;
  329. runAt?: RunAt;
  330. values?: StringMap;
  331. }
  332. }
  333. declare interface VMRealmData {
  334. lists: {
  335. start: VMScript[];
  336. body: VMScript[];
  337. end: VMScript[];
  338. idle: VMScript[];
  339. }
  340. is: boolean;
  341. info: VMInjection.Info;
  342. }
  343. /**
  344. * Internal request()
  345. */
  346. declare namespace VMReq {
  347. interface Options extends RequestInit {
  348. /** @implements XMLHttpRequestResponseType */
  349. responseType?: '' | 'arraybuffer' | 'blob' | 'json' | 'text';
  350. }
  351. interface OptionsMulti extends Options {
  352. /** truthy = multi script update, 'auto' = autoUpdate, falsy = single */
  353. multi?: boolean | 'auto';
  354. }
  355. interface Response {
  356. url: string;
  357. status: number;
  358. headers: Headers;
  359. data: string | ArrayBuffer | Blob | PlainJSONValue;
  360. }
  361. }
  362. declare type VMSearchOptions = {
  363. reversed?: boolean;
  364. wrapAround?: boolean;
  365. reuseCursor?: boolean;
  366. pos?: { line: number, ch: number };
  367. }
  368. /** Throws on error */
  369. declare type VMStorageFetch = (
  370. url: string,
  371. /** 'res' makes the function resolve with the result */
  372. options?: VMReq.Options | 'res',
  373. ) => Promise<void>
  374. /** Augmented by handleCommandMessage in messages from the content script */
  375. declare interface VMMessageSender extends chrome.runtime.MessageSender {
  376. top?: VMTopRenderMode;
  377. }
  378. declare type VMMessageTargetFrame = { frameId?: number } | { documentId?: string }
  379. /**
  380. * 0 = frame
  381. * 1 = top page
  382. * 2 = pre-rendered top page invisible
  383. * 3 = pre-rendered top-page reified (the mode is set temporarily just to notify bg)
  384. * 4 = pre-rendered top-page post-reification
  385. */
  386. declare type VMTopRenderMode = 0 | 1 | 2 | 3 | 4;
  387. //#endregion Generic