2

I have a list that every bock is constructed like below. Some of the blocks have a <span class="protected-icon"></span>.
I would like to make a really simple greasemonkey plugin that removes that block.

So, my question is using Javascript how can I remove/hide the entire block ( <div data-item-type="user" class="js-stream-item stream-item"></div>that contains it?

<div data-item-type="user" class="js-stream-item stream-item">
<div class="user-content-rest">
    <span class="user-name">
         <span class="protected-icon"></span>
      </span>
</div>
</div>
arb
  • 7,753
  • 7
  • 31
  • 66
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

8 Answers8

5

How to do it without jQuery:

var p = document.getElementsByClassName('protected-icon');
for (var i=p.length; --i>=0;) {
    p[i].parentNode.removeChild(p[i]);
}

http://jsfiddle.net/sRs4s/1/

UPDATE If you want to remove the entire stream-item block, you have to loop up to it:

var p = document.getElementsByClassName('protected-icon');
var cstr = "stream-item";
for (var i=p.length; --i>=0;) {
    var n = p[i];
    while(n.className.split(" ").indexOf(cstr)==-1) { // won't work in older browsers
        n = n.parentNode;
    }
    n.parentNode.removeChild(n);
}

http://jsfiddle.net/sRs4s/3/

See Best way to find if an item is in a JavaScript array? if you need to support older browsers.

Community
  • 1
  • 1
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
3

To hide you can use the .hide() method.
To remove you can use the .remove() method.

Now to target the block you want

// change hide to remove for complete removal from the DOM.
$('.stream-item:has(.protected-icon)').hide(); 

will hide all the divs with class stream-item that contain an element with class protected-icon
Demo at http://jsfiddle.net/gaby/eeuQd/


Update

Here is a reference on using jQuery with greasemonkey How can I use jQuery in Greasemonkey?

I read that you are trying to use this with twitter page. Twitter is using Ajax requests to load parts of the page (and load new tweets..) so you might need to use an interval to your script that that it gets re-applied periodically..

That is because your code might run before the twitter has actually loaded the tweets in the page..

something like

setInterval(function(){
    $('.stream-item:has(.protected-icon)').hide();
}, 2000 ); // 2000 means every two seconds (in milliseconds)
Community
  • 1
  • 1
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

Use jQuery's closest() and .remove():

$('protected-icon').closest('div[data-item-type="user"]').remove();

You could also hide the element for later use:

$('protected-icon').closest('div[data-item-type="user"]').hide();
James Hill
  • 60,353
  • 20
  • 145
  • 161
  • But I want to remove the entire
    – EnexoOnoma Nov 28 '11 at 20:08
  • I don't think you want `closest()` here. The documentation says it only returns 0 or 1 item. If you want to remove ALL the
    tags, I don't think this would do it.
    – arb Nov 28 '11 at 20:13
  • @Zero21xxx, maybe I don't understand the requirement. Who said anything about all the div tags? The OP seems to indicate that he wants the div that contains `protected-icon` to be removed. – James Hill Nov 28 '11 at 20:14
  • I can't understand. Even though this is working on my local web server using Grease Monkey, it can not work on twitter page. Why is that ? – EnexoOnoma Nov 28 '11 at 20:59
  • @Kaoukkos, What do you mean it's not working? Do you get an error? Does the page in question include jQuery? – James Hill Nov 30 '11 at 14:29
1

I'm not very familiar with Greasemonkey but I noticed you tagged this as jQuery, so I'm assuming you would get use out of a jQuery script.

I would do this in case you want to bring it back at some point:

$('.protected-icon').parents('.js-stream-item.stream-item[data-item-type=user]').css({'display':'none'});
Jasper
  • 75,717
  • 14
  • 151
  • 146
Morgan Delaney
  • 2,349
  • 3
  • 20
  • 23
  • I can't understand. Even though this is working on my local web server using Grease Monkey, it can not work on twitter page. Why is that ? – EnexoOnoma Nov 28 '11 at 21:00
  • Hey Kaoukkos, I set up a jsfiddle for you to look at. http://jsfiddle.net/2nHKj/. If the problem persists, try isolating this script from any other script blocks, and continue debugging from there. – Morgan Delaney Nov 28 '11 at 22:16
1

It is simple using jQuery. Add the following in the top area of your greasemonkey script:

// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js

Then use the following jQuery:

$('span.protected-icon').parents('.js-stream-item').hide();

Update: Sorry, I had a typo. parent should have been parents.

xbrady
  • 1,683
  • 14
  • 23
  • I can't understand. Even though this is working on my local web server using Grease Monkey, it can not work on twitter page. Why is that ? – EnexoOnoma Nov 28 '11 at 20:59
1

With JQuery you could do the this to hide the block:

 $(document).ready(function() {
    $('span.protected-icon').hide();
 });

Or to remove it:

$(document).ready(function() {
   $('span.protected-icon').remove();
});
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
0

See the question asked here, which is a different question but contains the answer :-)

Remove element by id

edit: assuming you mean pure javascript, if you are using jQuery see all the other answers!

Community
  • 1
  • 1
Kamiel Wanrooij
  • 12,164
  • 6
  • 37
  • 43
  • I can't understand. Even though this is working on my local web server using Grease Monkey, it can not work on twitter page. Why is that ? – EnexoOnoma Nov 28 '11 at 20:59
0

This should work:

$('span.protected-icon').parents('.user-content-rest').remove();

That will find all the spans with the protected-icon class, then traverse the DOM tree until an element with the user-content-rest is encountered and remove that object.

arb
  • 7,753
  • 7
  • 31
  • 66
  • I can't understand. Even though this is working on my local web server using Grease Monkey, it can not work on twitter page. Why is that ? – EnexoOnoma Nov 28 '11 at 20:59
  • Then there is a difference between your local web page and Twitter or you're not loading the jQuery library correctly when you're not pointing at your local server. – arb Nov 28 '11 at 22:57