cdp.d.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Copyright 2023 Google LLC.
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import type { Protocol } from 'devtools-protocol';
  18. import type { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping.js';
  19. import type { BrowsingContext, JsUint, Script } from './generated/webdriver-bidi.js';
  20. export type EventNames = Event['method'];
  21. export type Message = CommandResponse | Event;
  22. export type Command = {
  23. id: JsUint;
  24. } & CommandData;
  25. export type CommandData = SendCommandCommand | GetSessionCommand | ResolveRealmCommand;
  26. export type CommandResponse = {
  27. type: 'success';
  28. id: JsUint;
  29. result: ResultData;
  30. };
  31. export type ResultData = SendCommandResult | GetSessionResult | ResolveRealmResult;
  32. export type SendCommandCommand = {
  33. method: 'cdp.sendCommand';
  34. params: SendCommandParameters;
  35. };
  36. export type SendCommandParameters<Command extends keyof ProtocolMapping.Commands = keyof ProtocolMapping.Commands> = {
  37. method: Command;
  38. params?: ProtocolMapping.Commands[Command]['paramsType'][0];
  39. session?: Protocol.Target.SessionID;
  40. };
  41. export type SendCommandResult = {
  42. result: ProtocolMapping.Commands[keyof ProtocolMapping.Commands]['returnType'];
  43. session?: Protocol.Target.SessionID;
  44. };
  45. export type GetSessionCommand = {
  46. method: 'cdp.getSession';
  47. params: GetSessionParameters;
  48. };
  49. export type GetSessionParameters = {
  50. context: BrowsingContext.BrowsingContext;
  51. };
  52. export type GetSessionResult = {
  53. session?: Protocol.Target.SessionID;
  54. };
  55. export type ResolveRealmCommand = {
  56. method: 'cdp.resolveRealm';
  57. params: ResolveRealmParameters;
  58. };
  59. export type ResolveRealmParameters = {
  60. realm: Script.Realm;
  61. };
  62. export type ResolveRealmResult = {
  63. executionContextId: Protocol.Runtime.ExecutionContextId;
  64. };
  65. export type Event = {
  66. type: 'event';
  67. } & EventData;
  68. export type EventData = EventDataFor<keyof ProtocolMapping.Events>;
  69. export type EventDataFor<EventName extends keyof ProtocolMapping.Events> = {
  70. method: `cdp.${EventName}`;
  71. params: EventParametersFor<EventName>;
  72. };
  73. export type EventParametersFor<EventName extends keyof ProtocolMapping.Events> = {
  74. event: EventName;
  75. params: ProtocolMapping.Events[EventName][0];
  76. session: Protocol.Target.SessionID;
  77. };