github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/templates/attributes.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/cdp/eval" 9 "github.com/MontFerret/ferret/pkg/runtime/values" 10 ) 11 12 const getAttribute = `(el, name) => { 13 return el.getAttribute(name) 14 }` 15 16 func GetAttribute(id runtime.RemoteObjectID, name values.String) *eval.Function { 17 if name == "style" { 18 return GetStyles(id) 19 } 20 21 return eval.F(getAttribute).WithArgRef(id).WithArgValue(name) 22 } 23 24 var getAttributes = fmt.Sprintf(`(element) => { 25 const getStyles = %s; 26 return element.getAttributeNames().reduce((res, name) => { 27 const out = res; 28 let value; 29 30 if (name !== "style") { 31 value = element.getAttribute(name); 32 } else { 33 value = getStyles(element); 34 } 35 36 out[name] = value; 37 38 return out; 39 }, {}); 40 }`, getStyles) 41 42 func GetAttributes(id runtime.RemoteObjectID) *eval.Function { 43 return eval.F(getAttributes).WithArgRef(id) 44 } 45 46 const setAttribute = `(el, name, value) => { 47 el.setAttribute(name, value) 48 }` 49 50 func SetAttribute(id runtime.RemoteObjectID, name, value values.String) *eval.Function { 51 return eval.F(setAttribute).WithArgRef(id).WithArgValue(name).WithArgValue(value) 52 } 53 54 const setAttributes = `(el, values) => { 55 Object.keys(values).forEach((name) => { 56 const value = values[name]; 57 el.setAttribute(name, value) 58 }); 59 }` 60 61 func SetAttributes(id runtime.RemoteObjectID, values *values.Object) *eval.Function { 62 return eval.F(setAttributes).WithArgRef(id).WithArgValue(values) 63 } 64 65 const removeAttribute = `(el, name) => { 66 el.removeAttribute(name) 67 }` 68 69 func RemoveAttribute(id runtime.RemoteObjectID, name values.String) *eval.Function { 70 return eval.F(removeAttribute).WithArgRef(id).WithArgValue(name) 71 } 72 73 const removeAttributes = `(el, names) => { 74 names.forEach(name => el.removeAttribute(name)); 75 }` 76 77 func RemoveAttributes(id runtime.RemoteObjectID, names []values.String) *eval.Function { 78 return eval.F(removeAttributes).WithArgRef(id).WithArg(names) 79 } 80 81 const getNodeType = `(el) => el.nodeType` 82 83 func GetNodeType(id runtime.RemoteObjectID) *eval.Function { 84 return eval.F(getNodeType).WithArgRef(id) 85 } 86 87 const getNodeName = `(el) => el.nodeName` 88 89 func GetNodeName(id runtime.RemoteObjectID) *eval.Function { 90 return eval.F(getNodeName).WithArgRef(id) 91 }