Chrome extension, making a link from key words in the body

前端 未结 1 1149
孤独总比滥情好
孤独总比滥情好 2020-12-20 05:10

So that you understand my knowledge base, I am a computer engineering major, and am working a job right now at a medical company over the summer. I have little (almost zero

相关标签:
1条回答
  • 2020-12-20 05:55

    I wondered a few times on what's the best way to get the text nodes and meant to look at TreeWalking, so I did this time. Following is the test page I made, I can't say if this is the best way but may suite your needs.

    treewalker.html

    <html>
      <head>
        <style>
        </style>
        <script src="treewalker.js"></script>
      </head>
      <body>
        <div>This is a div</div>
        <div><div id='testevent'>Test event</div>This is a div 000-000-0000</div>
        <div>This is a div 000-000-0000</div>
         <div>This is<a href='sf'>bleh 000-000-0000 a div</a></div>
      </body>
    </html>
    

    treewalker.js

    function onLoad() {
    
      document.querySelector('#testevent').onclick = function() {
        alert('clicked')
      };
    
      // Here starts the bit for your content script
      var re = /(\d*[/-]*[0-9][0-9][0-9][/ -]*[0-9][0-9][0-9][/ -]*[0-9][0-9][0-9][0-9][ ]*)/g;
      var regs;
    
      var walker = document.createTreeWalker(
      document.body, NodeFilter.SHOW_TEXT, function(node) {
        if((regs = re.exec(node.textContent))) {
          // make sure the text nodes parent doesnt have an attribute we add to know its allready been highlighted
          if(!node.parentNode.classList.contains('highlighted_text')) {
            var match = document.createElement('A');
            match.appendChild(document.createTextNode(regs[0]));
            match.href = 'http://www.google.com';
    
            // add an attribute so we know this element is one we added
            // Im using a class so you can target it with css easily
            match.classList.add('highlighted_text');
    
            var after = node.splitText(regs.index);
            after.nodeValue = after.nodeValue.substring(regs[0].length);
            node.parentNode.insertBefore(match, after);
          }
        }
        return NodeFilter.FILTER_SKIP;
      }, false);
    
      // Make the walker step through the nodes
      walker.nextNode();
    
      // and it ends here
    }
    
    (function() {
      document.addEventListener("DOMContentLoaded", onLoad);
    })();
    

    Code Stolen From

    http://paul.kinlan.me/dom-treewalker/
    Thats where I got the treewalker code from. Problem with his sample is it wraps the match using innerHTML on the parent (a lot of the examples do), this kills the event in the test page.

    http://www.the-art-of-web.com/javascript/search-highlight/
    Showed how to split the text node properly. And for all I know is a better way of doing this, but I was interested in the TreeWalker way.

    EDIT
    I just updated it because if you ran the old version (click the Edited link below to see it) failed on the html in this new version. For some reason that I really dont understand it wouldn't wrap the second numner. This new version doesn't work the way all the examples I saw did and seems an abusive way to use TreeWalker...but it works!

    0 讨论(0)
提交回复
热议问题