iamroose / POE Trade Fixes

// ==UserScript==
// @name          POE Trade Fixes
// @namespace     poetradefixes
// @version       2.0.1
// @description   Fixes for official POE Trade
// @author        iamroose
// @license       MIT
// @match         https://www.pathofexile.com/trade*
// @grant         none
// @require       https://raw.githubusercontent.com/tommoor/tinycon/master/tinycon.min.js
// @run-at        document-start
// ==/UserScript==

(function(){
  install();

  var is_focused = true;

  // Change the favicon
  function updateFavicon() {
    //document.querySelector("link[rel='icon']")?.remove();
    document.querySelector("link[rel*='icon']")?.remove();
    document.querySelector("link[rel='shortcut icon']")?.remove();
    document.querySelector("link[rel='mask-icon']")?.remove();
    var link = document.querySelector("link[rel='icon']") || document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'icon';
    link.sizes = '16x16';
    link.href = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAaSURBVDhPY3DTkSMJjWogBo1qIAYNOg06cgC39JABiSgUbwAAAABJRU5ErkJggg==';
    document.getElementsByTagName('head')[0].appendChild(link);
  }

  // Check live trade count and update favicon if necessary.
  function updateCount(title) {
    if(!is_focused){
      var count = title.match(/\d+/)[0];
      Tinycon.setBubble(count);
    }
  }

  // Add some styles
  function addStyles() {
    var css = "#trade {max-width:920px;} .search-advanced-items {display:flex; flex-direction:column; margin-bottom: 20px;} .search-advanced-pane {width:100% !important; margin-bottom:20px;}.results .resultset{margin-bottom:0;}";
    var style = document.createElement("style");
    style.type = "text/css";
    if (style.styleSheet){
      style.styleSheet.cssText = css;
    } else {
      style.appendChild(document.createTextNode(css));
    }

    document.documentElement.appendChild(style);
  }

  // Observe any change in the <title> content, checking for the unread notification character.
  function install() {
    updateFavicon();
    document.addEventListener('DOMContentLoaded', function(){
      addStyles();
    }, false);    
    var titleNode = document.querySelector('head > title');
    this.observer = new MutationObserver(function(mutations) {
      mutations.forEach(function(mutation) {
        var newTitle = mutation.target.textContent;
        updateCount(newTitle);
      });
    });
    observer.observe(titleNode, { subtree: true, characterData: true, childList: true });
  }

  window.addEventListener("focus", function(event)
  {
    is_focused = true;
    Tinycon.setBubble(0);
  }, false);

  window.addEventListener("blur", function(event)
  {
    is_focused = false;
  }, false);

  
})();