github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/templates/query.go (about) 1 package templates 2 3 import ( 4 "fmt" 5 6 "github.com/mafredri/cdp/protocol/runtime" 7 8 "github.com/MontFerret/ferret/pkg/drivers" 9 "github.com/MontFerret/ferret/pkg/drivers/cdp/eval" 10 ) 11 12 const ( 13 queryCSSSelectorFragment = "const found = el.querySelector(selector);" 14 15 queryCSSSelectorAllFragment = "const found = el.querySelectorAll(selector);" 16 ) 17 18 var ( 19 queryCSSSelector = ` 20 (el, selector) => { 21 const found = el.querySelector(selector); 22 23 return found; 24 } 25 ` 26 queryXPathSelector = fmt.Sprintf(` 27 (el, selector) => { 28 %s 29 30 return found; 31 } 32 `, 33 xpathAsElementFragment, 34 ) 35 ) 36 37 func QuerySelector(id runtime.RemoteObjectID, selector drivers.QuerySelector) *eval.Function { 38 return toFunction(selector, queryCSSSelector, queryXPathSelector). 39 WithArgRef(id). 40 WithArgSelector(selector) 41 } 42 43 const queryCSSSelectorAll = `(el, selector) => { 44 return el.querySelectorAll(selector); 45 }` 46 47 var queryXPathSelectorAll = fmt.Sprintf(`(el, selector) => { 48 %s 49 50 return found; 51 }`, xpathAsElementArrayFragment) 52 53 func QuerySelectorAll(id runtime.RemoteObjectID, selector drivers.QuerySelector) *eval.Function { 54 return toFunction(selector, queryCSSSelectorAll, queryXPathSelectorAll). 55 WithArgRef(id). 56 WithArgSelector(selector) 57 } 58 59 const existsByCSSSelector = ` 60 (el, selector) => { 61 const found = el.querySelector(selector); 62 63 return found != null; 64 } 65 ` 66 67 var existsByXPathSelector = fmt.Sprintf(` 68 (el, selector) => { 69 %s 70 71 return found != null; 72 } 73 `, xpathAsElementFragment) 74 75 func ExistsBySelector(id runtime.RemoteObjectID, selector drivers.QuerySelector) *eval.Function { 76 return toFunction(selector, existsByCSSSelector, existsByXPathSelector). 77 WithArgRef(id). 78 WithArgSelector(selector) 79 } 80 81 const countByCSSSelector = ` 82 (el, selector) => { 83 const found = el.querySelectorAll(selector); 84 85 return found.length; 86 } 87 ` 88 89 var countByXPathSelector = fmt.Sprintf(` 90 (el, selector) => { 91 %s 92 93 return found.length; 94 } 95 `, xpathAsElementArrayFragment) 96 97 func CountBySelector(id runtime.RemoteObjectID, selector drivers.QuerySelector) *eval.Function { 98 return toFunction(selector, countByCSSSelector, countByXPathSelector). 99 WithArgRef(id). 100 WithArgSelector(selector) 101 }