Greasy Fork

No Apple News

Remove Apple related entries in news sites

目前为 2022-03-26 提交的版本。查看 最新版本

// ==UserScript==
// @name        No Apple News
// @description Remove Apple related entries in news sites
// @version 0.1.1
// @grant none
// @author Lucie Cupcakes
// @namespace lucie-dev.xyz
// @license UNLICENSE - https://unlicense.org/
// @match *://news.ycombinator.com/
// @match *://news.ycombinator.com/news*
// @match *://lobste.rs/
// @match *://lobste.rs/page/*
// ==/UserScript==
(() => {
  const filterWords = [
    'apple',
    'mac',
    'iphone',
    'ipad',
    'ipod',
    'icloud',
    'ios',
    'siri',
    'm1',
    'macbook',
    'airbook',
    'macos',
    'safari'
  ];

  const filterWordsVar = [
    ...filterWords.map(el => `${el} `),
    ...filterWords.map(el => ` ${el}`)
  ]

  const domReady = (cb) => {
    if (document.readyState === "complete") {
      return cb();
    }
    window.addEventListener("DOMContentLoaded", cb);
  };

  const strIncludesAny = (str, findArray) => {
    if (!findArray || !str || findArray.length == 0 || str == "") {
      return false;
    }
    for (findEl of findArray) {
      if (str.includes(findEl)) {
        return true;
      }
    }
    return false;
  };

  const removeHtmlElement = el => el.parentElement.removeChild(el);

  const filterArticlesByHostname = {
    "news.ycombinator.com": () => {
      return Array.from(document.querySelectorAll(".title > a"))
        .map(el => {
          // Check if matches blocklist
          if (! strIncludesAny(el.innerText.toLowerCase(), filterWordsVar)) {
            return null;
          }
          // Grab metadata
          const articleTr = el.parentElement.parentElement;
          const id = articleTr.id.repeat(1);
          const title = el.innerText.repeat(1);
          const link = el.href.repeat(1);
          // Remove entry
          (() => {
            [
            document.querySelector(`.athing[id="${id}"]`), // articleTr
            document.querySelector(`.athing[id="${id}"] + tr`), //subtext
            document.querySelector(`.athing[id="${id}"] + tr + tr.spacer`)
            ].filter(el => el !== null).forEach(removeHtmlElement);
          })()
          // Return metadata
          return {
            title,
            link,
            id
          };
        }).filter(el => el !== null);
    },
    "lobste.rs": () => {
      return Array.from(document.querySelectorAll(".story .u-url"))
        .map(el => {
          // Check if matches blocklist
          if (! strIncludesAny(el.innerText.toLowerCase(), filterWordsVar)) {
            return null;
          }
          // Grab metadata
          const title = el.innerText.repeat(1);
          const link = el.href.repeat(1);
          // Remove entry
          (() => {
            const articleDiv = el.parentElement.parentElement.parentElement.parentElement
            removeHtmlElement(articleDiv)
          })()
          // Return metadata
          return {
            title,
            link
          };
        }).filter(el => el !== null);
    }
  };

  domReady(() => {
    const articles = filterArticlesByHostname[location.hostname]();
    console.log(`Filtered ${articles.length} article(s).`, articles);
  });
})();