VcSaJen / Namize Background Ponies!

// ==UserScript==
// @name        Namize Background Ponies!
// @namespace   http://derpiboo.ru/images/namize_bp
// @description Namize Background Ponies on Derpibooru!
// @icon        http://orig13.deviantart.net/d1b5/f/2017/079/6/7/derpy_by_theshadowartist100_by_vcsajen-db2xkv3.png
// @include     http://derpiboo.ru/*
// @include     http://www.derpiboo.ru/*
// @include     http://trixiebooru.org/*
// @include     http://www.trixiebooru.org/*
// @include     http://derpibooru.org/*
// @include     http://www.derpibooru.org/*
// @include     https://derpiboo.ru/*
// @include     https://www.derpiboo.ru/*
// @include     https://trixiebooru.org/*
// @include     https://www.trixiebooru.org/*
// @include     https://derpibooru.org/*
// @include     https://www.derpibooru.org/*
// @include     https://ronxgr5zb4dkwdpt.onion/*
// @include     https://www.ronxgr5zb4dkwdpt.onion/*
// @version     1.038
// @grant       none
// @require     https://openuserjs.org/src/libs/soufianesakhi/node-creation-observer.js
// ==/UserScript==

//Change this
//vvv

var colored = true;          //change this to false if you don't like colors
var fullNameColor = false;   //true -  full name will be coloured
                             //false - only marker will be coloured
var addBackgroundPony = true;//change this to false if you don't wish add " (Background Pony)" to the end of anon's "names"

//------------------------------------------------------------------------------------------------
//Do not change this (if you user. Programmers/scripters can change this)
//vvv

var names1 = ["","8-bit","Aero","Air","Almond","Alpha","Amaranthine","Amber","American","Annoying","Apple","Apricot","Aqua","Asian","Astral","Awesome","Bad","Beauty","Beige","Bell","Belle","Berry","Big","Bitter","Black","Blaze","Blazing","Blue","Bold","Bon","Bright","Brisk","Broken","Bronze","Brown","Candy","Caramel","Careless","Carrot","Charming","Cherry","Chilly","Chimmy","Choco","Chocolate","Chromatic","Chubby","Citric","Classic","Clean","Clear","Clever","Cloudy","Cocky","Coco","Cold","Cool","Copper","Corky","Correct","Cosmic","Cranky","Crazy","Curly","Cute","Cutie","Da’","Daring","Dark","Deadly","Delta","Derpy","Desert","Desired","Diamond","Dim","Dirty","Ditzy","Dizzy","DJ","Doctor","Double","Drama","Dusk","Eastern","Easy","Emerald","Empress","Epic","European","Fancy","Fantastic","Fast","Fat","Fifth","Filthy","First","Flash","Flirtatious","Fluffy","Flutter","Foggy","Fourth","Full","Funny","Fuzzy","Gamma","General","Gentle","Ginger","Glass","Glitter","Golden","Good","Gotta","Gray","Green","Half","Half Baked","Handsome","Happy","Hard","Hasty","Heavy","Hematite","High","Holly","Honey","Horny","Hot","Hyper","Ice","Igneous","Innocent","Iron","Ivory","Jasper","Large","Last","Lavender","Legendary","Lemon","Lemony","Liberty","Light","Lightning","Lilac","Little","Lone","Long","Lovely","Low","Lucky","Main","Malachite","Melo","Metal","Meteor","Mini","Misty","Mixed","Mud","Multicolored","My Little","Mysterious","Mythical","Navy","Neat","Neon","Night","Noisy","Northern","Nurse","Nyan","Old","Olive","Omega","Onyx","Orange","Over","Paper","Party","Peachy","Pear","Pearl","Perfect","Pink","Pinkie","Platinum","Plumy","Princess","Professor","Proud","Pure","Purple","Quartz","Queen","Quick","Rainbow","Rainy","Red","Rosy","Ruby","Ruff","Sad","Sapphire","Saucy","Sea","Second","Shadow","Sharp","Shining","Short","Shy","Silken","Silver","Simply","Slow","Smart","Smooth","Snappy","Sneaky","Soft","Sonic","Southern","Speedy","Spicy","Starry","Stella","Stellar","Stellate","Stinkin'","Stolid","Strict","Strong","Sugar","Sunny","Super","Sweet","Tasty","The Great and Powerful","Third","Thunder","Timid","True","Turquoise","Twilight","Under","Vanilla","Violet","Warning","Western","White","Windy","Winy","Yellow"];
var names2 = ["Angel","Apple","Apples","Armor","Armour","Assassin","Autumn","Barry","Beauty","Bee","Beetle","Bell","Belle","Berries","Blade","Blink","Blossom","Bomb","Bon","Bone","Bones","Book","Boom","Boomer","Bread","Brooch","Brook","Bubble","Bubbles","Bug","Bun","Bunny","Butterfly","Button","Cake","Candle","Candy","Cargo","Cat","Changa","Charge","Cherry","Cider","Class","Clearing","Cloud","Cola","Comet","Computer","Cook","Crasher","Crate","Cream","Creeper","Crown","Crush","Crusher","Cupcake","Cupcakes","Curse","Daiquiri","Dance","Dancer","Dash","Dasher","Dashy","Day","Days","Deal","Derp","Dessert","Devil","Dew","Diamond","Dog","Dovahkiin","Dream","Dress","Drop","Dubstep","Dust","Dusty","Earring","Eclair","Egg","Emerald","Envy","Eye","Eyes","Factory","Faith","Fall","Fear","Feather","Feathers","Fire","Flag","Flame","Flames","Flare","Floor","Flower","Flowers","Flyer","Folder","Forest","Fork","Frost","Glass","Ground","Halo","Harvest","Hawk","Hay","Haze","Head","Heart","Hills","Hoof","Hooves","Hope","Horn","Horns","Horseshoe","Horseshoes","Hunt","Hunter","Image","In Socks","Jam","Jazz","Journal","Kettle","Key","Kill","Kills","Knife","Knight","Ladle","Lady","Lake","Lamp","Leaf","Lemon","Lemonade","Light","List","Loaf","Love","Luna","Mane","Mess","Miner","Mint","Mints","Mist","Moon","Mouse","Muffin","Music","Necklace","Needle","News","Night","Nights","Noon","Note","Notes","Nova","Patty","Pepper","Picnic","Picture","Pie","Pirate","Pixel","Pony","Pop","Popper","Pride","Prism","Prod","Punk","Rainbow","Rat","Reaper","Rice","Ring","Rock","Roll","Romance","Rosette","Ruby","Runner","Saddle","Sapphire","Scapula","Shield","Shimmer","Shine","Shovel","Shower","Shy","Signal","Skies","Sky","Slapjack","Snap","Snow","Song","Soul","Sparkle","Sparky","Spawn","Sphere","Spider","Spin","Spirit","Spoon","Spring","Stairs","Star","Stars","Stool","Strike","String","Strings","Stripe","Stripes","Stuff","Summer","Sun","Swirl","Sword","Swords","Table","Tart","Tiara","Time","Times","Top","Tree","Trees","Trixie","Tron","Twist","Vise","Water","Waterfall","Wheat","Wind","Window","Wing","Wings","Winter","Wolf"];

textNodes = document.evaluate( 
    ".//a | .//strong | .//div[@class='metasection'] | .//div[contains(concat(' ', normalize-space(@class), ' '), ' post-author ')] | .//div[contains(concat(' ', normalize-space(@class), ' '), ' communication__body ')] | .//div[@class='small'] | .//div[@class='small-text'] | .//span[@class='block__header__title'] | .//td[@class='topic-lastpost'] | .//td[@class='table--communication-list__last-post'] | .//div[@id='duplicate_reporting']//td[@class='center image-cell'] | .//div[@class='block__content flex communication__options']/div[@class='flex__right'] | .//span[@class='spacing-right'] | .//main[@id='content']/h1 | .//div[@class='block__content alternating-color'] | .//div[@class='block__content flex alternating-color']/div[@class='flex__grow'] | .//td[@class='blank']", document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
//var searchREF = new RegExp('Background Pony #([0-9A-F]{4})','g'); 
var searchRER = new RegExp('Background Pony #([0-9A-F]{2})([0-9A-F]{2})',''); 
/*var searchRER = new RegExp('Background Pony','gi');*/
var replace = 'pling'; 

//if (window.jQuery) alert('jQuery is here'); else alert('jQuery is not here');

function getBPName(re, match, overrideNoColors)
{
    if(typeof(overrideNoColors)==='undefined') overrideNoColors=colored;

    var index1 = parseInt(match[1],16);
    var index2 = parseInt(match[2],16);
    var replace=names1[index1];
    
    if (replace!='') replace+=' ';
    replace+=names2[index2];
    var span_open='<span title="'+match[1]+match[2]+'" style="color: '+getRGBstr(match[1]+match[2])+';">';
    if (overrideNoColors)
    {
        if (fullNameColor) replace=span_open+replace+'</span>';
        else replace+=span_open+' •</span>';
    }
    else replace='<span title="'+match[1]+match[2]+'">'+replace+'</span>';
    if (addBackgroundPony) replace+=" (Background Pony)";
    re=re.replace(searchRER, replace);
    return re;
}


for (var i=0;i<textNodes.snapshotLength;i++) 
{ 
    var node = textNodes.snapshotItem(i); 
    
    processVanillaJSNode(node);
}

function processVanillaJSNode(node)
{
    var childs = node.childNodes, i = 0;
    var skip;
    
    while(true){ 
        var chnode = childs[i];
        if (chnode===null) break;
        if (chnode===undefined) break;
        if (skip>0) 
        {
            i++;
            skip--;
            continue;
        }

        if (chnode.nodeType == 3){ // text node found, do the replacement
            //alert(getMethods(chnode));
            //alert(getProperties(chnode));
            var re = chnode.data;
            
            var match = re.match(searchRER);
            
            if (match!==null)
            {
                re=getBPName(safe_tags_replace(re), match);
                chnode.data=re;
                skip = wrapTextNode(chnode)-1;
            }
        }
        i++; 
    }
}

NodeCreationObserver.onCreation("div.comment_info > strong, div.comment_body a, div.post-prevue a, span.post-author, div.post-author, span.communication__body__sender-name, div.communication__body__sender-name, div.post-text a, div.communication__body__text a", get_span_content);

function getRGBstr(color16bit)
{
    var pixel = parseInt(color16bit,16);
    
    var red_mask = 0xF800;
    var green_mask = 0x7E0;
    var blue_mask = 0x1F;
    
    var red_value = (pixel & red_mask) >> 11;
    var green_value = (pixel & green_mask) >> 5;
    var blue_value = (pixel & blue_mask);
    
    // Expand to 8-bit values.
    var red   = red_value << 3;
    var green = green_value << 2;
    var blue  = blue_value << 3;
    
    return 'rgb('+red+','+green+','+blue+')';
}

function get_span_content (node) 
{
    //alert('It works!');
    
    processVanillaJSNode(node)

    return true;
}

function insertAfterThis(someElement, newElement) {
    someElement.parentNode.insertBefore(newElement, someElement.nextSibling);
}

function wrapTextNode(textNode) {
  var spanNode = document.createElement('span');
  spanNode.innerHTML = textNode.textContent;
  //alert(spanNode.outerHTML);
  
  var childs = spanNode.childNodes, i = 0;
  //alert(JSON.stringify(childs));
  //alert(childs.length);

  var lastNode = textNode;
  while(true){
    //alert("n="+childs.length);
    var chnode = childs[0];
    //if (chnode===null) break;
    if (chnode===undefined) break;
    //alert(chnode.data);
    insertAfterThis(lastNode, chnode);
    lastNode = chnode;
    i++; 
  }
  textNode.parentNode.removeChild(textNode);
  return i;
}

var tagsToReplace = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;'
};

function replaceTag(tag) {
    return tagsToReplace[tag] || tag;
}

function safe_tags_replace(str) {
    return str.replace(/[&<>]/g, replaceTag);
}

/**/