Search code examples
greasemonkey

Greasemonkey delete li element


Using greasemonkey how can I delete a 'li' node from youtube page:

...
    <li class="guide-channel guide-notification-item overflowable-list-item " id="create_channel-guide-item" data-visibility-tracking="" role="menuitem">

      <a class="guide-item yt-uix-sessionlink yt-valign spf-nolink   " href="/create_channel" title="My Channel" data-sessionlink="ei=CzwqV8TfL8KqWpTMrqgN&amp;feature=g-personal&amp;ved=CJUCELUsGAEiEwiEkeuVg8HMAhVClRYKHRSmC9Uojh4" data-visibility-tracking="" data-external-id="create_channel" data-serialized-endpoint="8qHduQECCAE%3D">
        <span class="yt-valign-container">
            <span class="thumb guide-my-channel-icon yt-sprite"></span>
            <span class="display-name  no-count">
              <span>
                My Channel
              </span>
            </span>
        </span>
      </a>
....

Solution

  • You can just do:

    var li = document.getElementById('create_channel-guide-item');
    li.parentNode.removeChild(li);
    

    However, if that content is loaded asynchronously, you need to repeat this until it works:

    (function removeLi(try) {
        if (!try) return; // give up
        var li = document.getElementById('create_channel-guide-item');
        if (li) { // found!
            li.parentNode.removeChild(li);
            return;
        }
        setTimeout(removeLi.bind(null, try-1), 100);
    })(100); // try 100 times