Removing content from a page using Greasemonkey

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moishy
    New Member
    • Oct 2006
    • 104

    #1

    Removing content from a page using Greasemonkey

    I had some trouble trying to do this, so can somebody please help me out.
    I want a greasemonkey (javascript) that will remove the following code from the loading page:

    Remove all the code starting From:
    Code:
    <td>
    <span class="title1">
    <b>
    Until:
    Code:
    <hr>
    </td>
    I need the javascript that will remove that block of code.

    Thanx in advance!
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Access the table cell and call the parent's (the table/tbody) removeChild method.

      Comment

      • moishy
        New Member
        • Oct 2006
        • 104

        #4
        I tried several methods, but they don't seem to work for me.
        Can somebody please be so kind and post the javascript to accomplish this.

        Thanks!

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Moishy.

          Are you getting errors? Post your code so we can have a look.

          TSDN members are expected to write their own code. We're happy to help you out if you have specific questions, but you can't realistically expect someone to do your coding for you on a free forum.

          Comment

          • moishy
            New Member
            • Oct 2006
            • 104

            #6
            Ok, this is the Regex of what I want to eliminate:
            Code:
            (/<tr><td><span class=\"title1\"><b>[a-zA-Z0-9_\.]+<hr><\/td><\/tr>/g, "")
            But I can't figure out what to do with this Regex that I now have!
            I want this Regex to be removed from the HTML from the loading page.

            Any help please?

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Use regular expression replace, see link on the table object's innerHTML. However, the removeChild method would be better.

              Comment

              • moishy
                New Member
                • Oct 2006
                • 104

                #8
                But how am i supposed to 'getElementBy' to access the child that I want to remove?
                There are many <td>'s on the page, but I only want to remove those that have a <b>!
                How is that possible with removeChild?!

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, Moishy.

                  Try using document.getEle mentsByTagName( 'td') to locate the element you want to remove.

                  Comment

                  • moishy
                    New Member
                    • Oct 2006
                    • 104

                    #10
                    The page looks like this:

                    [HTML]<tr>
                    <td>
                    <span class="title1">
                    <b>
                    Here is the text that I want to remove, cuze I'm bold.
                    </b>
                    </span>
                    </td>
                    </tr>

                    <tr>
                    <td>
                    <span class="title1">
                    Here is the text that I want to keep, cuze I'm not bold.
                    </span>
                    </td>
                    </tr>
                    [/HTML]

                    There are some cells that have bold text, (those i want to remove), and some cells don't have bold text (those I don't want to toutch)

                    This is the code I came up with:
                    [HTML]<script type="text/javascript">
                    Find all TR's
                    var cell = document.getEle mentsByTagName( 'tr');
                    check if matches my string (bold)
                    if(cell.innerHT ML.match('<td>< span class="title1"> <b>')){
                    then remove that whole TR
                    cell.parentNode .removeChild(ce ll);
                    </script>[/HTML]

                    But it doesn't work!

                    Any help please?

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Remember that getElementsByTa gName will return an array.

                      Comment

                      • moishy
                        New Member
                        • Oct 2006
                        • 104

                        #12
                        Originally posted by acoder
                        Remember that getElementsByTa gName will return an array.
                        So what should I do if I want to process every <tr>, not just <tr>[1]
                        ?

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by moishy
                          So what should I do if I want to process every <tr>, not just <tr>[1]
                          ?
                          Loop over them using a for loop, e.g.
                          [CODE=javascript]var trs = document.getEle mentsByTagName( "tr");
                          for (i = 0; i < trs.length; i++) {
                          val = trs[i];...
                          }[/CODE]

                          Comment

                          • moishy
                            New Member
                            • Oct 2006
                            • 104

                            #14
                            ok, I've tried this, but this one erased everything!

                            Code:
                            tmp = document.getElementsByTagName('td');
                            for (i=0;i<tmp.length;i++){
                            if (tmp[i].innerHTML.match(/<span class="title1"><b>/)) {tmp[i].style.display = 'none';}
                            }
                            Any Help?

                            Comment

                            • moishy
                              New Member
                              • Oct 2006
                              • 104

                              #15
                              This is the best script that I can come up with, and I don't see why it shouldn't work!

                              Code:
                              var tmp = document.getElementsByTagName('td');
                              for (i=0;i<tmp.length;i++){
                              if(tmp[i].innerHTML.match(/<span class=\"title1\"><b>[a-zA-Z0-9_\.]+/)){
                              tmp[i].parentNode.removeChild(tmp[i]);}
                              }
                              But there must be something wrong with the IF, because this script removes Every <TD>

                              Anybody please?

                              Comment

                              Working...