github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/templates/inner_html.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 setInnerHTML = `(el, value) => {
    14  	el.innerHTML = value;
    15  }`
    16  
    17  func SetInnerHTML(id runtime.RemoteObjectID, value values.String) *eval.Function {
    18  	return eval.F(setInnerHTML).WithArgRef(id).WithArgValue(value)
    19  }
    20  
    21  const getInnerHTML = `(el) => {
    22  	if (el.nodeType !== 9) {
    23  		return el.innerHTML;
    24  	}
    25  
    26  	return document.documentElement.innerHTML;
    27  }`
    28  
    29  func GetInnerHTML(id runtime.RemoteObjectID) *eval.Function {
    30  	return eval.F(getInnerHTML).WithArgRef(id)
    31  }
    32  
    33  var (
    34  	setInnerHTMLByCSSSelector = fmt.Sprintf(`(el, selector, value) => {
    35  	const found = el.querySelector(selector);
    36  
    37  	%s
    38  
    39  	found.innerHTML = value;
    40  }`, notFoundErrorFragment)
    41  
    42  	setInnerHTMLByXPathSelector = fmt.Sprintf(`(el, selector, value) => {
    43  	%s
    44  
    45  	%s
    46  
    47  	found.innerHTML = value;
    48  }`, xpathAsElementFragment, notFoundErrorFragment)
    49  )
    50  
    51  func SetInnerHTMLBySelector(id runtime.RemoteObjectID, selector drivers.QuerySelector, value values.String) *eval.Function {
    52  	return toFunction(selector, setInnerHTMLByCSSSelector, setInnerHTMLByXPathSelector).
    53  		WithArgRef(id).
    54  		WithArgSelector(selector).
    55  		WithArgValue(value)
    56  }
    57  
    58  var (
    59  	getInnerHTMLByCSSSelector = fmt.Sprintf(`(el, selector) => {
    60  	const found = el.querySelector(selector);
    61  
    62  	%s
    63  
    64  	return found.innerHTML;
    65  }`, notFoundErrorFragment)
    66  
    67  	getInnerHTMLByXPathSelector = fmt.Sprintf(`(el, selector) => {
    68  	%s
    69  
    70  	%s
    71  
    72  	return found.innerHTML;
    73  }`, xpathAsElementFragment, notFoundErrorFragment)
    74  )
    75  
    76  func GetInnerHTMLBySelector(id runtime.RemoteObjectID, selector drivers.QuerySelector) *eval.Function {
    77  	return toFunction(selector, getInnerHTMLByCSSSelector, getInnerHTMLByXPathSelector).
    78  		WithArgRef(id).
    79  		WithArgSelector(selector)
    80  }
    81  
    82  const getInnerHTMLByCSSSelectorAll = `(el, selector) => {
    83  	const found = el.querySelectorAll(selector);
    84  
    85  	return Array.from(found).map(i => i.innerHTML);
    86  }`
    87  
    88  var getInnerHTMLByXPathSelectorAll = fmt.Sprintf(`(el, selector) => {
    89  	%s
    90  
    91  	%s
    92  
    93  	return found.map(i => i.innerHTML);
    94  }`, xpathAsElementArrayFragment, notFoundErrorFragment)
    95  
    96  func GetInnerHTMLBySelectorAll(id runtime.RemoteObjectID, selector drivers.QuerySelector) *eval.Function {
    97  	return toFunction(selector, getInnerHTMLByCSSSelectorAll, getInnerHTMLByXPathSelectorAll).
    98  		WithArgRef(id).
    99  		WithArgSelector(selector)
   100  }