github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/templates/styles.go (about)

     1  package templates
     2  
     3  import (
     4  	"github.com/mafredri/cdp/protocol/runtime"
     5  
     6  	"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  )
     9  
    10  const getStyles = `(el) => {
    11  	const out = {};
    12  	const styles = window.getComputedStyle(el);
    13  
    14  	Object.keys(styles).forEach((key) => {
    15  		if (!isNaN(parseFloat(key))) {
    16  			const name = styles[key];
    17  			const value = styles.getPropertyValue(name);
    18  			out[name] = value;
    19  		}
    20  	});
    21  
    22  	return out;
    23  }`
    24  
    25  func GetStyles(id runtime.RemoteObjectID) *eval.Function {
    26  	return eval.F(getStyles).WithArgRef(id)
    27  }
    28  
    29  const getStyle = `(el, name) => {
    30  	const styles = window.getComputedStyle(el);
    31  
    32  	return styles[name];
    33  }`
    34  
    35  func GetStyle(id runtime.RemoteObjectID, name values.String) *eval.Function {
    36  	return eval.F(getStyle).WithArgRef(id).WithArgValue(name)
    37  }
    38  
    39  const setStyle = `(el, name, value) => {
    40  	el.style[name] = value;
    41  }`
    42  
    43  func SetStyle(id runtime.RemoteObjectID, name, value values.String) *eval.Function {
    44  	return eval.F(setStyle).WithArgRef(id).WithArgValue(name).WithArgValue(value)
    45  }
    46  
    47  const setStyles = `(el, values) => {
    48  	Object.keys(values).forEach((key) => {
    49  		el.style[key] = values[key]
    50  	});
    51  }`
    52  
    53  func SetStyles(id runtime.RemoteObjectID, values *values.Object) *eval.Function {
    54  	return eval.F(setStyles).WithArgRef(id).WithArgValue(values)
    55  }
    56  
    57  const removeStyles = `(el, names) => {
    58  	const style = el.style;
    59  	names.forEach((name) => { style[name] = "" })
    60  }`
    61  
    62  func RemoveStyles(id runtime.RemoteObjectID, names []values.String) *eval.Function {
    63  	return eval.F(removeStyles).WithArgRef(id).WithArg(names)
    64  }
    65  
    66  const removeStylesAll = `(el) => {
    67  	el.removeAttribute("style");
    68  }`
    69  
    70  func RemoveStylesAll(id runtime.RemoteObjectID) *eval.Function {
    71  	return eval.F(removeStylesAll).WithArgRef(id)
    72  }