Settings

Theme

Show HN: I used ChatGPT to write a UserScript that removes all mentions of *GPT

1 points by skarz 3 years ago · 0 comments · 2 min read


I have little to no coding experience, just basic HTML and CSS. This was generated entirely by ChatGPT with me giving the prompts. It took some trial and error but we got there:

// ==UserScript== // @name Block GPT Posts on Hacker News // @namespace http://tampermonkey.net/ // @version 0.1 // @description Block posts containing the term "GPT" on news.ycombinator.com and renumber the remaining posts // @author You // @match https://news.ycombinator.com/* // @grant none // ==/UserScript==

(function() { 'use strict';

    function getStartingNumber() {
        const firstRankSpan = document.querySelector('span.rank');
        if (firstRankSpan) {
            const number = parseInt(firstRankSpan.textContent, 10);
            return isNaN(number) ? 1 : number;
        }
        return 1;
    }

    function blockGPTPosts() {
        const searchTerm = /GPT/i;
        const titleLines = document.querySelectorAll('span.titleline');
        const rankSpans = document.querySelectorAll('span.rank');
        const startingNumber = getStartingNumber();

        let visiblePosts = [];

        titleLines.forEach((titleLine, index) => {
            const postLink = titleLine.querySelector('a');
            if (postLink && searchTerm.test(postLink.textContent)) {
                const athingRow = titleLine.closest('tr.athing');
                if (athingRow) {
                    athingRow.style.display = 'none';
                    const subtextRow = athingRow.nextElementSibling;
                    if (subtextRow && subtextRow.querySelector('td.subtext')) {
                        subtextRow.style.display = 'none';
                    }
                }
            } else {
                visiblePosts.push(index);
            }
        });

        visiblePosts.forEach((visiblePostIndex, index) => {
            const postNumber = startingNumber + index;
            if (rankSpans[visiblePostIndex]) {
                rankSpans[visiblePostIndex].textContent = `${postNumber}.`;
            }
        });
    }

    // Wait for the content to load before executing the function
    window.addEventListener('load', blockGPTPosts);
})();

No comments yet.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection