Skip to main content

chromiumoxide/javascript/
extract.rs

1pub(crate) const OUTER_HTML: &str = r###"{let rv = ''; if(document.doctype){rv+=new XMLSerializer().serializeToString(document.doctype);} if(document.documentElement){rv+=document.documentElement.outerHTML;} rv}"###;
2/// UTF-16 code-unit length of the same serialization as [`OUTER_HTML`].
3/// Returns a primitive `Number` so the result carries no `RemoteObjectId`
4/// and no runtime release is required.
5pub(crate) const OUTER_HTML_LEN: &str = r###"{let rv = ''; if(document.doctype){rv+=new XMLSerializer().serializeToString(document.doctype);} if(document.documentElement){rv+=document.documentElement.outerHTML;} rv.length}"###;
6/// UTF-8 byte length of the same serialization as [`OUTER_HTML`], computed
7/// inside V8 via `Blob` so the bytes are never shipped to Rust.
8pub(crate) const OUTER_HTML_BYTE_LEN: &str = r###"{let rv = ''; if(document.doctype){rv+=new XMLSerializer().serializeToString(document.doctype);} if(document.documentElement){rv+=document.documentElement.outerHTML;} new Blob([rv]).size}"###;
9/// XML serializer for custom pages or testing.
10pub(crate) const FULL_XML_SERIALIZER_JS: &str = "(()=>{let e=document.querySelector('#webkit-xml-viewer-source-xml');let x=e?e.innerHTML:new XMLSerializer().serializeToString(document);return x.startsWith('<?xml')?x:'<?xml version=\"1.0\" encoding=\"UTF-8\"?>\\n'+x})()";
11
12/// Generate a marker at the click position. Useful for older chrome versions.
13pub(crate) fn generate_marker_js(x: f64, y: f64) -> String {
14    format!(
15        "(()=>{{const m=document.createElement('div');m.style='position:absolute;left:{}px;top:{}px;width:10px;height:10px;background:hsl('+Math.floor(Math.random()*360)+',100%,50%);border:2px solid white;border-radius:50%;z-index:9999;pointer-events:none';document.body.appendChild(m);}})();",
16        x - 5.0,
17        y - 5.0
18    )
19}