Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/addInterceptParameters.js
2884 views
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
18
const { UrlPattern } = require('./urlPattern')
19
20
class AddInterceptParameters {
21
#phases = []
22
#urlPatterns = []
23
24
constructor(phases) {
25
if (phases instanceof Array) {
26
phases.forEach((phase) => this.#phases.push(phase))
27
} else {
28
this.#phases.push(phases)
29
}
30
}
31
32
/**
33
* Adds a URL pattern to intercept.
34
*
35
* @param {UrlPattern} pattern - The URL pattern to add.
36
* @returns {AddInterceptParameters} - Returns the current instance of the class AddInterceptParameters for chaining.
37
* @throws {Error} - Throws an error if the pattern is not an instance of UrlPattern.
38
*/
39
urlPattern(pattern) {
40
if (!(pattern instanceof UrlPattern)) {
41
throw new Error(`Pattern must be an instance of UrlPattern. Received: '${pattern})'`)
42
}
43
this.#urlPatterns.push(Object.fromEntries(pattern.asMap()))
44
return this
45
}
46
47
/**
48
* Adds array of URL patterns to intercept.
49
*
50
* @param {UrlPattern[]} patterns - An array of UrlPattern instances representing the URL patterns to intercept.
51
* @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining.
52
* @throws {Error} - Throws an error if the pattern is not an instance of UrlPattern.
53
*/
54
urlPatterns(patterns) {
55
patterns.forEach((pattern) => {
56
if (!(pattern instanceof UrlPattern)) {
57
throw new Error(`Pattern must be an instance of UrlPattern. Received:'${pattern}'`)
58
}
59
this.#urlPatterns.push(Object.fromEntries(pattern.asMap()))
60
})
61
return this
62
}
63
64
/**
65
* Adds string URL to intercept.
66
*
67
* @param {string} pattern - The URL pattern to be added.
68
* @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining..
69
* @throws {Error} - If the pattern is not an instance of String.
70
*/
71
urlStringPattern(pattern) {
72
if (typeof pattern !== 'string') {
73
throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)
74
}
75
76
this.#urlPatterns.push({ type: 'string', pattern: pattern })
77
return this
78
}
79
80
/**
81
* Adds array of string URLs to intercept.
82
* @param {string[]} patterns - An array of URL string patterns.
83
* @returns {this} - Returns the instance of AddInterceptParameters for chaining.
84
*/
85
urlStringPatterns(patterns) {
86
patterns.forEach((pattern) => {
87
if (typeof pattern !== 'string') {
88
throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)
89
}
90
this.#urlPatterns.push({ type: 'string', pattern: pattern })
91
})
92
return this
93
}
94
95
asMap() {
96
const map = new Map()
97
map.set('phases', this.#phases)
98
if (this.#urlPatterns.length > 0) {
99
map.set('urlPatterns', this.#urlPatterns)
100
}
101
102
return map
103
}
104
}
105
106
module.exports = { AddInterceptParameters }
107
108