Jump to content

User:Flatscan/rollbackToggle.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
const rollbackToggle_rblClass = "mw-rollback-link";

var rollbackToggle_CSS;
var rollbackToggle_hidden;

function rollbackToggle_init()
{
  // TODO: detect initial setting
  rollbackToggle_hidden = true;
}

function rollbackToggle_toggle()
{
  rollbackToggle_hidden = !rollbackToggle_hidden;
  var css = rollbackToggle_getCSS(rollbackToggle_hidden);

  if (rollbackToggle_CSS === undefined) {
    rollbackToggle_CSS = appendCSS(css);
  } else {
    // appendCSS, [[MediaWiki talk:Common.css/Archive 4#line-spacing with sub & sup]]
    if (rollbackToggle_CSS.styleSheet) {
      rollbackToggle_CSS.styleSheet.cssText = css; // IE
    } else {
      rollbackToggle_CSS.lastChild.nodeValue = css;
    }
  }
}

function rollbackToggle_getCSS(hide)
{
  return "." + rollbackToggle_rblClass +
         " { display: " +
         ( hide ? "none" : "inline" ) +
         "; }";
}

function rollbackToggle_hasRollbackLink()
{
  var spans = document.getElementsByTagName("span");
  for (var i = 0; i < spans.length; i++) {
    var span = spans[i];
    if (span.className == rollbackToggle_rblClass) {
      return true;
    }
  }
  return false;
}

function rollbackToggle_addLink()
{
  if (!rollbackToggle_hasRollbackLink()) { return; }
  rollbackToggle_init();
  mw.util.addPortletLink("p-cactions", "javascript:rollbackToggle_toggle()", "rb toggle", "ca-rollbackToggle", "Toggles rollback links");
}

addOnloadHook(rollbackToggle_addLink);