Ahab / faction armoury bonus filter

// ==UserScript==
// @name         faction armoury bonus filter
// @namespace    torn.com
// @version      1.1
// @author       Ahab [1735214]
// @license      MIT
// @updateURL    https://openuserjs.org/meta/Ahab/faction_armoury_bonus_filter.meta.js
// @match        *www.torn.com/factions.php*
// @grant        GM_addStyle
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @require      https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// ==/UserScript==

GM_addStyle(`
.bonusButton{
  font-family: fjalla one,Arial,serif;
  font-size: 14px;
  text-align: center;
  border-radius: 5px;
  padding: 5px 10px;
  cursor: pointer;
  color: var(--btn-color);
  text-shadow: var(--btn-text-shadow);
  background: var(--btn-background);
  border: var(--btn-border);
  display: inline-block;
  vertical-align: middle;
}
.bonusButton:hover{
  background: var(--btn-hover-background);
}
`)

const bonusWeapons = {'Blindfire':'blindfire','Burn':'burn','Demoralized':'demoralized','Emasculate':'emasculate','Freeze':'freeze','Hazardous':'hazardous','Laceration':'laceration','Poisoned':'poisoned','Shock':'shock','Smash':'smash','Spray':'spray','Storage':'storage','Toxin':'toxin','Achilles':'achilles','Assassinate':'assassinate','Backstab':'backstab','Berserk':'berserk','Bleed':'bleed','Blindside':'blindside','Bloodlust':'bloodlust','Comeback':'comeback','Conserve':'conserve','Cripple':'cripple','Crusher':'crusher','Cupid':'cupid','Deadeye':'deadeye','Deadly':'deadly','Disarm':'disarm','Double-edged':'doubleedged','Double Tap':'fury','Empower':'empower','Eviscerate':'eviscerate','Execute':'execute','Expose':'expose','Finale':'finale','Focus':'focus','Frenzy':'frenzy','Fury':'fury','Grace':'grace','Home Run':'homerun','Irradiate':'irradiate','Motivation':'motivation','Paralyzed':'paralyzed','Parry':'parry','Penetrate':'penetrate','Plunder':'plunder','Powerful':'powerful','Proficience':'proficience','Puncture':'puncture','Quicken':'quicken','Rage':'rage','Revitalize':'revitalize','Roshambo':'roshambo','Slow':'slow','Smurf':'smurf','Specialist':'specialist','Stricken':'stricken','Stun':'stun','Suppress':'suppress','Sure Shot':'sureshot','Throttle':'throttle','Warlord':'warlord','Weaken':'weaken','Wind-up':'windup','Wither':'wither'}
const bonusArmours = {'Impenetrable':'bullets-protection','Impregnable':'melee-protection','Insurmountable':'quarter-life-damage-mitigation','Invulnerable':'negative-status_mitigation','Imperviable':'life-bonus','Immutable':'sentinel','Irrepressible':'vanguard','Impassable':'full-block'}
const listen = $(document).on('mouseup', '#bonus', function(event) {
    if(selected == event.target.attributes[2].nodeValue){
        $(event.target).css('background','')
        selected = ""
        buttonAction('same')
    }else{
        selected = event.target.attributes[2].nodeValue
        $("button[id*='bonus']").filter(":not("+selected+")").css('background','')
        $(event.target).css('background','var(--btn-hover-background)')
        buttonAction()
    }
})

var tempRow = $("<div class='bonusCont'></div>")
var selected = ""

function buttonAction(action){
    if(action == 'same'){
        if(window.location.href.includes('factions')){
            $("li[class*='bonus'] :first-child").parent().parent().parent().show()
        }else{
            $("span[class='bonus-attachment-icons'] :first-child").parent().parent().parent().parent().parent().show()
        }
    }else{
        if(window.location.href.includes('factions')){
            $("li[class*='bonus'] :first-child").filter(":not('.bonus-attachment-"+selected+"')").parent().parent().parent().hide();
            $("li[class*='bonus'] i[class*='bonus-attachment-"+selected+"']").parent().parent().parent().show();
        }else{
            $("span[class='bonus-attachment-icons'] :first-child").filter(":not('.bonus-attachment-"+selected+"')").parent().parent().parent().parent().parent().hide();
            $("span[class='bonus-attachment-icons'] i[class*='bonus-attachment-"+selected+"']").parent().parent().parent().parent().parent().show();
        }
    }
}

function init(){
    var target = ""
    if(window.location.href.includes('factions')){
        target = $('div[id*="faction-armoury-tabs"]')[0];
    }else{
        target = $('div[id*="auction-house-tabs"]')[0];
    }
    var config = { attributes: false, childList: true, characterData: false, subtree: true };
    var tab = new MutationObserver(function(mutations) {
        $.each(mutations, function(i,v){
            if(mutations[i].target.className.includes('items60x30') && mutations[i].addedNodes.length > 1 || mutations[i].target.className.indexOf('items-list') > -1 && mutations[i].addedNodes.length > 1 && mutations[i].target.className.indexOf('time') == -1){
                $("div[class*='bonusCont']").remove()
                $(document).off('mouseup', listen);
                load()
                tab.disconnect();
            }
        })
    })
    tab.observe(target, config);
    load()
}

function load(){
    if($("div[class*='bonusCont']").length == 0 && window.location.href.includes('weapons')){
        if(window.location.href.includes('factions')){
            add(bonusWeapons)
        }else{
           $("div#types-tab-1 img[src*='/images/items']").load(function(){
               $("div#types-tab-1 img[src*='/images/items']").off('load')
               add(bonusWeapons)
            }).each(function(){
                if(this.complete) {
                    $(this).trigger('load');
                }
            });
        }
    }else if($("div[class*='bonusCont']").length == 0 && window.location.href.includes('armour') || $("div[class*='bonusCont']").length == 0 && window.location.href.includes('armor')){
        if(window.location.href.includes('factions')){
            add(bonusArmours)
        }else{
            $("div#types-tab-2 img[src*='/images/items']").load(function(){
                $("div#types-tab-2 img[src*='/images/items']").off('load')
                add(bonusArmours)
            }).each(function(){
                if(this.complete) {
                    $(this).trigger('load');
                }
            });
        }
    }
}

function add(itemCat){
    tempRow = $("<div class='bonusCont'></div>")
    $.each(Object.keys(itemCat).sort(), function(i,v){
        if($("li[class*='bonus'] i[class*='bonus-attachment-"+itemCat[v]+"']").length > 0 || $("div[class*='iconsbonuses'] span[class*='bonus'] i[class*='bonus-attachment-"+itemCat[v]+"']").length > 0){
            tempRow.append("<button id='bonus' class='bonusButton' name='"+itemCat[v]+"'>"+v+"</button>")
        }
    })
    $(tempRow).insertAfter($("hr[class*='delimiter-999 m-top10']"))
    if($("button[name*='"+selected+"']").length > 0){
        $("button[name*='"+selected+"']").css('background','var(--btn-hover-background)')
        buttonAction()
    }
    listen
}

if(window.location.href.includes('factions')){
    waitForKeyElements("ul.item-list .last", init);
}else{
    waitForKeyElements("div .pagination-l", init);
}