houtianze / SCB LMI: HK->TW Hack

// ==UserScript==
// @namespace     https://openuserjs.org/users/houtianze
// @author        Hou Tianze
// @name          SCB LMI: HK->TW Hack
// @description   SCB LMI: Hack HK website to look like expected TW one
// @copyright     2017, houtianze (https://openuserjs.org/users/houtianze)
// @license       MIT
// @version       0.1.0
// @include       *://www.sc.com/retail/mce/*
// @grant none
// ==/UserScript==

// ==OpenUserJS==
// @author houtianze
// ==/OpenUserJS==

(function () {
  'use strict';

  const HilightDiv = 'style="padding: 2px; border-width: 2px; border-style: solid; border-color: red; "';

  function addHref(text) {
    return '<a ' + HilightDiv + ' ' + 'href="https://www.sc.com/tw"' + '>' + text + '</a>';
  }

  function hilight(text) {
    return '<div ' + HilightDiv + '>' + text + '</div>';
  }

  function getNode(xpath) {
    return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  }

  function getNodes(xpath) {
    return document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
  }

  function changeNode(node, value, isText) {
    if (node !== null) {
      if (isText) {
        node.innerText = value;
      }
      else {
        node.innerHTML = value;
      }
    }
  }

  function change(xpath, value, isText) {
    var node = getNode(xpath);
    changeNode(node, value);
  }

  function changeAll(xpath, value, isText) {
    var xpr = getNodes(xpath);
    var nodes = [];
    while (true) {
      var node = xpr.iterateNext();
      if (!node) {
        break;
      }
      nodes.push(node);
    }
    nodes.forEach((node) => {
      changeNode(node, value, isText);
    });
  }

  function main() {
    var overweight = '看多';
    var neutral = '中立';
    var underweight = '看淡';
    var labels = [overweight, underweight, neutral];
    for (var i = 0; i < 3; i++) {
      change('//*[@class="c-legend"]/div/div[1]/div[' + (i + 1) + ']/span[2]', hilight(labels[i]));
    }
    changeAll('//span[contains(@class, "overweight") and contains(@class, "text")]', hilight(overweight));
    changeAll('//span[contains(@class, "underweight") and contains(@class, "text")]', hilight(underweight));
    changeAll('//span[contains(@class, "neutral") and contains(@class, "text")]', hilight(neutral));
    changeAll('//*[text()="澳元"]', hilight('澳幣'));
    var node = getNode('/html/body/div//ul[text()="主頁"]');
    changeNode(node, addHref('主頁'));
  }

  main();
})();