webExtension.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. const ExtensionData = require('./extensionData')
  18. /**
  19. * Represents the commands and events under Extension Module.
  20. * Described in https://w3c.github.io/webdriver-bidi/#module-webExtension
  21. */
  22. class WebExtension {
  23. constructor(driver) {
  24. this._driver = driver
  25. }
  26. async init() {
  27. if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {
  28. throw Error('WebDriver instance must support BiDi protocol')
  29. }
  30. this.bidi = await this._driver.getBidi()
  31. }
  32. /**
  33. * Install a browser webExtension.
  34. *
  35. * @param {ExtensionData} extensionData
  36. * An instance of ExtensionData containing the webExtension’s path, archive path or base64 encoded path.
  37. * @returns {Promise<string>}
  38. * The installed webExtension’s ID.
  39. * @throws {Error} If extensionData is not an ExtensionData instance.
  40. */
  41. async install(extensionData) {
  42. if (!(extensionData instanceof ExtensionData)) {
  43. throw new Error('install() requires an ExtensionData instance')
  44. }
  45. const command = {
  46. method: 'webExtension.install',
  47. params: {
  48. extensionData: extensionData.asMap(),
  49. },
  50. }
  51. // 在 webExtension.js:58 行后添加
  52. let response = await this.bidi.send(command)
  53. console.log('=== BiDi Response ===')
  54. console.log('Type:', response.type)
  55. console.log('Result:', response.result)
  56. console.log('Full:', JSON.stringify(response, null, 2))
  57. return response.result.extension
  58. }
  59. /**
  60. * Uninstall a browser webExtension by ID.
  61. *
  62. * @param {string} id
  63. * The webExtension ID.
  64. * @returns {Promise<void>}
  65. * @throws {Error} If the uninstall command returns an error from the browser.
  66. */
  67. async uninstall(id) {
  68. const command = {
  69. method: 'webExtension.uninstall',
  70. params: {
  71. extension: id,
  72. },
  73. }
  74. const response = await this.bidi.send(command)
  75. if (response.type === 'error') {
  76. throw new Error(`${response.error}: ${response.message}`)
  77. }
  78. }
  79. }
  80. /**
  81. * Helper to create and initialize an Extension instance.
  82. *
  83. * @param {import('selenium-webdriver').WebDriver} driver
  84. * A Selenium WebDriver instance.
  85. * @returns {Promise<WebExtension>}
  86. * An Extension instance.
  87. */
  88. async function getWebExtensionInstance(driver) {
  89. let instance = new WebExtension(driver)
  90. await instance.init()
  91. return instance
  92. }
  93. module.exports = getWebExtensionInstance