forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
214 lines (184 loc) · 9.38 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// generated by "lib/js/generate"
package js
// Element ...
var Element = &Function{
Name: "element",
Definition: `function(e){const t=functions.selectable(this);return t.querySelector(e)}`,
Dependencies: []*Function{Selectable},
}
// Elements ...
var Elements = &Function{
Name: "elements",
Definition: `function(e){return functions.selectable(this).querySelectorAll(e)}`,
Dependencies: []*Function{Selectable},
}
// ElementX ...
var ElementX = &Function{
Name: "elementX",
Definition: `function(e){var t=functions.selectable(this);return document.evaluate(e,t,null,XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue}`,
Dependencies: []*Function{Selectable},
}
// ElementsX ...
var ElementsX = &Function{
Name: "elementsX",
Definition: `function(e){var t,n=functions.selectable(this);const i=document.evaluate(e,n,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE),s=[];for(;t=i.iterateNext();)s.push(t);return s}`,
Dependencies: []*Function{Selectable},
}
// ElementR ...
var ElementR = &Function{
Name: "elementR",
Definition: `function(e,t){var n=t.match(/(\/?)(.+)\1([a-z]*)/i),i=n[3]&&!/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(n[3])?new RegExp(t):new RegExp(n[2],n[3]);const s=functions.selectable(this);e=Array.from(s.querySelectorAll(e)).find(e=>i.test(functions.text.call(e)));return e||null}`,
Dependencies: []*Function{Selectable, Text},
}
// Parents ...
var Parents = &Function{
Name: "parents",
Definition: `function(e){let t=this.parentElement;const n=[];for(;t;)t.matches(e)&&n.push(t),t=t.parentElement;return n}`,
Dependencies: []*Function{},
}
// ContainsElement ...
var ContainsElement = &Function{
Name: "containsElement",
Definition: `function(e){for(var t=e;null!=t;){if(t===this)return!0;t=t.parentElement}return!1}`,
Dependencies: []*Function{},
}
// InitMouseTracer ...
var InitMouseTracer = &Function{
Name: "initMouseTracer",
Definition: `async function(e,t){if(await functions.waitLoad(),!document.getElementById(e)){const n=document.createElement("div");n.innerHTML=t;const i=n.lastChild;i.id=e,i.style="position: absolute; z-index: 2147483647; width: 17px; pointer-events: none;",i.removeAttribute("width"),i.removeAttribute("height"),document.body.parentElement.appendChild(i)}}`,
Dependencies: []*Function{WaitLoad},
}
// UpdateMouseTracer ...
var UpdateMouseTracer = &Function{
Name: "updateMouseTracer",
Definition: `function(e,t,n){const i=document.getElementById(e);return!!i&&(i.style.left=t-2+"px",i.style.top=n-3+"px",!0)}`,
Dependencies: []*Function{},
}
// Rect ...
var Rect = &Function{
Name: "rect",
Definition: `function(){var e=functions.tag(this).getBoundingClientRect();return{x:e.x,y:e.y,width:e.width,height:e.height}}`,
Dependencies: []*Function{Tag},
}
// Overlay ...
var Overlay = &Function{
Name: "overlay",
Definition: `async function(e,t,n,i,s,r){await functions.waitLoad();const o=document.createElement("div");if(o.id=e,o.style=` + "`" + `position: fixed; z-index:2147483647; border: 2px dashed red;
border-radius: 3px; box-shadow: #5f3232 0 0 3px; pointer-events: none;
box-sizing: border-box;
left: ${t}px;
top: ${n}px;
height: ${s}px;
width: ${i}px;` + "`" + `,i*s==0&&(o.style.border="none"),r){const l=document.createElement("div");l.style=` + "`" + `position: absolute; color: #cc26d6; font-size: 12px; background: #ffffffeb;
box-shadow: #333 0 0 3px; padding: 2px 5px; border-radius: 3px; white-space: nowrap;
top: ${s}px;` + "`" + `,l.innerHTML=r,o.appendChild(l),document.body.parentElement.appendChild(o),window.innerHeight<l.offsetHeight+n+s&&(l.style.top=-l.offsetHeight-2+"px"),window.innerWidth<l.offsetWidth+t&&(l.style.left=window.innerWidth-l.offsetWidth-t+"px")}else document.body.parentElement.appendChild(o)}`,
Dependencies: []*Function{WaitLoad},
}
// ElementOverlay ...
var ElementOverlay = &Function{
Name: "elementOverlay",
Definition: `async function(n,e){const i=100,s=functions.tag(this);let r=s.getBoundingClientRect();await functions.overlay(n,r.left,r.top,r.width,r.height,e);const o=()=>{const e=document.getElementById(n);var t;null!==e&&(t=s.getBoundingClientRect(),r.left===t.left&&r.top===t.top&&r.width===t.width&&r.height===t.height||(e.style.left=t.left+"px",e.style.top=t.top+"px",e.style.width=t.width+"px",e.style.height=t.height+"px",r=t),setTimeout(o,i))};setTimeout(o,i)}`,
Dependencies: []*Function{Tag, Overlay},
}
// RemoveOverlay ...
var RemoveOverlay = &Function{
Name: "removeOverlay",
Definition: `function(e){e=document.getElementById(e);e&&Element.prototype.remove.call(e)}`,
Dependencies: []*Function{},
}
// WaitIdle ...
var WaitIdle = &Function{
Name: "waitIdle",
Definition: `function(t){return new Promise(e=>{window.requestIdleCallback(e,{timeout:t})})}`,
Dependencies: []*Function{},
}
// WaitLoad ...
var WaitLoad = &Function{
Name: "waitLoad",
Definition: `function(){const n=this===window;return new Promise((e,t)=>{if(n){if("complete"===document.readyState)return e();window.addEventListener("load",e)}else void 0===this.complete||this.complete?e():(this.addEventListener("load",e),this.addEventListener("error",t))})}`,
Dependencies: []*Function{},
}
// InputEvent ...
var InputEvent = &Function{
Name: "inputEvent",
Definition: `function(){this.dispatchEvent(new Event("input",{bubbles:!0})),this.dispatchEvent(new Event("change",{bubbles:!0}))}`,
Dependencies: []*Function{},
}
// InputTime ...
var InputTime = &Function{
Name: "inputTime",
Definition: `function(e){const t=new Date(e);var e=e=>e.toString().padStart(2,"0"),n=t.getFullYear(),i=e(t.getMonth()+1),s=e(t.getDate()),r=e(t.getHours()),o=e(t.getMinutes());switch(this.type){case"date":this.value=n+` + "`" + `-${i}-` + "`" + `+s;break;case"datetime-local":this.value=n+` + "`" + `-${i}-${s}T${r}:` + "`" + `+o;break;case"month":this.value=i;break;case"time":this.value=r+":"+o}functions.inputEvent.call(this)}`,
Dependencies: []*Function{InputEvent},
}
// SelectText ...
var SelectText = &Function{
Name: "selectText",
Definition: `function(e){e=this.value.match(new RegExp(e));e&&this.setSelectionRange(e.index,e.index+e[0].length)}`,
Dependencies: []*Function{},
}
// SelectAllText ...
var SelectAllText = &Function{
Name: "selectAllText",
Definition: `function(){this.select()}`,
Dependencies: []*Function{},
}
// Select ...
var Select = &Function{
Name: "select",
Definition: `function(e,n,t){let i;switch(t){case"regex":i=e.map(e=>{const t=new RegExp(e);return e=>t.test(e.innerText)});break;case"css-selector":i=e.map(t=>e=>e.matches(t));break;default:i=e.map(t=>e=>e.innerText.includes(t))}const s=Array.from(this.options);let r=!1;return i.forEach(e=>{const t=s.find(e);t&&(t.selected=n,r=!0)}),this.dispatchEvent(new Event("input",{bubbles:!0})),this.dispatchEvent(new Event("change",{bubbles:!0})),r}`,
Dependencies: []*Function{},
}
// Visible ...
var Visible = &Function{
Name: "visible",
Definition: `function(){const e=functions.tag(this);var t=e.getBoundingClientRect(),n=window.getComputedStyle(e);return"none"!==n.display&&"hidden"!==n.visibility&&!!(t.top||t.bottom||t.width||t.height)}`,
Dependencies: []*Function{Tag},
}
// Invisible ...
var Invisible = &Function{
Name: "invisible",
Definition: `function(){return!functions.visible.apply(this)}`,
Dependencies: []*Function{Visible},
}
// Text ...
var Text = &Function{
Name: "text",
Definition: `function(){switch(this.tagName){case"INPUT":case"TEXTAREA":return this.value||this.placeholder;case"SELECT":return Array.from(this.selectedOptions).map(e=>e.innerText).join();case void 0:return this.textContent;default:return this.innerText}}`,
Dependencies: []*Function{},
}
// Resource ...
var Resource = &Function{
Name: "resource",
Definition: `function(){return new Promise((e,t)=>{if(this.complete)return e(this.currentSrc);this.addEventListener("load",()=>e(this.currentSrc)),this.addEventListener("error",e=>t(e))})}`,
Dependencies: []*Function{},
}
// AddScriptTag ...
var AddScriptTag = &Function{
Name: "addScriptTag",
Definition: `function(i,s,r){if(!document.getElementById(i))return new Promise((e,t)=>{var n=document.createElement("script");s?(n.src=s,n.onload=e):(n.type="text/javascript",n.text=r,e()),n.id=i,n.onerror=t,document.head.appendChild(n)})}`,
Dependencies: []*Function{},
}
// AddStyleTag ...
var AddStyleTag = &Function{
Name: "addStyleTag",
Definition: `function(i,s,r){if(!document.getElementById(i))return new Promise((e,t)=>{var n;s?((n=document.createElement("link")).rel="stylesheet",n.href=s):((n=document.createElement("style")).type="text/css",n.appendChild(document.createTextNode(r)),e()),n.id=i,n.onload=e,n.onerror=t,document.head.appendChild(n)})}`,
Dependencies: []*Function{},
}
// Selectable ...
var Selectable = &Function{
Name: "selectable",
Definition: `function(e){return e.querySelector?e:document}`,
Dependencies: []*Function{},
}
// Tag ...
var Tag = &Function{
Name: "tag",
Definition: `function(e){return e.tagName?e:e.parentElement}`,
Dependencies: []*Function{},
}
// ExposeFunc ...
var ExposeFunc = &Function{
Name: "exposeFunc",
Definition: `function(e,t){let r=0;window[e]=e=>new Promise((n,i)=>{const s=t+"_cb"+r++;window[s]=(e,t)=>{delete window[s],t?i(t):n(e)},window[t](JSON.stringify({req:e,cb:s}))})}`,
Dependencies: []*Function{},
}