github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/templates/inner_text.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 "github.com/MontFerret/ferret/pkg/runtime/values" 11 ) 12 13 const setInnerText = `(el, value) => { 14 el.innerText = value; 15 }` 16 17 func SetInnerText(id runtime.RemoteObjectID, value values.String) *eval.Function { 18 return eval.F(setInnerText).WithArgRef(id).WithArgValue(value) 19 } 20 21 const getInnerText = `(el) => { 22 if (el.nodeType !== 9) { 23 return el.innerText; 24 } 25 26 return document.documentElement.innerText; 27 }` 28 29 func GetInnerText(id runtime.RemoteObjectID) *eval.Function { 30 return eval.F(getInnerText).WithArgRef(id) 31 } 32 33 var ( 34 setInnerTextByCSSSelector = fmt.Sprintf(` 35 (el, selector, value) => { 36 const found = el.querySelector(selector); 37 38 %s 39 40 found.innerText = value; 41 }`, notFoundErrorFragment) 42 43 setInnerTextByXPathSelector = fmt.Sprintf(` 44 (el, selector, value) => { 45 %s 46 47 %s 48 49 found.innerText = value; 50 }`, xpathAsElementFragment, notFoundErrorFragment) 51 ) 52 53 func SetInnerTextBySelector(id runtime.RemoteObjectID, selector drivers.QuerySelector, value values.String) *eval.Function { 54 return toFunction(selector, setInnerTextByCSSSelector, setInnerTextByXPathSelector). 55 WithArgRef(id). 56 WithArgSelector(selector). 57 WithArgValue(value) 58 } 59 60 var ( 61 getInnerTextByCSSSelector = fmt.Sprintf(` 62 (el, selector) => { 63 const found = el.querySelector(selector); 64 65 %s 66 67 return found.innerText; 68 }`, notFoundErrorFragment) 69 70 getInnerTextByXPathSelector = fmt.Sprintf(` 71 (el, selector) => { 72 %s 73 74 %s 75 76 return found.innerText; 77 }`, xpathAsElementFragment, notFoundErrorFragment) 78 ) 79 80 func GetInnerTextBySelector(id runtime.RemoteObjectID, selector drivers.QuerySelector) *eval.Function { 81 return toFunction(selector, getInnerTextByCSSSelector, getInnerTextByXPathSelector). 82 WithArgRef(id). 83 WithArgSelector(selector) 84 } 85 86 var ( 87 getInnerTextByCSSSelectorAll = fmt.Sprintf(` 88 (el, selector) => { 89 const found = el.querySelectorAll(selector); 90 91 %s 92 93 return Array.from(found).map(i => i.innerText); 94 }`, notFoundErrorFragment) 95 96 getInnerTextByXPathSelectorAll = fmt.Sprintf(` 97 (el, selector) => { 98 %s 99 100 %s 101 102 return found.map(i => i.innerText); 103 }`, xpathAsElementArrayFragment, notFoundErrorFragment) 104 ) 105 106 func GetInnerTextBySelectorAll(id runtime.RemoteObjectID, selector drivers.QuerySelector) *eval.Function { 107 return toFunction(selector, getInnerTextByCSSSelectorAll, getInnerTextByXPathSelectorAll). 108 WithArgRef(id). 109 WithArgSelector(selector) 110 }