zono

htmlSearcher

Sep 10th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var htmlSearcher = ( function ( ) {
  2.     /**************************************
  3.      * Entry point of the "application"
  4.      **************************************/
  5.     var onloadAttachedFunctions = null;
  6.    
  7.     var startTagMark = "<!--comment_id=tag_";
  8.    
  9.     var endTagmark = "<!--end_of_html_tag_holder-->";
  10.    
  11.     if(typeof window.onload == "function") {
  12.         onloadAttachedFunctions = window.onload;
  13.     }
  14.            
  15.     window.onload = function() {
  16.       if(onloadAttachedFunctions != null) {
  17.         onloadAttachedFunctions();
  18.       }
  19.       var innerHtmlBodyContent = document.body.innerHTML;
  20.       innerHtmlBodyContent = wrapCommentsAroundHtmlTag("img", innerHtmlBodyContent);
  21.       document.body.innerHTML = innerHtmlBodyContent;
  22.       innerHtmlBodyContent = null;
  23.       onloadAttachedFunctions = null;
  24.     }
  25.    
  26.     var wrapCommentsAroundHtmlTag = function( htmlTagName, htmlString ) {
  27.         var htmlTagRegExp = new RegExp("<\s*" + htmlTagName + "[^>]+>", "gi");
  28.         var tagCounter = 0;
  29.         return htmlString.replace(htmlTagRegExp,
  30.         function(htmlTagAsString) {
  31.                 tagCounter++;
  32.                 var replacement = startTagMark + tagCounter + "-->" + htmlTagAsString + endTagmark;
  33.                 return  replacement;
  34.         });
  35.     }
  36.    
  37.     var findEquivalentHtmlNode = function(htmlString) {
  38.         for(var i = 0 ; i < document.body.childNodes.length ; i++)
  39.         {
  40.             var currentNode = document.body.childNodes[i];
  41.             var currentData = typeof currentNode.data != 'undefined' ? currentNode.data : '';
  42.             if (currentData == htmlString) {
  43.                 return currentNode.nextSibling;
  44.             }
  45.         }
  46.         return null;
  47.     }
  48.    
  49.     return {
  50.        
  51.         getElementAsString : function ( elementId ) {
  52.             var tagBetweenComments = new RegExp(startTagMark + elementId + "-->((?:(?!" + endTagmark + ")(?:.|\s))+)" + endTagmark, "gi");
  53.             if(tagBetweenComments.test(document.body.innerHTML)) {
  54.                 return RegExp.$1;
  55.             }
  56.             return null;
  57.         },
  58.        
  59.         getElementAsObject : function ( elementId ) {
  60.                 var arg = "comment_id=tag_"+elementId;
  61.                 return findEquivalentHtmlNode(arg);
  62.         }
  63.     }
  64.    
  65. })( );
Advertisement
Add Comment
Please, Sign In to add comment