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 |
||
(One intermediate revision by the same user not shown) | |||
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. | + | bb_openpilot_container.innerHTML = parseCfg(request.responseText); |
} else { | } else { | ||
bb_openpilot_container.innerText = "Error loading " + url; | bb_openpilot_container.innerText = "Error loading " + url; | ||
Line 22: | Line 21: | ||
} | } | ||
} | } | ||
+ | |||
+ | // Tables with class=zebra | ||
+ | $(document).ready(function() { | ||
+ | // the "className" includes all the classes so we must use re; | ||
+ | // CSS is case-sensitive anyway, so there is no point ignoring case: | ||
+ | var re = /\bzebra\b/, | ||
+ | t = document.getElementsByTagName("TABLE"), | ||
+ | n = t.length, | ||
+ | r, nr; | ||
+ | for (var i=0; i<n; i++) { | ||
+ | if ( re.test(t[i].className)) { | ||
+ | r = t[i].getElementsByTagName("TR"); | ||
+ | nr = r.length; | ||
+ | for (var j=1; j < nr; j+=2) { | ||
+ | if (r[j].className) { r[j].className += " stripe";} | ||
+ | else r[j].className = "stripe"; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }); | ||
function parseCfg(code) { | function parseCfg(code) { | ||
Line 33: | Line 52: | ||
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 += "# | + | cfg += '<span class="c1"># ' + comment + "</span>\n" + key + " = " + defaultValue + "\n\n"; |
} | } | ||
return cfg; | return cfg; | ||
Line 39: | Line 58: | ||
function extractParamInfo(rawParam) { | 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/, "$2"); // strip surrounding single or double quotes | ||
rawParam = rawParam.replace(/\\(['"])/, "$1") // unescape escaped quotes | rawParam = rawParam.replace(/\\(['"])/, "$1") // unescape escaped quotes | ||
− | |||
return rawParam; | return rawParam; | ||
} | } |
Latest revision as of 12:55, 15 February 2022
/* 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;
}
}
}
}
// Tables with class=zebra
$(document).ready(function() {
// the "className" includes all the classes so we must use re;
// CSS is case-sensitive anyway, so there is no point ignoring case:
var re = /\bzebra\b/,
t = document.getElementsByTagName("TABLE"),
n = t.length,
r, nr;
for (var i=0; i<n; i++) {
if ( re.test(t[i].className)) {
r = t[i].getElementsByTagName("TR");
nr = r.length;
for (var j=1; j < nr; j+=2) {
if (r[j].className) { r[j].className += " stripe";}
else r[j].className = "stripe";
}
}
}
});
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;
}