Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Last active November 30, 2022 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenSem/0f354541b831711195ef406b167f1113 to your computer and use it in GitHub Desktop.
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) )
// 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