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...")
 
m
Line 1: Line 1:
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
  
  
Line 15: Line 14:
 
     if (request.readyState === 4) {
 
     if (request.readyState === 4) {
 
         if (request.status === 200) {
 
         if (request.status === 200) {
         bb_openpilot_container.innerText = parseCfg(request.responseText);
+
         bb_openpilot_container.innerHTML = parseCfg(request.responseText);
 
       } else {
 
       } else {
 
           bb_openpilot_container.innerText = "Error loading " + url;
 
           bb_openpilot_container.innerText = "Error loading " + url;
Line 33: Line 32:
 
     var defaultValue = extractParamInfo(cfgParams[6]);
 
     var defaultValue = extractParamInfo(cfgParams[6]);
 
     var comment = extractParamInfo(cfgParams.splice(7, cfgParams.length).join(", "));
 
     var comment = extractParamInfo(cfgParams.splice(7, cfgParams.length).join(", "));
       cfg += "# " + comment + "\n" + key + " = " + defaultValue + "\n\n";
+
       cfg += '<span class="c1"># ' + comment + "</span>\n" + key + " = " + defaultValue + "\n\n";
 
   }
 
   }
 
   return cfg;
 
   return cfg;
Line 39: Line 38:
  
 
function extractParamInfo(rawParam) {
 
function extractParamInfo(rawParam) {
   var paramCleanupPattern = /(?:.*?=)?\s*?(.*?)\s*/;
+
   rawParam = rawParam.replace(/(?:.*?=)?\s*?(.*?)\s*/, "$1"); // strip param variable name if present
 
   rawParam = rawParam.replace(/(["''])(.*)\1/, "$2"); // strip surrounding single or double quotes
 
   rawParam = rawParam.replace(/(["''])(.*)\1/, "$2"); // strip surrounding single or double quotes
 
   rawParam = rawParam.replace(/\\(['"])/, "$1") // unescape escaped quotes
 
   rawParam = rawParam.replace(/\\(['"])/, "$1") // unescape escaped quotes
  rawParam = rawParam.replace(paramCleanupPattern, "$1");
 
 
   return rawParam;
 
   return rawParam;
 
}
 
}

Revision as of 13:40, 28 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.innerHTML = 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 += '<span class="c1"># ' + comment + "</span>\n" + key + " = " + defaultValue + "\n\n";
  }
  return cfg;
}

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