Created
September 11, 2020 18:41
-
-
Save viniciusbig/b865bc1e024e67a08b93cee4b873f8f5 to your computer and use it in GitHub Desktop.
Chrome snippet to get HTML comments with jQuery
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
jQuery.fn.comments = function( blnDeep ){ | |
var blnDeep = (blnDeep || false); | |
var jComments = $( [] ); | |
// Loop over each node to search its children for | |
// comment nodes and element nodes (if deep search). | |
this.each( | |
function( intI, objNode ){ | |
var objChildNode = objNode.firstChild; | |
var strParentID = $( this ).attr( "id" ); | |
// Keep looping over the top-level children | |
// while we have a node to examine. | |
while (objChildNode){ | |
// Check to see if this node is a comment. | |
if (objChildNode.nodeType === 8){ | |
// We found a comment node. Add it to | |
// the nodes collection wrapped in a | |
// DIV (as we may have HTML). | |
jComments = jComments.add( | |
"<div rel='" + strParentID + "'>" + | |
objChildNode.nodeValue + | |
"</div>" | |
); | |
} else if ( | |
blnDeep && | |
(objChildNode.nodeType === 1) | |
) { | |
// Traverse this node deeply. | |
jComments = jComments.add( | |
$( objChildNode ).comments( true ) | |
); | |
} | |
// Move to the next sibling. | |
objChildNode = objChildNode.nextSibling; | |
} | |
} | |
); | |
// Return the jQuery comments collection. | |
return( jComments ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment