Guest User

TamperMonkey script to hide ChatGPT related posts on HN

a guest
Apr 21st, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Hide AI and GPT posts
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Hides ChatGPT related posts from HackerNews. If you can't see AI it can't take your job.
  6. // @author       https://news.ycombinator.com/user?id=quiet_light
  7. // @match        https://news.ycombinator.com/*
  8. // @icon         https://www.google.com/s2/favicons?sz=64&domain=ycombinator.com
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     window.onload = function () {
  15.         // Regular expression to match the keywords "AI" or "GPT"
  16.         const keywordRegex = /\b(AI|GPT)\b/i;
  17.  
  18.         // Find all the submissions on the page
  19.         const submissions = document.querySelectorAll(".athing");
  20.         const filteredLinks = [];
  21.         const isHomePage = window.location.href.match("https://news.ycombinator.com/$") || window.location.href.match("https://news.ycombinator.com/news/*");
  22.         if (!isHomePage) {
  23.             return;
  24.         }
  25.         // Loop through the submissions
  26.         for (let i = 0; i < submissions.length; i++) {
  27.             const submission = submissions[i];
  28.  
  29.             // Find the submission title and metadata(author, points, etc) elements
  30.             const title = submission.querySelector(".title a");
  31.             const submissionMetaData = submission.nextElementSibling;
  32.  
  33.             // Check if the submission title contains the keywords
  34.             const shouldRemove = (title && keywordRegex.test(title.textContent))
  35.             if (shouldRemove) {
  36.                 const link = {
  37.                     title: title ? title.textContent.trim() : "",
  38.                     url: 'https://news.ycombinator.com/item?id=' + submission.getAttribute('id')
  39.                 };
  40.                 filteredLinks.push(link);
  41.                 submission.parentNode.removeChild(submission);
  42.                 submissionMetaData.parentNode.removeChild(submissionMetaData);
  43.             }
  44.         }
  45.  
  46.         console.log(filteredLinks);
  47.     };
  48.  
  49. })();
Advertisement
Add Comment
Please, Sign In to add comment