0

There are many easily found examples of removing a single element from a page's DOM.

A good example here is How to use greasemonkey to selectively remove content from a website

I've tried to write scripts that will remove multiple DIVs and only the first is removed;

// ==UserScript==
// @name        Clean CreativeMarket
// @namespace   Fred
// @include     http://creativemarket.com*
// @include     https://creativemarket.com*
// @version     1
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant       GM_addStyle

var elem = document.getElementById("site-banner");
elem.parentNode.removeChild(elem);

var elem02 = document.getElementById("navigation-sub");
elem02.parentNode.removeChild(elem02);

...won't work. Only the first div is removed.

The goal is a single userscript which will work on static and AJAX pages (as demonstrated in the stackoverflow post mentioned above), matching DIVs by domain and (id and/or style).

Here is an example of some uBlock Origin filters to illustrate:

couponscorpion.com##.clearfix.single.main-side > .swp_one.scale-.scale-100.swp_other_full_color.swp_individual_full_color.swp_default_full_color.swp_flat_fresh.swp_social_panel
couponscorpion.com##.dark_style.footer-bottom
couponscorpion.com##.left_st_postproduct
couponscorpion.com##.sidebar
couponscorpion.com##ul:nth-of-type(2)
coupontry.com###header
coupontry.com###sidebar
creativemarket.com###communicate
creativemarket.com###fgotw-seller-list
creativemarket.com###products-by
creativemarket.com###similar-recommended

Would someone please show me how to do this?

  • edit 01

Yes, I know see how what I typed is confusing. There are 3 questions:

  1. Remove an element based on its own characteristics (id, style, etc.)
  2. Remove an element based on its own and its parents characteristics
  3. Conditional execution of portions of the code only on specified URL or portion thereof. For example, removing common code such as 1x1 pixels with reference to farcebook for tracking as opposed to site-specific elements. The goal is a single userscript which is variably applied depending on URL.

    @Brock Adams - You've commented on a couple of my posts. I didn't give up trying things, I kept looking for answers then posted as my comprehension increased. Your function looks like it might be the solution BUT, as with almost every other example I've seen on the web, the code snippets show only one removal function and the code I'd created was only removing the first occurrence of multiple removal functions. Those aren't the same situation. Every attempt I made to find examples of multiple removals didn't bring a good example. Was my code syntactically incorrect, grammatically incorrect, subject to concurrence issues, subject to variable scope, etc., etc. etc.? I don't know. That's why I'm asking.

  • 1
    Do you have errors in your console? – skyline3000 Jul 24 '18 at 02:31
  • What do you want exactly? Do you want `elem` removed, `elem`'s children removed, the parent node of `elem` removed, or all the childrens of `elem`'s parent node removed (i.e. `elem` and all its siblings)? – SamVK Jul 24 '18 at 02:51
  • Currently you're getting an element, then grabbing its parent...but then deleting that original child element. If you want to remove the parent element, just do `elem.parentNode.remove()`. If you want to keep that element but empty it, take a look at: https://stackoverflow.com/a/3955238/5812047 – SamVK Jul 24 '18 at 02:57
  • This appears to be a duplicate of the question you linked, except that you did not use the ajax techniques and probably need to. Answer skyline3000's question and then explain how this is different from the previous question(s). – Brock Adams Jul 24 '18 at 03:14
  • @Brock Adams - linked post doesn't discuss variable application of code based on URL. I should have more explicitly stated I'd like to use one method that would work on AJAX and non-AJAX source. – FredThompson Jul 29 '18 at 21:47
  • @SamVK - Both situations, actually. Many of the ref sources for userscripts specifically mention that odd circular pointer use. Here's one from O'Reilly's book, "Greasemonkey Hacks"; https://www.safaribooksonline.com/library/view/greasemonkey-hacks/0596101651/ch01s07.html – FredThompson Jul 29 '18 at 21:49

0 Answers0