Posted: . At: 10:01 AM. This was 3 months ago. Post ID: 19092
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


A nice userscript example to edit a web page and remove a CSS class.


Editing a website with a userscript is very useful. This can allow a lot of manipulation of the content of a website. This example I am showing is how to edit the HTML of a site and remove all CSS classes of a certain type.

Here we have the CSS class “hidden”.

This is used to hide all these paragraphs unless a visitor is a subscriber.

But this can be fixed easily with this userscript.

// ==UserScript==
// @name         Daily Advertiser
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.dailyadvertiser.com.au/story/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=dailyadvertiser.com.au
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const boxes = document.querySelectorAll("div");

    boxes.forEach(div => {
        // ✅ Remove class from each element
        div.classList.remove('hidden');
         //✅ Add class to each element
        div.classList.add('show');
    });

})();

This will iterate over all DIV tags and remove the CSS class “hidden”.

This is how easy this is. This makes it very easy to remove an annoying CSS property for example.

Another example, this will remove a CSS class from a TD tag.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.dailytelegraph.com.au/news/nsw
// @icon         https://www.google.com/s2/favicons?sz=64&domain=dailytelegraph.com.au
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const allBoldTd = document.querySelectorAll('article[class*=""]');
    allBoldTd.forEach(td => td.class.removeProperty('storyblock'))

    // Your code here...
})();

To remove query parameters added on a website, use a userscript like this.

// ==UserScript==
// @name         Remove Daily Mail UK query parameters.
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Removes URL query parameters from the current page.
// @author       John Cartwright
// @match        https://www.dailymail.co.uk/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=dailymail.co.uk
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const cleanURL = window.location.protocol + "//" + window.location.host + window.location.pathname;

    if (window.location.search) {
        history.replaceState({}, document.title, cleanURL);
    }
})();

This will remove unnecessary query parameters from a website URL.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.