Last active
November 30, 2022 20:00
-
-
Save DarrenSem/0f354541b831711195ef406b167f1113 to your computer and use it in GitHub Desktop.
selectedHtml.js -- returns [htmlString, htmlString.length] , args are ALL optional ( doc, selection = doc.getSelection(), range = selection.getRangeAt(0) )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// selectedHtml.js -- returns [htmlString, htmlString.length] , args are ALL optional ( doc, selection = doc.getSelection(), range = selection.getRangeAt(0) ) | |
// const selectedHtml=(b=document,c=b.getSelection(),d=c&&c.getRangeAt(0),e=d&&a.createElement("div"),f=e?(e.appendChild(d.cloneContents()),e.innerHTML):"")=>[f,f.length]; | |
const selectedHtml = ( | |
_doc = document, | |
_sel = _doc.getSelection(), // "You can call Window.getSelection(), which works identically to Document.getSelection()." https://developer.mozilla.org/en-US/docs/Web/API/Document/getSelection | |
_range = _sel && _sel.getRangeAt(0), | |
_el = _range && doc.createElement("div"), | |
_result = !_el ? "" : ( | |
_el.appendChild( _range.cloneContents() ), | |
_el.innerHTML | |
) | |
) => [_result, _result.length]; | |
let [htmlThatIsSelected, length] = selectedHtml(); | |
if(length) { | |
console.log({htmlThatIsSelected}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment