Use camel case in JS

This commit is contained in:
Adrian Vollmer 2024-04-21 16:23:03 +02:00
parent ac664c27a1
commit 2e8a3ee989
5 changed files with 126 additions and 126 deletions

View File

@ -11,10 +11,10 @@ var _base64ToArrayBuffer = function (base64) {
// Set up the virtual file tree
var GC = window.global_context;
var GC = window.globalContext;
GC = _base64ToArrayBuffer(GC);
GC = pako.inflate(GC);
GC = new TextDecoder("utf-8").decode(GC);
GC = JSON.parse(GC);
window.global_context = GC;
eval(window.global_context.main);
window.globalContext = GC;
eval(window.globalContext.main);

View File

@ -1,16 +1,16 @@
var split_url = function(url) {
var splitUrl = function(url) {
// Return a list of three elements: path, GET parameters, anchor
var anchor = url.split('#')[1] || "";
var get_parameters = url.split('#')[0].split('?')[1] || "";
var getParameters = url.split('#')[0].split('?')[1] || "";
var path = url.split('#')[0];
path = path.split('?')[0];
let result = [path, get_parameters, anchor];
let result = [path, getParameters, anchor];
// console.log("Split URL", url, result);
return result;
}
var virtual_click = function(evnt) {
var virtualClick = function(evnt) {
// Handle GET parameters and anchors
// console.log("Virtual click", evnt);
@ -18,22 +18,22 @@ var virtual_click = function(evnt) {
var name = el.tagName.toLowerCase();
if (name == 'a') {
var [path, get_parameters, anchor] = split_url(el.getAttribute('href'));
var [path, getParameters, anchor] = splitUrl(el.getAttribute('href'));
} else if (name == 'form') {
var [path, get_parameters, anchor] = split_url(el.getAttribute('action'));
var [path, getParameters, anchor] = splitUrl(el.getAttribute('action'));
const formData = new FormData(el);
get_parameters = new URLSearchParams(formData).toString();
getParameters = new URLSearchParams(formData).toString();
} else {
console.error("Invalid element", el);
}
path = normalize_path(path);
path = normalizePath(path);
window.parent.postMessage({
action: "virtual_click",
action: "virtualClick",
argument: {
path: path,
get_parameters: get_parameters,
getParameters: getParameters,
anchor: anchor,
}
}, '*');
@ -42,7 +42,7 @@ var virtual_click = function(evnt) {
return false;
};
var is_virtual = function(url) {
var isVirtual = function(url) {
// Return true if the url should be retrieved from the virtual file tree
var _url = url.toString().toLowerCase();
return (! (
@ -56,10 +56,10 @@ var is_virtual = function(url) {
));
};
var retrieve_file = function(path) {
var retrieveFile = function(path) {
// console.log("Retrieving file: " + path);
var file_tree = window.global_context.file_tree;
var file = file_tree[path];
var fileTree = window.globalContext.fileTree;
var file = fileTree[path];
if (!file) {
console.warn("File not found: " + path);
return "";
@ -68,14 +68,14 @@ var retrieve_file = function(path) {
}
};
var normalize_path = function(path) {
var normalizePath = function(path) {
// make relative paths absolute in context of our virtual file tree
while (path && path[0] == '/') {
path = path.substr(1);
}
var result = window.global_context.current_path;
var result = window.globalContext.current_path;
result = result.split('/');
result.pop();
result = result.concat(path.split('/'));
@ -94,14 +94,14 @@ var normalize_path = function(path) {
});
result = array.join('/');
// console.log(`Normalized path: ${path} -> ${result} (@${window.global_context.current_path})`);
// console.log(`Normalized path: ${path} -> ${result} (@${window.globalContext.current_path})`);
return result;
};
var on_set_data = function(argument) {
// window.global_context = argument;
console.debug("Received data from parent", window.global_context);
var onSetData = function(argument) {
// window.globalContext = argument;
console.debug("Received data from parent", window.globalContext);
try {
// window.document.dispatchEvent(new Event("DOMContentLoaded", { bubbles: true, cancelable: true }));
} finally {
@ -128,10 +128,10 @@ var on_set_data = function(argument) {
}
var fix_link = function(a) {
if (is_virtual(a.getAttribute('href'))) {
// a.addEventListener('click', virtual_click);
a.setAttribute("onclick", "virtual_click(event)");
var fixLink = function(a) {
if (isVirtual(a.getAttribute('href'))) {
// a.addEventListener('click', virtualClick);
a.setAttribute("onclick", "virtualClick(event)");
} else if (a.getAttribute('href').startsWith('#')) {
a.setAttribute('href', "about:srcdoc" + a.getAttribute('href'))
} else if (!a.getAttribute('href').startsWith('about:srcdoc')) {
@ -142,22 +142,22 @@ var fix_link = function(a) {
};
var fix_form = function(form) {
var fixForm = function(form) {
var href = form.getAttribute('action');
if (is_virtual(href) && form.getAttribute('method').toLowerCase() == 'get') {
// form.addEventListener('submit', virtual_click);
form.setAttribute("onsubmit", "virtual_click(event)");
if (isVirtual(href) && form.getAttribute('method').toLowerCase() == 'get') {
// form.addEventListener('submit', virtualClick);
form.setAttribute("onsubmit", "virtualClick(event)");
}
};
var embed_img = function(img) {
var embedImg = function(img) {
if (img.hasAttribute('src')) {
const src = img.getAttribute('src');
if (is_virtual(src)) {
var path = normalize_path(src);
const file = retrieve_file(path);
const mime_type = window.global_context.file_tree[path].mime_type;
if (isVirtual(src)) {
var path = normalizePath(src);
const file = retrieveFile(path);
const mime_type = window.globalContext.fileTree[path].mime_type;
if (mime_type == 'image/svg+xml') {
img.setAttribute('src', "data:image/svg+xml;charset=utf-8;base64, " + btoa(file));
} else {
@ -168,9 +168,9 @@ var embed_img = function(img) {
};
var on_scroll_to_anchor = function(argument) {
if (window.global_context.anchor) {
document.location.replace("about:srcdoc#" + window.global_context.anchor);
var onScrollToAnchor = function(argument) {
if (window.globalContext.anchor) {
document.location.replace("about:srcdoc#" + window.globalContext.anchor);
}
}
@ -180,20 +180,20 @@ const observer = new MutationObserver((mutationList) => {
mutationList.forEach((mutation) => {
if (mutation.type == 'childList') {
Array.from(mutation.target.querySelectorAll("a")).forEach( a => {
fix_link(a);
fixLink(a);
});
Array.from(mutation.target.querySelectorAll("img")).forEach( img => {
embed_img(img);
embedImg(img);
});
Array.from(mutation.target.querySelectorAll("form")).forEach( form => {
fix_form(form);
fixForm(form);
});
}
});
});
var monkey_patch = function() {
var monkeyPatch = function() {
if (typeof jQuery === 'undefined') {return;} // Only for jQuery at the moment
/**
* Monkey patch getQueryParameters
@ -205,7 +205,7 @@ var monkey_patch = function() {
jQuery._getQueryParameters = jQuery.getQueryParameters;
jQuery.getQueryParameters = function(s) {
if (typeof s === 'undefined')
s = '?' + window.global_context.get_parameters;
s = '?' + window.globalContext.getParameters;
return jQuery._getQueryParameters(s);
};
@ -216,11 +216,11 @@ var monkey_patch = function() {
*/
jQuery._ajax = jQuery.ajax;
jQuery.ajax = function(settings) {
url = normalize_path(settings.url);
if (is_virtual(url)) {
url = normalizePath(settings.url);
if (isVirtual(url)) {
var result;
var data;
data = retrieve_file(url);
data = retrieveFile(url);
result = settings.complete({responseText: data}, "");
return; // Return value not actually needed in searchtools.js
} else {
@ -229,15 +229,15 @@ var monkey_patch = function() {
};
}
monkey_patch();
monkeyPatch();
// Set up message listener
window.addEventListener("message", (evnt) => {
console.log("Received message in iframe", evnt);
if (evnt.data.action == 'set_data') {
on_set_data(evnt.data.argument);
onSetData(evnt.data.argument);
} else if (evnt.data.action == 'scroll_to_anchor') {
on_scroll_to_anchor(evnt.data.argument);
onScrollToAnchor(evnt.data.argument);
}
}, false);

View File

@ -7,7 +7,7 @@
* creative.
*
* Here, we patch the `URLSearchParams` class so it returns the information
* stored in `window.global_context.get_parameters`.
* stored in `window.globalContext.getParameters`.
*
*/
@ -16,12 +16,12 @@ const originalGet = URLSearchParams.prototype.get;
var myGet = function (arg) {
// If searchtools.js of sphinx is used
if (
window.global_context &&
window.global_context.get_parameters &&
window.globalContext &&
window.globalContext.getParameters &&
(window.location.search === "") &&
(Array.from(this.entries()).length == 0)
) {
const params = new URLSearchParams('?' + window.global_context.get_parameters);
const params = new URLSearchParams('?' + window.globalContext.getParameters);
const result = params.get(arg);
// console.log("Return virtual get parameter:", arg, result);
return result;
@ -80,18 +80,18 @@ var _base64ToArrayBuffer = function (base64) {
};
window.fetch = async (...args) => {
// wait until global_context is ready
// wait until globalContext is ready
try {
await waitFor(() => window.hasOwnProperty("global_context"), 10000);
await waitFor(() => window.hasOwnProperty("globalContext"), 10000);
} catch (err) {
throw err;
}
let [resource, config ] = args;
var path = normalize_path(resource);
var path = normalizePath(resource);
var response;
if (is_virtual(path)) {
var file = retrieve_file(path);
if (isVirtual(path)) {
var file = retrieveFile(path);
var data = file.data;
if (file.base64encoded) {
data = _base64ToArrayBuffer(data);

View File

@ -1,11 +1,11 @@
const iFrameId = 'zundler-iframe';
var set_favicon = function(href) {
var setFavicon = function(href) {
if (!href) {return;}
var favicon = document.createElement("link");
favicon.setAttribute('rel', 'shortcut icon');
href = normalize_path(href);
const file = window.global_context.file_tree[href];
href = normalizePath(href);
const file = window.globalContext.fileTree[href];
if (!file) {return;}
if (file.mime_type == 'image/svg+xml') {
favicon.setAttribute('href', 'data:' + file.mime_type + ';charset=utf-8;base64,' + btoa(file.data));
@ -31,10 +31,10 @@ var createIframe = function() {
return iframe;
}
var retrieve_file = function(path) {
var retrieveFile = function(path) {
// console.log("Retrieving file: " + path);
var file_tree = window.global_context.file_tree;
var file = file_tree[path];
var fileTree = window.globalContext.fileTree;
var file = fileTree[path];
if (!file) {
console.warn("File not found: " + path);
return "";
@ -43,7 +43,7 @@ var retrieve_file = function(path) {
}
};
var is_virtual = function(url) {
var isVirtual = function(url) {
// Return true if the url should be retrieved from the virtual file tree
var _url = url.toString().toLowerCase();
return (! (
@ -57,13 +57,13 @@ var is_virtual = function(url) {
));
};
var split_url = function(url) {
var splitUrl = function(url) {
// Return a list of three elements: path, GET parameters, anchor
var anchor = url.split('#')[1] || "";
var get_parameters = url.split('#')[0].split('?')[1] || "";
var getParameters = url.split('#')[0].split('?')[1] || "";
var path = url.split('#')[0];
path = path.split('?')[0];
let result = [path, get_parameters, anchor];
let result = [path, getParameters, anchor];
// console.log("Split URL", url, result);
return result;
};
@ -83,7 +83,7 @@ var prepare = function(html) {
// problematic characters: `, ", ', &, </script>, ...
// atob is insufficient, because it only deals with ASCII - we have
// unicode
var serializedGC = unicodeToBase64(JSON.stringify(window.global_context));
var serializedGC = unicodeToBase64(JSON.stringify(window.globalContext));
scriptTag.textContent = `
function base64ToUnicode(base64String) {
@ -91,34 +91,34 @@ var prepare = function(html) {
return decodeURIComponent(escape(utf8EncodedString));
}
window.global_context = JSON.parse(base64ToUnicode("${serializedGC}"));
window.globalContext = JSON.parse(base64ToUnicode("${serializedGC}"));
`;
doc.head.prepend(scriptTag);
embed_js(doc);
embed_css(doc);
embed_imgs(doc);
embedJs(doc);
embedCss(doc);
embedImgs(doc);
fix_links(doc);
fix_forms(doc);
fixLinks(doc);
fixForms(doc);
return doc.documentElement.outerHTML;
}
var embed_js = function(doc) {
var embedJs = function(doc) {
Array.from(doc.querySelectorAll("script")).forEach( oldScript => {
const newScript = doc.createElement("script");
Array.from(oldScript.attributes).forEach( attr => {
newScript.setAttribute(attr.name, attr.value);
});
try {
if (newScript.hasAttribute('src') && is_virtual(newScript.getAttribute('src'))) {
if (newScript.hasAttribute('src') && isVirtual(newScript.getAttribute('src'))) {
var src = newScript.getAttribute('src');
let [path, get_parameters, anchor] = split_url(src);
path = normalize_path(path);
let [path, getParameters, anchor] = splitUrl(src);
path = normalizePath(path);
console.debug("Embed script: " + path);
var src = retrieve_file(path).data + ' \n//# sourceURL=' + path;
var src = retrieveFile(path).data + ' \n//# sourceURL=' + path;
newScript.appendChild(doc.createTextNode(src));
newScript.removeAttribute('src');
oldScript.parentNode.replaceChild(newScript, oldScript);
@ -131,31 +131,31 @@ var embed_js = function(doc) {
}
var embed_css = function(doc) {
var embedCss = function(doc) {
Array.from(doc.querySelectorAll("link")).forEach( link => {
if (link.getAttribute('rel') == 'stylesheet' && link.getAttribute("href")) {
const style = doc.createElement("style");
var href = link.getAttribute('href');
let [path, get_parameters, anchor] = split_url(href);
path = normalize_path(path);
style.textContent = retrieve_file(path).data;
let [path, getParameters, anchor] = splitUrl(href);
path = normalizePath(path);
style.textContent = retrieveFile(path).data;
link.replaceWith(style);
};
});
};
var fix_links = function(doc) {
var fixLinks = function(doc) {
Array.from(doc.querySelectorAll("a")).forEach( a => {
fix_link(a);
fixLink(a);
});
};
var fix_link = function(a) {
if (is_virtual(a.getAttribute('href'))) {
// a.addEventListener('click', virtual_click);
a.setAttribute("onclick", "virtual_click(event)");
var fixLink = function(a) {
if (isVirtual(a.getAttribute('href'))) {
// a.addEventListener('click', virtualClick);
a.setAttribute("onclick", "virtualClick(event)");
} else if (a.getAttribute('href').startsWith('#')) {
a.setAttribute('href', "about:srcdoc" + a.getAttribute('href'))
} else if (!a.getAttribute('href').startsWith('about:srcdoc')) {
@ -166,28 +166,28 @@ var fix_link = function(a) {
};
var fix_form = function(form) {
var fixForm = function(form) {
var href = form.getAttribute('action');
if (is_virtual(href) && form.getAttribute('method').toLowerCase() == 'get') {
// form.addEventListener('submit', virtual_click);
form.setAttribute("onsubmit", "virtual_click(event)");
if (isVirtual(href) && form.getAttribute('method').toLowerCase() == 'get') {
// form.addEventListener('submit', virtualClick);
form.setAttribute("onsubmit", "virtualClick(event)");
}
};
var fix_forms = function(doc) {
var fixForms = function(doc) {
Array.from(doc.querySelectorAll("form")).forEach( form => {
fix_form(form);
fixForm(form);
});
};
var embed_img = function(img) {
var embedImg = function(img) {
if (img.hasAttribute('src')) {
const src = img.getAttribute('src');
if (is_virtual(src)) {
var path = normalize_path(src);
const file = retrieve_file(path);
if (isVirtual(src)) {
var path = normalizePath(src);
const file = retrieveFile(path);
const mime_type = file.mime_type;
if (mime_type == 'image/svg+xml') {
img.setAttribute('src', "data:image/svg+xml;charset=utf-8;base64, " + btoa(file.data));
@ -199,18 +199,18 @@ var embed_img = function(img) {
};
var embed_imgs = function(doc) {
var embedImgs = function(doc) {
Array.from(doc.querySelectorAll("img")).forEach( img => {
embed_img(img);
embedImg(img);
});
};
var load_virtual_page = (function (path, get_params, anchor) {
var loadVirtualPage = (function (path, get_params, anchor) {
// fill the iframe with the new page
// return True if it worked
// return False if loading indicator should be removed right away
const file = window.global_context.file_tree[path];
const file = window.globalContext.fileTree[path];
var iframe = createIframe();
if (!file) {
@ -219,11 +219,11 @@ var load_virtual_page = (function (path, get_params, anchor) {
}
const data = file.data;
window.global_context.get_parameters = get_params;
window.globalContext.getParameters = get_params;
if (file.mime_type == 'text/html') {
window.global_context.current_path = path;
window.global_context.anchor = anchor;
window.globalContext.current_path = path;
window.globalContext.anchor = anchor;
const html = prepare(data);
iframe.setAttribute("srcdoc", html);
window.history.pushState({path, get_params, anchor}, '', '#');
@ -237,10 +237,10 @@ var load_virtual_page = (function (path, get_params, anchor) {
});
var normalize_path = function(path) {
var normalizePath = function(path) {
// TODO remove redundant definition of this function (in inject.js)
// make relative paths absolute
var result = window.global_context.current_path;
var result = window.globalContext.current_path;
result = result.split('/');
result.pop();
result = result.concat(path.split('/'));
@ -259,7 +259,7 @@ var normalize_path = function(path) {
});
result = array.join('/');
// console.log(`Normalized path: ${path} -> ${result} (@${window.global_context.current_path})`);
// console.log(`Normalized path: ${path} -> ${result} (@${window.globalContext.current_path})`);
return result;
};
@ -271,33 +271,33 @@ window.onload = function() {
var iframe = document.getElementById(iFrameId);
if (evnt.data.action == 'ready') {
// iframe is ready to receive the global_context
// iframe is ready to receive the globalContext
iframe.contentWindow.postMessage({
action: "set_data",
argument: window.global_context,
argument: window.globalContext,
}, "*");
} else if (evnt.data.action == 'set_title') {
// iframe has finished loading and sent us its title
// parent sets the title and responds with the global_context object
// parent sets the title and responds with the globalContext object
window.document.title = evnt.data.argument.title;
set_favicon(evnt.data.argument.favicon);
setFavicon(evnt.data.argument.favicon);
} else if (evnt.data.action == 'virtual_click') {
} else if (evnt.data.action == 'virtualClick') {
// user has clicked on a link in the iframe
show_loading_indictator();
var loaded = load_virtual_page(
showLoadingIndicator();
var loaded = loadVirtualPage(
evnt.data.argument.path,
evnt.data.argument.get_parameters,
evnt.data.argument.getParameters,
evnt.data.argument.anchor,
);
if (!loaded) {
hide_loading_indictator();
hideLoadingIndicator();
}
} else if (evnt.data.action == 'show_iframe') {
// iframe finished fixing the document and is ready to be shown;
hide_loading_indictator();
hideLoadingIndicator();
iframe.contentWindow.postMessage({
action: "scroll_to_anchor",
}, "*");
@ -306,15 +306,15 @@ window.onload = function() {
// Set up history event listener
window.addEventListener("popstate", (evnt) => {
load_virtual_page(evnt.state.path, evnt.state.get_params, evnt.state.anchor);
loadVirtualPage(evnt.state.path, evnt.state.get_params, evnt.state.anchor);
});
// Load first page
load_virtual_page(window.global_context.current_path, "", "");
loadVirtualPage(window.globalContext.current_path, "", "");
}
var show_loading_indictator = function() {
var showLoadingIndicator = function() {
var iframe = document.getElementById(iFrameId);
iframe.remove()
var loading = document.getElementById('loading-indicator');
@ -322,7 +322,7 @@ var show_loading_indictator = function() {
}
var hide_loading_indictator = function() {
var hideLoadingIndicator = function() {
var iframe = document.getElementById(iFrameId);
iframe.style.display = '';
var loading = document.getElementById('loading-indicator');

View File

@ -98,7 +98,7 @@ https://github.com/AdrianVollmer/Zundler
<html>
<head><style>{style}</style></head>
<body>{body}
<script>window.global_context = "{global_context}"</script>
<script>window.globalContext = "{global_context}"</script>
<script>{pako} \n//# sourceURL=pako.js</script>
<script>{bootstrap} \n//# sourceURL=boostrap.js</script>
</body><!-- {license} --></html>