Last active
December 31, 2023 23:49
-
-
Save crabshank/da1ea7537443d6820fcdf3b9cc48d089 to your computer and use it in GitHub Desktop.
Adds numerical references (incrementing by 1 from "[0]") to the parentElements of each text node in a document, or specified tag (see lines 81-82). This gist is to help with note-taking and annotation.
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
| function keepMatchesShadow(els,slcArr,isNodeName){ | |
| if(slcArr[0]===false){ | |
| return els; | |
| }else{ | |
| let out=[]; | |
| for(let i=0, len=els.length; i<len; i++){ | |
| let n=els[i]; | |
| for(let k=0, len_k=slcArr.length; k<len_k; k++){ | |
| let sk=slcArr[k]; | |
| if(isNodeName){ | |
| if((n.nodeName.toLocaleLowerCase())===sk){ | |
| out.push(n); | |
| } | |
| }else{ //selector | |
| if(!!n.matches && typeof n.matches!=='undefined' && n.matches(sk)){ | |
| out.push(n); | |
| } | |
| } | |
| } | |
| } | |
| return out; | |
| } | |
| } | |
| function getMatchingNodesShadow(docm, slc, isNodeName, onlyShadowRoots){ | |
| let slcArr=[]; | |
| if(typeof(slc)==='string'){ | |
| slc=(isNodeName && slc!==false)?(slc.toLocaleLowerCase()):slc; | |
| slcArr=[slc]; | |
| }else if(typeof(slc[0])!=='undefined'){ | |
| for(let i=0, len=slc.length; i<len; i++){ | |
| let s=slc[i]; | |
| slcArr.push((isNodeName && slc!==false)?(s.toLocaleLowerCase()):s) | |
| } | |
| }else{ | |
| slcArr=[slc]; | |
| } | |
| var shrc=[docm]; | |
| var shrc_l=1; | |
| var out=[]; | |
| let srCnt=0; | |
| while(srCnt<shrc_l){ | |
| let curr=shrc[srCnt]; | |
| let sh=(!!curr.shadowRoot && typeof curr.shadowRoot !=='undefined')?true:false; | |
| let nk=keepMatchesShadow([curr],slcArr,isNodeName); | |
| let nk_l=nk.length; | |
| if( !onlyShadowRoots && nk_l>0){ | |
| for(let i=0; i<nk_l; i++){ | |
| out.push(nk[i]); | |
| } | |
| } | |
| for(let i=0, len=curr.childNodes.length; i<len; i++){ | |
| shrc.push(curr.childNodes[i]); | |
| } | |
| if(sh){ | |
| let cs=curr.shadowRoot; | |
| let csc=[...cs.childNodes]; | |
| if(onlyShadowRoots){ | |
| if(nk_l>0){ | |
| out.push({root:nk[0], childNodes:csc}); | |
| } | |
| } | |
| for(let i=0, len=csc.length; i<len; i++){ | |
| shrc.push(csc[i]); | |
| } | |
| } | |
| srCnt++; | |
| shrc_l=shrc.length; | |
| } | |
| return out; | |
| } | |
| let refClass='ref_text'; //Class name for tags with references | |
| let n=getMatchingNodesShadow(document.documentElement, '#text', true, false).filter((t)=>{return t.parentElement.tagName!=='TITLE';}); | |
| //A different 1st argument to "getMatchingNodesShadow(…)" can be used | |
| let allTextNodeParentEls=[]; | |
| for(let k=0, len_k=n.length; k<len_k; k++){ | |
| let nk=n[k] | |
| let pp=nk.parentElement; | |
| if(!allTextNodeParentEls.includes(pp)){ //Extra conditions can be added to restrict the types of tags to which references will be added | |
| allTextNodeParentEls.push(pp); | |
| pp.classList.add(refClass); | |
| } | |
| } | |
| let styl=`<style>:root { | |
| counter-reset: link !important; | |
| counter-increment:link -1 !important; //Makes 1st reference "[0]" rather than "[1]" | |
| } | |
| .${refClass}:before{ | |
| counter-increment:link !important; | |
| content: "[" counter(link) "]" !important; | |
| color: initial !important; | |
| }</style>`; | |
| document.head.insertAdjacentHTML('afterbegin',styl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment