Difference between revisions of "MediaWiki:Common.js"

From Tinkla: Tinkering with Tesla
(Created page with "Any JavaScript here will be loaded for all users on every page load.: var bb_openpilot_container = document.getElementById("bb_openpilot.cfg"); if (bb_openpilot_contai...")
(No difference)

Revision as of 22:35, 27 November 2019

/* Any JavaScript here will be loaded for all users on every page load. */



var bb_openpilot_container = document.getElementById("bb_openpilot.cfg");
if (bb_openpilot_container)
  loadCfg(bb_openpilot_container);

function loadCfg(bb_openpilot_container) {
  var request = new XMLHttpRequest();
  var url = "https://raw.githubusercontent.com/BogGyver/openpilot/tesla_devel/selfdrive/car/tesla/readconfig.py";
  request.open('GET', url, true);
  request.send(null);
  request.onreadystatechange = function () {
    if (request.readyState === 4) {
        if (request.status === 200) {
        bb_openpilot_container.innerText = parseCfg(request.responseText);
      } else {
          bb_openpilot_container.innerText = "Error loading " + url;
      }
    }
  }
}

function parseCfg(code) {
  var cfgEntryPattern = /self\.read_config_entry\(([\s\S]*?)\r?\n?\s*?\)\r?\n\s*?file_changed/g;
  var paramSeparatorPattern = /\s*\r?\n?\s*,\s*\r?\n?\s*/;
  var cfg = "";
  var m;
  while (m = cfgEntryPattern.exec(code)) {
      var cfgParams = m[1].split(paramSeparatorPattern);
    var key = extractParamInfo(cfgParams[4]);
    var defaultValue = extractParamInfo(cfgParams[6]);
    var comment = extractParamInfo(cfgParams.splice(7, cfgParams.length).join(", "));
      cfg += "# " + comment + "\n" + key + " = " + defaultValue + "\n\n";
  }
  return cfg;
}

function extractParamInfo(rawParam) {
  var paramCleanupPattern = /(?:.*?=)?\s*?(.*?)\s*/;
  rawParam = rawParam.replace(/(["''])(.*)\1/, "$2"); // strip surrounding single or double quotes
  rawParam = rawParam.replace(/\\(['"])/, "$1") // unescape escaped quotes
  rawParam = rawParam.replace(paramCleanupPattern, "$1");
  return rawParam;
}