Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Delete table row based on its contents

    So what I'm trying to do is, delete all table rows that contains a link that contains any number from my "blacklist".

    For example, my blacklist contains:
    50636
    64067
    60469
    60342
    (and 46 more..)

    What the script will do is check if a link (a href) contains any of those numbers. If it finds a match it will delete the parent table row (tr) of the link.
    I tried googling but most results are non-related or they're jquery. I can't make jquery work on userscript, sadly.
    Last edited by Undisclosed; 06-12-2014 at 07:36 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    I remember I saw this question earlier on userscripts.org, right?

    So let's get started

    Code:
    window.addEventListener('load', function() {
       var blacklist = ["50636", "64067"];   // add more items here
       var theLinks = document.links;
       for(i=0, max=theLinks.length; i<max; i++) {
          if(blacklist.indexOf(theLinks[i].href.match(/(\d+)/)[1]) != -1) {
             removeParentTR(theLinks[i].parentNode);
          }
       }
    }, false);
    
    function removeParentTR(node) {
       if(node.tagName.toUpperCase() == 'TR') {
          node.parentNode.removeChild(node);
       } else if(node != document) {
          removeParentTR(node.parentNode);
       }
    }
    The important thing here is, that the link's first number must be the one matching your blacklist, because it will try to match the first number found in each link.

    EDIT: There are two ways to include jQuery in your userscript

    1. If the current page uses jQuery, you can use this jQuery in your userscript too
    Code:
    if(unsafeWindow.jQuery) {
       $ = unsafeWindow.jQuery;
       .... // your code here
    }
    2. Your can @require jQuery from any CDN in the head section of the userscript
    Code:
    // @require    https://code.jquery.com/jquery-1.11.0.min.js
    Last edited by devnull69; 06-11-2014 at 09:41 PM.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by devnull69 View Post

    1. If the current page uses jQuery, you can use this jQuery in your userscript too
    Code:
    if(unsafeWindow.jQuery) {
       $ = unsafeWindow.jQuery;
       .... // your code here
    }
    from what I've seen, if you specify @grant none in the metablock you can piggyback straight onto the host page's jQuery without having to use unsafeWindow

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Is it supposed to work even if the tr is not the link's direct parent? (I'm not fully sure how the parentnode works) Anyway, the actual set up is like this. The script doesn't work, it doesn't delete the tablerow.
    Code:
    <tr>
    	<td></td>
    	<td>
    		<div>
    			<div>
    				<span></span>
    				<a href="http://www.example.com/forums/thread.999999"></a>
    			</div>
    		</div>
    		
    	</td>
    	<td></td>
    	<td></td>
    </tr>
    PS. Yes, I tried in userscripts.org but it was filled with spamming ad bots, I figured my post would be unseen.

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Right ... this is what happens with untested code. The problem was that you shouldn't delete one of the link's parents while you are still in the links loop. I had to store the links and delete those links after the loop
    Code:
    window.addEventListener('load', function() {
       var blacklist = ["999999"];   // add more items here
       var theLinks = document.links;
       var toBeDeleted = [];
       for(i=0, max=theLinks.length; i<max; i++) {
          if(blacklist.indexOf(theLinks[i].href.match(/(\d+)/)[1]) != -1) {
             toBeDeleted.push(theLinks[i]);
          }
       }
       for(i=0, max=toBeDeleted.length; i<max; i++) {
          removeParentTR(toBeDeleted[i].parentNode);
       }
    }, false);
    
    function removeParentTR(node) {
       if(node.tagName.toUpperCase() == 'TR') {
          node.parentNode.removeChild(node);
       } else if(node != document) {
          removeParentTR(node.parentNode);
       }
    }
    See working example here: JS Bin - Collaborative JavaScript Debugging

  • Users who have thanked devnull69 for this post:

    Undisclosed (06-12-2014)

  • #6
    New Coder
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thank you so much it seems to work perfectly. I even created my own html page to see if it works on userscript and it did. Somehow it doesn't work on the site I am aiming to use it on, but I'll see if I can figure out why.

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Could you give us the URL to the site on which you want to use the userscript?

  • Users who have thanked devnull69 for this post:

    Undisclosed (06-12-2014)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •