github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/templates/scroll.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 isElementInViewportFragment = `function isInViewport(i) { 14 var bounding = i.getBoundingClientRect(); 15 16 return ( 17 bounding.top >= 0 && 18 bounding.left >= 0 && 19 bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) && 20 bounding.right <= (window.innerWidth || document.documentElement.clientWidth) 21 ); 22 }` 23 24 scroll = `(opts) => 25 window.scrollTo({ 26 left: opts.left, 27 top: opts.top, 28 behavior: opts.behavior, 29 block: opts.block, 30 inline: opts.inline 31 }); 32 }` 33 34 scrollTop = `(opts) => { 35 window.scrollTo({ 36 left: 0, 37 top: 0, 38 behavior: opts.behavior, 39 block: opts.block, 40 inline: opts.inline 41 }); 42 }` 43 44 scrollBottom = `(opts) => { 45 window.scrollTo({ 46 left: 0, 47 top: window.document.body.scrollHeight, 48 behavior: opts.behavior, 49 block: opts.block, 50 inline: opts.inline 51 }); 52 }` 53 ) 54 55 var ( 56 scrollIntoView = fmt.Sprintf(`(el, opts) => { 57 %s 58 59 if (!isInViewport(el)) { 60 el.scrollIntoView({ 61 behavior: opts.behavior, 62 block: opts.block, 63 inline: opts.inline 64 }); 65 } 66 67 return true; 68 }`, isElementInViewportFragment) 69 70 scrollIntoViewByCSSSelector = fmt.Sprintf(`(el, selector, opts) => { 71 const found = el.querySelector(selector); 72 73 %s 74 75 %s 76 77 if (!isInViewport(found)) { 78 found.scrollIntoView({ 79 behavior: opts.behavior, 80 block: opts.block, 81 inline: opts.inline 82 }); 83 } 84 85 return true; 86 }`, notFoundErrorFragment, isElementInViewportFragment) 87 88 scrollIntoViewByXPathSelector = fmt.Sprintf(`(el, selector, opts) => { 89 %s 90 91 %s 92 93 %s 94 95 if (!isInViewport(found)) { 96 found.scrollIntoView({ 97 behavior: opts.behavior, 98 block: opts.block, 99 inline: opts.inline 100 }); 101 } 102 103 return true; 104 }`, xpathAsElementFragment, notFoundErrorFragment, isElementInViewportFragment) 105 ) 106 107 func Scroll(options drivers.ScrollOptions) *eval.Function { 108 return eval.F(scroll).WithArg(options) 109 } 110 111 func ScrollTop(options drivers.ScrollOptions) *eval.Function { 112 return eval.F(scrollTop).WithArg(options) 113 } 114 115 func ScrollBottom(options drivers.ScrollOptions) *eval.Function { 116 return eval.F(scrollBottom).WithArg(options) 117 } 118 119 func ScrollIntoView(id runtime.RemoteObjectID, options drivers.ScrollOptions) *eval.Function { 120 return eval.F(scrollIntoView).WithArgRef(id).WithArg(options) 121 } 122 123 func ScrollIntoViewBySelector(id runtime.RemoteObjectID, selector drivers.QuerySelector, options drivers.ScrollOptions) *eval.Function { 124 return toFunction(selector, scrollIntoViewByCSSSelector, scrollIntoViewByXPathSelector). 125 WithArgRef(id). 126 WithArgSelector(selector). 127 WithArg(options) 128 }