Large

Multiply - Theme Sticker

By Marti Last update Nov 4, 2011 — Installed 1,314 times.

There are 27 previous versions of this script.

(function () {

/*  ***************************************************************************
      User defined configurable variables
    ------------------------------------------------------------------------ */

  const alwaysSameBase = false;       // NOTE: Change this to true to force the same selected base theme on every users page.
  const alwaysSameCustom = false;     // NOTE: Change this to true to force the same selected use of custom CSS on every users page.
  const disableUserscriptCSS = false; // NOTE: Change this to true to eliminate CSS styling in this user script.


/*  ------------------------------------------------------------------------ */
//  ***************************************************************************

// ==UserScript==
// @name              Multiply - Theme Sticker
// @namespace         http://userscripts.org/users/37004
// @description       Toggles viewing of base theme and custom.css, and persists them across browser sessions.
// @copyright         2008+, Marti Martz (http://userscripts.org/users/37004)
// @license           GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @license           Creative Commons; http://creativecommons.org/licenses/by-nc-nd/3.0/
// @version           0.2.0
// @icon      http://s3.amazonaws.com/uso_ss/icon/42758/thumb.png
//
// @include   http://*.multiply.com/*
// @exclude   http://images.multiply.com/*
// @exclude   http://images.*.multiply.com/*
//
// @require   http://usocheckup.redirectme.net/42758.js?method=install&open=window&maxage=7&id=usoCheckup&custom=yes&topicid=23549
// @require   http://userscripts.org/scripts/source/61794.user.js
//
// ==/UserScript==

/*

CHANGELOG
=========
http://userscripts.org/topics/23549

*/


  //  ***************************************************************************
    function getNick () {
      let matches = location.href.match(/http:\/\/(.+).multiply.com\/.*/i);
      if (matches)
        return matches[1];
    }

  //  ***************************************************************************
    function getBase () {
      let xpr = document.evaluate(
        "//link[@rel='stylesheet'][@type='text/css'][contains(@href,'/style/custom/')]",
        document.documentElement,
        null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        null
      );

      if (xpr && xpr.singleNodeValue) {
        let thisNode = xpr.singleNodeValue;

        let matches = thisNode.href.match(/http:\/\/multiply.com\/style\/custom\/(.+)\/\d+(?:\/|\.css)/i);
        if (matches)
          return matches[1];
      }
      return undefined;
    }

  //  ***************************************************************************
    function getCustom () {
      let xpr = document.evaluate(
        "//link[@rel='stylesheet'][@type='text/css'][contains(@href,'/style-custom/')]",
        document.documentElement,
        null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        null
      );

      if (xpr && xpr.singleNodeValue) {
        let thisNode = xpr.singleNodeValue;

        let regex = new RegExp("http:\/\/" + userNick + "\.multiply\.com\/style\-custom\/" + userNick + "\/(.+)\/custom\.css", "i");
        return thisNode.href.match(regex)[1];

      }
      else {
        return "";
      }
    }

  //  ***************************************************************************
    function setIcon (thisNode, thisBase, thisImage) {
      if (!disableUserscriptCSS) {
        if (thisNode)
          switch (thisBase) {
            case "clean":
            case "default":
            case "whiteout":
              thisNode.style.background = "transparent url(" + thisImage + ") no-repeat scroll left top";
              break;
            default:
              thisNode.style.background = "";
              break;
          }
      }
    }

  //  ***************************************************************************
    function stickBase (ev) {
      let newBase;
      try {
        ev.stopPropagation();
        newBase = ev.target.value;
      }
      catch (e) {
        newBase = ev;
      }

      let xpr = document.evaluate(
        "//link[@rel='stylesheet'][@type='text/css'][contains(@href,'style/custom/')]",
        document.documentElement,
        null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        null
      );

      if (xpr && xpr.singleNodeValue) {
        let thisNode = xpr.singleNodeValue;

        let currentBase = thisNode.href.match(/http:\/\/multiply.com\/style\/custom\/(.+)\/\d+(?:\/|.css)/i)[1];
        if (currentBase != newBase) {
          if (newBase == "") {
            try {
              if (alwaysSameBase) GM_deleteValue("useBase"); else GM_deleteValue(userNick + ".useBase");
            }
            catch (e) {
              if (alwaysSameBase) GM_setValue("useBase", ""); else GM_setValue(userNick + ".useBase", "");
            }
            location.reload();
          }
          else {
            setIcon(document.getElementById("show-custom"), newBase, imageCustom);
            setIcon(document.getElementById("hide-custom"), newBase, imageCustom);
            setIcon(document.getElementById("view-base"), newBase, imageBase);

            if (alwaysSameBase)
              GM_setValue("useBase", newBase);
            else
              GM_setValue(userNick + ".useBase", newBase);

            thisNode.href = thisNode.href.replace(currentBase, newBase);
          }
        }
      }
    }

  //  ***************************************************************************
    function hideCSS (ev) {
      if (ev)
        ev.stopPropagation();

      let xpr = document.evaluate(
        "//link[@rel='stylesheet'][@type='text/css'][contains(@href,'/style-custom/')]",
        document.documentElement,
        null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        null
      );

      if (xpr && xpr.singleNodeValue) {
        let thisNode = xpr.singleNodeValue;

        thisNode.parentNode.removeChild(thisNode);

        let liNode;

        liNode = document.getElementById("show-custom");
        liNode.style.display = "list-item";

        liNode = document.getElementById("hide-custom");
        liNode.style.display = "none";

        if (alwaysSameCustom)
          GM_setValue("useCustom", false); else GM_setValue(userNick + ".useCustom", false);
      }
    }

  //  ***************************************************************************
    function showCSS (ev) {
      if (ev)
        ev.stopPropagation();

      try {
        if (alwaysSameCustom)
          GM_deleteValue("useCustom"); else GM_deleteValue(userNick + ".useCustom");

      }
      catch (e) {
        if (alwaysSameCustom)
          GM_setValue("useCustom", true); else GM_setValue(userNick + ".useCustom", true);
      }

      let xpr = document.evaluate(
        "//link[@rel='stylesheet'][@type='text/css'][contains(@href,'/custom/')]",
        document.documentElement,
        null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        null
      );

      if (xpr && xpr.singleNodeValue) {
        let thisNode = xpr.singleNodeValue;

        let linkNode = document.createElement("link");
        linkNode.rel = "stylesheet";
        linkNode.type = "text/css";
        linkNode.href = "http://" + userNick + ".multiply.com/style-custom/" + userNick + "/custom.css";
        thisNode.parentNode.insertBefore(linkNode, thisNode.nextSibling);

        let liNode;
        liNode = document.getElementById("show-custom");
        liNode.style.display = "none";

        liNode = document.getElementById("hide-custom");
        liNode.style.display = "list-item";

      } else {
        location.reload();
      }
    }

  //  ***************************************************************************
    function main () {

      let xpr = document.evaluate(
        "//div[@id='rail']/*/ul[@class='sidelist']",
        document.body,
        null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        null
      );

      if (xpr && !xpr.singleNodeValue) {
        document.evaluate(
          "//div[@id='rail']/*/div[@class='railsep']",
          document.body,
          null,
          XPathResult.ANY_UNORDERED_NODE_TYPE,
          xpr
        );

        if (xpr && xpr.singleNodeValue) {
          let thisNode = xpr.singleNodeValue.nextSibling;

          let ulNode = document.createElement("ul");
          ulNode.className = "sidelist";
          thisNode.parentNode.insertBefore(ulNode, thisNode);

          let brNode = document.createElement("br");
          thisNode.parentNode.insertBefore(brNode, thisNode);
         }
      }

      document.evaluate(
        "//link[@rel='stylesheet'][@type='text/css'][contains(@href,'/style-custom/')]",
        document.documentElement,
        null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        xpr
      );

      if (xpr && xpr.singleNodeValue) {

        document.evaluate(
          "//ul[@class='sidelist']",
          document.body,
          null,
          XPathResult.ANY_UNORDERED_NODE_TYPE,
          xpr
        );

        if (xpr && xpr.singleNodeValue) {
          let thisNode = xpr.singleNodeValue;

          let textNode = document.createTextNode("Show " + userNick + "'s custom theme");

          let aNode = document.createElement("a");
          aNode.href = "javascript:void(0);";
          aNode.addEventListener("click", showCSS, true);

          let liNode = document.createElement("li");
          liNode.id = "show-custom";
          liNode.className = "custom";
          liNode.style.display = "none";
          setIcon(liNode, userBase, imageCustom);

          aNode.appendChild(textNode);
          liNode.appendChild(aNode);
          thisNode.insertBefore(liNode, thisNode.firstChild);

          textNode = document.createTextNode("Hide " + userNick + "'s custom theme");

          aNode = document.createElement("a");
          aNode.href = "javascript:void(0);";
          aNode.addEventListener("click", hideCSS, true);

          liNode = document.createElement("li");
          liNode.id ="hide-custom";
          liNode.className = "custom";
          liNode.style.display = "list-item";
          setIcon(liNode, userBase, imageCustom);

          aNode.appendChild(textNode);
          liNode.appendChild(aNode);
          thisNode.insertBefore(liNode, thisNode.firstChild);
        }

        let customCSS = (alwaysSameCustom) ? GM_getValue("useCustom") : GM_getValue(userNick + ".useCustom");
        if (customCSS != undefined) {
          if (customCSS == false) {
            hideCSS();
          }
          else {
            try {
              if (alwaysSameCustom) GM_deleteValue("useCustom"); else GM_deleteValue(userNick + ".useCustom");
            } catch (e) {}
          }
        }
      }

      document.evaluate(
        "//ul[@class='sidelist']",
        document.body,
        null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        xpr
      );

      if (xpr && xpr.singleNodeValue) {
        let thisNode = xpr.singleNodeValue;

        liNode = document.createElement("li");
        liNode.id = "view-base";
        liNode.className = "vbase";
        setIcon(liNode, userBase, imageBase);

        let thatNode = document.getElementById("show-custom");
        if (thatNode)
          thisNode.insertBefore(liNode, thatNode.nextSibling);
        else
          thisNode.insertBefore(liNode, thisNode.firstChild);

        let spanNode = document.createElement("span");
        spanNode.className = "slisttxt";

        let textNode = document.createTextNode("View with");

        spanNode.appendChild(textNode);
        liNode.appendChild(spanNode);

        let brNode = document.createElement("br");
        brNode.className = "slisttxt";
        liNode.appendChild(brNode);

        let selectNode = document.createElement("select");
        selectNode.id = "stick-list";
        selectNode.className = "slist";
        selectNode.title = "Click to view with this base theme.";
        selectNode.style.width = "95%";
        selectNode.size = 1;
        selectNode.addEventListener("change", stickBase, true);
        liNode.appendChild(selectNode);

        let optionNode;
        for (let i = 0, len = themeBase.length; i < len; ++i) {
          optionNode = document.createElement("option");
          optionNode.value = themeBase[i][0];
          optionNode.text = themeBase[i][1];
          optionNode.title = themeBase[i][2];
          selectNode.appendChild(optionNode);
        }

        brNode = document.createElement("br");
        brNode.className = "slisttxt";
        liNode.appendChild(brNode);

        spanNode = document.createElement("span");
        spanNode.className = "slisttxt";

        textNode = document.createTextNode("base theme");

        spanNode.appendChild(textNode);
        liNode.appendChild(spanNode);

        let useBase = (alwaysSameBase) ? GM_getValue("useBase") : GM_getValue(userNick + ".useBase");

        if (useBase != undefined) {
          if (useBase != "") {
            stickBase(useBase);

            for (let i = 0, len = selectNode.length; i < len; ++i)
              if (selectNode.options[i].value == useBase) {
                selectNode.selectedIndex = i;
                  break;
              }

            if (useBase == userBase && !alwaysSameBase) {
              try {
                  GM_deleteValue(userNick + ".useBase");
                }
              catch (e) {
                GM_setValue(userNick + ".useBase", "");
              }
            }
          }
          else {
            try {
              if (alwaysSameBase) GM_deleteValue("useBase"); else GM_deleteValue(userNick + ".useBase");
            } catch (e) {}
          }
        }
      }
    }

    if (top.location != location)
      return;

    const userNick = getNick();
    const userBase = getBase();
    const userCustom = getCustom();

    const themeBase = [
      ["", userNick + "'s", "Use this to force a window reload"],
      ["default", "Andros", "[600]"],
      ["avlack", "Avlack", "[500]"],
      ["bacchus", "Bacchus", "[500]"],
      ["basecamp", "Basecamp (wide)", "[500]"],
      ["blacklily", "Black Lily (wide)", "[500]"],
      ["blocks", "Blocks", "[500]"],
      ["bloodyromance", "Bloody Romance", "[500]"],
      ["blackout", "Blackout (wide)", "{600]"],
      ["blueslate", "Blue Slate", "[500]"],
      ["bluesy", "Bluesy (wide)", "{600]"],
      ["classic", "Classic (wide)", "[500]"],
      ["clean", "Clean", "[600]"],
      ["dancewithme", "Dance With Me", "[500]"],
      ["darkwood", "Dark Wood (wide)", "{600]"],
      ["horizon", "Horizon", "{600]"],
      ["independence", "Independence", "[500]"],
      ["japanese", "Japanese Minimalist", "[500]"],
      ["livelovelaugh", "Live Love Laugh", "[500]"],
      ["melon", "Melon", "[400]"],
      ["mod", "MOD", "[500]"],
      ["mykonos", "Mykonos", "[400]"],
      ["nature", "Nature (wide)", "{600]"],
      ["newspaper", "Newspaper", "[400]"],
      ["notebook", "Notebook", "[500]"],
      ["oasis", "Oasis (wide)", "[500]"],
      ["outburst", "Outburst", "[500]"],
      ["pagoda", "Pagoda (wide)", "{600]"],
      ["petals", "Petals", "[400]"],
      ["pinkie", "Pinkie (wide)", "{600]"],
      ["peppermint", "Peppermint (wide)", "{600]"],
      ["redfrog", "Red Frog", "{400]"],
      ["retro", "Retro", "{400]"],
      ["sand", "Sandskrit", "[400]"],
      ["shadowbox", "Shadow Box", "[500]"],
      ["skyline", "Skyline (wide)", "[500]"],
      ["sunken", "Sunken (wide)", "{600]"],
      ["slate", "Slate (wide)", "{600]"],
      ["whiteout", "Whiteout", "[500]"]
    ];

    for (let i = 0, len = themeBase.length; i < len; ++i)
      if (themeBase[i][0] == userBase) {
        themeBase[i][1] += "*";
        themeBase[i][2] += "This is the user selected base theme";
          break;
      }

    const imageCustom = "data:image/png;base64,"
      + 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A'
      + '/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9kBBgUIIvqcmCwAAACfSURBVDiN'
      + 'Y2CgBph55sx/ZJw3adIxigxIbGx8OMQMaNq69T8yzps+/R3QoEJkHFFaKo7TgLSZM/8j49oNG/6j'
      + 'u0rDzKxwOBtgaS/0Hxn7Zwb8z5k/HwX37tsHCtiHMFw6Zw4isRkbM/xHxl4JLv/RXTX52DEUF6V3'
      + 'df0fVAbwPjE2FvwPw06Bdv9DKypQcMm0af8rZs2C47DcXIQBlAAAccRkJfSQ6RcAAAAASUVORK5C'
      + 'YII=';

    const imageBase = "data:image/png;base64,"
      + 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A'
      + '/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9kBBw8qDDTTMXAAAAItSURBVDjL'
      + 'rZNNTxNRFIbZY9zpghBNXBmWNpGf4EpKFLQgU1sslg5aQE0MLbVGPvoFqUZNjIkmfnXrb9DfYKPc'
      + 'O1WoLQZKOnPbTqfMR49npnQoiq64yc2s3ue+73vOdHUd9ckIYu9iTkoGqfT1JmV63h3J6sHUx92Z'
      + 'lcnqo5fH/itOCyLno6w2QCowRKvgwrvpmoMmH7euPpUoNmZXLxwqXhVEt4uw5mUUj+dqMLteh1C+'
      + 'DtujYWgGYjbE4OMaQgYPiN8J4ulxymRTzP+QYaGgwOttFTIlFcrXwmBMLoPRhuDXmEowOfSsxwZE'
      + 'BCk9sFaBG/jyYqEBH1AYLyow8b0GBS6CgBjobUig5USdTqVsQIBK1Mx8Z12GV1u7EEPxxU9f4MyY'
      + 'F06c74cnTg/o/mW8S/t93E4SG4D2jRGhCvN5BTI7mvXyWR8P3Y5z1j3ucMCW76EVxe7iVsKwAR7C'
      + 'DLPxMAJM+2aUvom/AaaLdgzsYR/gIxIdRsC9jTq8QUD0pwJDn7PQx3nhZD9GGPRaHegdDjDCmg24'
      + 'S6W0EydgWo9jiZkdFd3UwYtOCu4HLTHmNzrGqQY7SnxOyqdGCZOHaQWCWGSy2IC3OMb3JQ3KOIU/'
      + 'J2CNce5pz4FdCFPRc4lUmlcwih+d3Mc4C+imNDa/N39brCnTK85DtzFExOu4jTUzzlUEcbkq/BoJ'
      + 'da7ypjLzj1Vun8e03MsTKcUR9g1h+oYnmtVaP5OfRV90H/nf+xvLVeyxDdctIQAAAABJRU5ErkJg'
      + 'gg==';

    main();
})();