Path: blob/trunk/javascript/selenium-webdriver/bidi/addInterceptParameters.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617const { UrlPattern } = require('./urlPattern')1819class AddInterceptParameters {20#phases = []21#urlPatterns = []2223constructor(phases) {24if (phases instanceof Array) {25phases.forEach((phase) => this.#phases.push(phase))26} else {27this.#phases.push(phases)28}29}3031/**32* Adds a URL pattern to intercept.33*34* @param {UrlPattern} pattern - The URL pattern to add.35* @returns {AddInterceptParameters} - Returns the current instance of the class AddInterceptParameters for chaining.36* @throws {Error} - Throws an error if the pattern is not an instance of UrlPattern.37*/38urlPattern(pattern) {39if (!(pattern instanceof UrlPattern)) {40throw new Error(`Pattern must be an instance of UrlPattern. Received: '${pattern})'`)41}42this.#urlPatterns.push(Object.fromEntries(pattern.asMap()))43return this44}4546/**47* Adds array of URL patterns to intercept.48*49* @param {UrlPattern[]} patterns - An array of UrlPattern instances representing the URL patterns to intercept.50* @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining.51* @throws {Error} - Throws an error if the pattern is not an instance of UrlPattern.52*/53urlPatterns(patterns) {54patterns.forEach((pattern) => {55if (!(pattern instanceof UrlPattern)) {56throw new Error(`Pattern must be an instance of UrlPattern. Received:'${pattern}'`)57}58this.#urlPatterns.push(Object.fromEntries(pattern.asMap()))59})60return this61}6263/**64* Adds string URL to intercept.65*66* @param {string} pattern - The URL pattern to be added.67* @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining..68* @throws {Error} - If the pattern is not an instance of String.69*/70urlStringPattern(pattern) {71if (typeof pattern !== 'string') {72throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)73}7475this.#urlPatterns.push({ type: 'string', pattern: pattern })76return this77}7879/**80* Adds array of string URLs to intercept.81* @param {string[]} patterns - An array of URL string patterns.82* @returns {this} - Returns the instance of AddInterceptParameters for chaining.83*/84urlStringPatterns(patterns) {85patterns.forEach((pattern) => {86if (typeof pattern !== 'string') {87throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)88}89this.#urlPatterns.push({ type: 'string', pattern: pattern })90})91return this92}9394asMap() {95const map = new Map()96map.set('phases', this.#phases)97if (this.#urlPatterns.length > 0) {98map.set('urlPatterns', this.#urlPatterns)99}100101return map102}103}104105module.exports = { AddInterceptParameters }106107108