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