types.d.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. script: VMScript;
  19. }
  20. /**
  21. * GM_xmlhttpRequest paraphernalia
  22. */
  23. declare namespace GMReq {
  24. type EventType = keyof XMLHttpRequestEventMap;
  25. type UserOpts = VMScriptGMDownloadOptions | VMScriptGMXHRDetails;
  26. interface BG {
  27. anonymous: boolean;
  28. cb: (data: GMReq.Message.BGAny) => Promise<void>;
  29. coreId: number;
  30. frameId: number;
  31. id: string;
  32. noNativeCookie: boolean;
  33. responseHeaders: string;
  34. storeId: string;
  35. tabId: number;
  36. url: string;
  37. xhr: XMLHttpRequest;
  38. }
  39. interface Content {
  40. arr?: Uint8Array;
  41. asBlob: boolean;
  42. fileName: string;
  43. realm: VMScriptInjectInto;
  44. }
  45. interface Web {
  46. id: string;
  47. scriptId: number;
  48. cb: { [name: EventType]: typeof VMScriptGMXHRDetails.onload };
  49. context?: any;
  50. raw?: string | Blob | ArrayBuffer;
  51. response?: string | Blob | ArrayBuffer;
  52. responseHeaders?: string;
  53. responseText?: string;
  54. responseType?: XMLHttpRequestResponseType;
  55. }
  56. namespace Message {
  57. /** From background */
  58. type BGAny = BG | BGChunk | BGError;
  59. interface BG {
  60. blobbed: boolean;
  61. chunked: boolean;
  62. contentType: string;
  63. data: VMScriptResponseObject;
  64. id: string;
  65. type: EventType;
  66. }
  67. interface BGChunk {
  68. id: string;
  69. chunk: number;
  70. data: string;
  71. size: number;
  72. }
  73. interface BGError {
  74. id: string;
  75. type: 'error';
  76. data: null; // helps avoid the need for hasOwnProperty in HttpRequested
  77. error: string;
  78. }
  79. /** From web/content bridge */
  80. interface Web {
  81. id: string;
  82. scriptId: number;
  83. anonymous: boolean;
  84. fileName: string;
  85. data: any[];
  86. events: EventType[];
  87. headers?: StringMap;
  88. method?: string;
  89. overrideMimeType?: string;
  90. password?: string;
  91. timeout?: number;
  92. url: string;
  93. user?: string;
  94. xhrType: XMLHttpRequestResponseType;
  95. }
  96. }
  97. }
  98. declare type VMBridgeMode = Exclude<VMScriptInjectInto, 'auto'>;
  99. declare type VMBridgeContentIds = {
  100. /** -1 = bad realm, 0 = disabled, 1 = enabled, 2 = starting, context name = running */
  101. [id: string]: -1 | 0 | 1 | 2 | VMBridgeMode;
  102. }
  103. declare type VMBridgePostFunc = (
  104. cmd: string,
  105. data: any, // all types supported by structuredClone algo
  106. realm?: VMBridgeMode,
  107. node?: Node,
  108. ) => void;
  109. //#endregion Generic
  110. //#region VM-specific
  111. declare type VMBadgeMode = 'unique' | 'total' | ''
  112. /**
  113. * Internal script representation
  114. */
  115. declare interface VMScript {
  116. config: VMScript.Config;
  117. custom: VMScript.Custom;
  118. meta: VMScript.Meta;
  119. props: VMScript.Props;
  120. /** Automatically inferred from other props in getData, in-memory only and not in storage */
  121. inferred?: {
  122. homepageURL?: string;
  123. supportURL?: string;
  124. },
  125. }
  126. declare namespace VMScript {
  127. type Config = {
  128. enabled: NumBool;
  129. removed: NumBool;
  130. shouldUpdate: NumBool;
  131. notifyUpdates?: NumBoolNull;
  132. }
  133. type Custom = {
  134. name?: string;
  135. downloadURL?: string;
  136. homepageURL?: string;
  137. lastInstallURL?: string;
  138. updateURL?: string;
  139. injectInto?: VMScriptInjectInto;
  140. noframes?: NumBoolNull;
  141. exclude?: string[];
  142. excludeMatch?: string[];
  143. include?: string[];
  144. match?: string[];
  145. origExclude: boolean;
  146. origExcludeMatch: boolean;
  147. origInclude: boolean;
  148. origMatch: boolean;
  149. pathMap?: StringMap;
  150. runAt?: VMScriptRunAt;
  151. }
  152. type Meta = {
  153. description?: string;
  154. downloadURL?: string;
  155. exclude: string[];
  156. excludeMatch: string[];
  157. grant: string[];
  158. homepageURL?: string;
  159. icon?: string;
  160. include: string[];
  161. injectInto?: VMScriptInjectInto;
  162. match: string[];
  163. namespace?: string;
  164. name: string;
  165. noframes?: boolean;
  166. require: string[];
  167. resources: StringMap;
  168. runAt?: VMScriptRunAt;
  169. supportURL?: string;
  170. unwrap?: boolean;
  171. version?: string;
  172. }
  173. type Props = {
  174. id: number;
  175. lastModified: number;
  176. lastUpdated: number;
  177. position: number;
  178. uri: string;
  179. uuid: string;
  180. }
  181. }
  182. /**
  183. * Injection data sent to the content bridge when injection is disabled
  184. */
  185. declare interface VMInjectionDisabled {
  186. expose: string | false;
  187. }
  188. /**
  189. * Injection data sent to the content bridge when injection is enabled
  190. */
  191. declare interface VMInjection extends VMInjectionDisabled {
  192. cache: StringMap;
  193. errors: string[];
  194. forceContent?: boolean;
  195. /** content bridge adds the actually running ids and sends via SetPopup */
  196. ids: number[];
  197. info: VMInjection.Info;
  198. injectInto: VMScriptInjectInto;
  199. /** cache key for envDelayed, which also tells content bridge to expect envDelayed */
  200. more: string;
  201. /** `page` mode will be necessary */
  202. page: boolean;
  203. scripts: VMInjection.Script[];
  204. }
  205. /**
  206. * Injection paraphernalia in the background script
  207. */
  208. declare namespace VMInjection {
  209. type RunAt = 'start' | 'body' | 'end' | 'idle';
  210. interface Env {
  211. /** Only present in envStart */
  212. allIds?: { [id: string]: NumBool };
  213. cache: StringMap;
  214. cacheKeys: string[];
  215. code: StringMap;
  216. /** Dependencies by key to script ids */
  217. depsMap: { [url: string]: number[] };
  218. forceContent?: boolean;
  219. ids: number[];
  220. /** Only present in envStart */
  221. more?: Env;
  222. promise: Promise<Env>;
  223. reqKeys: string[];
  224. require: StringMap;
  225. runAt: { [id: string]: RunAt };
  226. scripts: VMScript[];
  227. sizing?: boolean;
  228. value: { [scriptId: string]: StringMap };
  229. valueIds: number[];
  230. }
  231. /**
  232. * Contains the injected data and non-injected auxiliaries
  233. */
  234. interface Bag {
  235. csar: Promise<browser.contentScripts.RegisteredContentScript>;
  236. forceContent?: boolean;
  237. inject: VMInjection;
  238. more: Env;
  239. }
  240. interface Info {
  241. ua: VMScriptGMInfoPlatform;
  242. }
  243. /**
  244. * Script prepared for injection
  245. */
  246. interface Script {
  247. displayName: string;
  248. /** -1 ID_BAD_REALM if the desired realm is PAGE which is not injectable */
  249. code: string | -1;
  250. /** Omitted props are added in makeGmApiWrapper */
  251. gmi: Omit<VMScriptGMInfoObject, 'injectInto' | 'resources' | 'script' | 'scriptMetaStr'>;
  252. id: number;
  253. injectInto: VMScriptInjectInto;
  254. key: { data: string, win: string };
  255. /** `resources` is still an object, converted later in makeGmApiWrapper */
  256. meta: VMScript.Meta | VMScriptGMInfoScriptMeta;
  257. metaStr: (string|number)[];
  258. pathMap: StringMap;
  259. runAt?: RunAt;
  260. val?: StringMap;
  261. }
  262. }
  263. declare interface VMRealmData {
  264. lists: {
  265. start: VMScript[];
  266. body: VMScript[];
  267. end: VMScript[];
  268. idle: VMScript[];
  269. }
  270. is: boolean;
  271. info: VMInjection.Info;
  272. }
  273. /**
  274. * Internal request()
  275. */
  276. declare namespace VMReq {
  277. interface Options extends RequestInit {
  278. /** @implements XMLHttpRequestResponseType */
  279. responseType: '' | 'arraybuffer' | 'blob' | 'json' | 'text';
  280. }
  281. interface Response {
  282. url: string;
  283. status: number;
  284. headers: Headers;
  285. data: string | ArrayBuffer | Blob | PlainJSONValue;
  286. }
  287. }
  288. declare type VMSearchOptions = {
  289. reversed?: boolean;
  290. wrapAround?: chrome.tabs.Tab;
  291. reuseCursor?: boolean;
  292. pos?: { line: number, ch: number };
  293. }
  294. /** Throws on error */
  295. declare type VMStorageFetch = (
  296. url: string,
  297. options?: VMReq.Options,
  298. check?: (...args) => void // throws on error
  299. ) => Promise<void>
  300. declare interface VMUserAgent extends VMScriptGMInfoPlatform {
  301. /** Chrome/ium version number */
  302. chrome: number | typeof NaN;
  303. /** derived from UA string initially, a real number when `ready` */
  304. firefox: number | typeof NaN;
  305. /** resolves when `browser` API returns real versions */
  306. ready: Promise<void>;
  307. }
  308. //#endregion Generic