danuker / Keyword Highlighter

// ==UserScript==
// @name         Keyword Highlighter
// @description  Keywords Highlight remade by danuker for permanent keywords
// @namespace    https://openuserjs.org/users/danuker
// @version      1.0
// @author       danuker
// @icon         https://danuker.go.ro/favicon.ico
// @match         *://*/*
// @require      http://code.jquery.com/jquery-latest.js
// @license      MIT
// ==/UserScript==

// Provided under the MIT license by Dan Gheorghe Haiduc (https://danuker.go.ro/)

// Forked from: https://openuserjs.org/scripts/andrew.y0ufacebook.com/Keywords_Highlight/source
// Tested with jQuery v1.11.1; would love to remove dependency

// Instructions: Whatever you want highlighted, add to the to_highlight constant.
// Whatever is separated by spaces, tabs, or newlines gets treated as separate.

const to_highlight = `
scammers_on_reddit.com/r/bitmarket:
 
steve51184 ghettorissetto MMMM_BAKED_BABIES MarioLovesYou Area51Brett Coins4Ever
slimos28 yyhaoyue1 VadorTheMiner ScaredRush Bestes0910 MagicMikeWIns ChristinaHerrera099
christophigis Loganlord OsamaBin_Laggin PestiCsaba 1CPa2ZPw2CEmbaJmAexDZkPtHTYHisrtsj
420tieler XeiEUNE Snoozletoes FatOtis NearbyOlive Wildwest095 poetryandnafta
Berzerk4Dayz ARandomAnomaly merishone Acelittylit jfjenson ElwinRolfson


some_opinionated_highlights_follow:

medium.com quora.com linkedin.com loginwalled-gardens-to-avoid

files.catbox.moe crappy-video-commentaries

nullc https://www.reddit.com/r/btc/comments/jb30tk/greg_maxwell_engaging_in_gaslighting_and/

bsdlinuxnerd Damn_Stallman,_you're_right_on_this_one._I_just_hate_the_fact_that_you_defended_Epstein. https://www.reddit.com/r/StallmanWasRight/comments/jpzgng/a_prerecorded_message_from_richard_stallman_on/gblk24b/

Kruug r/linux subreddit_admin_advocating_proprietary_drivers https://www.reddit.com/r/linux/comments/54gtpc/letter_to_the_federal_trade_commission_regarding/d82txvd/

no_brainwash vim_microsoft_LSP_shill https://www.reddit.com/r/vim/comments/i46lyp/to_vim_from_emacstrying_to_set_up_vim_for_python/g0kos71/

TheAllNewBigGay made-people-self-dox https://www.reddit.com/r/AskReddit/comments/ki7v4r/whats_the_craziest_crime_you_or_somebody_in_your/

`.split(/[ \n\t]+/)

var mutated = true;

MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

var observer = new MutationObserver(function (mutations, observer) {
  // fired when a mutation occurs
  //console.log('mutated');
  mutated = true;
});

// define what element should be observed by the observer
// and what types of mutations trigger the callback
observer.observe(document, {
  subtree: true,
  attributes: true
});

$(document).ready(function () {

  $('head').append('<style type="text/css">.highlightKeywords{background: CornflowerBlue; color: #fff;}</style>');

  const hl = function () {
    if (mutated) {
      //console.log('hl');
      $('body').highlight(to_highlight);
      mutated = false;
    }
  };

  hl();
  var t = setInterval(hl, 1000);
});

$.extend({
  highlight: function (node, re, nodeName, className) {
    if (node.nodeType === 3) {
      var match = node.data.match(re);
      if (match) {
        var highlight = document.createElement(nodeName || 'span');
        highlight.className = className || 'highlightKeywords';
        var wordNode = node.splitText(match.index);
        wordNode.splitText(match[0].length);
        var wordClone = wordNode.cloneNode(true);
        highlight.appendChild(wordClone);
        wordNode.parentNode.replaceChild(highlight, wordNode);
        return 1; //skip added node in parent
      }
    }
    else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
      !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
      !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
      for (var i = 0; i < node.childNodes.length; i++) {
        i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
      }
    }
    return 0;
  }
});

$.fn.highlight = function (words, options) {
  var settings = {
    className: 'highlightKeywords',
    element: 'span',
    caseSensitive: false,
    wordsOnly: false
  };
  jQuery.extend(settings, options);
  if (words.constructor === String) {
    words = [words];
  }
  words = jQuery.grep(words, function (word, i) {
    return word != '';
  });
  words = jQuery.map(words, function (word, i) {
    return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  });
  if (words.length == 0) {
    return this;
  };

  var flag = settings.caseSensitive ? "" : "i";
  var pattern = "(" + words.join("|") + ")";
  if (settings.wordsOnly) {
    pattern = "\\b" + pattern + "\\b";
  }
  var re = new RegExp(pattern, flag);
  return this.each(function () {
    jQuery.highlight(this, re, settings.element, settings.className);
  });
};