mirror of
https://github.com/tcsenpai/Zundler.git
synced 2025-06-06 11:35:40 +00:00
Harmonize indentation
This commit is contained in:
parent
f4c4e7a31f
commit
a34cfa6027
48
src/init.js
48
src/init.js
@ -1,35 +1,35 @@
|
||||
var _ArrayBufferToBase64 = function (array_buffer) {
|
||||
var binary = '';
|
||||
var bytes = new Uint8Array(array_buffer);
|
||||
var len = bytes.byteLength;
|
||||
for (var i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode(bytes[i])
|
||||
}
|
||||
return window.btoa(binary);
|
||||
var binary = '';
|
||||
var bytes = new Uint8Array(array_buffer);
|
||||
var len = bytes.byteLength;
|
||||
for (var i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode(bytes[i])
|
||||
}
|
||||
return window.btoa(binary);
|
||||
};
|
||||
|
||||
|
||||
var _base64ToArrayBuffer = function (base64) {
|
||||
if (!base64) { return []}
|
||||
var binary_string = window.atob(base64);
|
||||
var len = binary_string.length;
|
||||
var bytes = new Uint8Array(len);
|
||||
for (var i = 0; i < len; i++) {
|
||||
bytes[i] = binary_string.charCodeAt(i);
|
||||
}
|
||||
return bytes.buffer;
|
||||
if (!base64) { return []}
|
||||
var binary_string = window.atob(base64);
|
||||
var len = binary_string.length;
|
||||
var bytes = new Uint8Array(len);
|
||||
for (var i = 0; i < len; i++) {
|
||||
bytes[i] = binary_string.charCodeAt(i);
|
||||
}
|
||||
return bytes.buffer;
|
||||
};
|
||||
|
||||
var createIframe = function() {
|
||||
var iframe = document.getElementById('main');
|
||||
if (iframe) { iframe.remove() };
|
||||
iframe = document.createElement("iframe");
|
||||
window.document.body.prepend(iframe);
|
||||
iframe.setAttribute('src', '#');
|
||||
iframe.setAttribute('name', 'main');
|
||||
iframe.setAttribute('id', 'main');
|
||||
iframe.style.display = 'none';
|
||||
return iframe;
|
||||
var iframe = document.getElementById('main');
|
||||
if (iframe) { iframe.remove() };
|
||||
iframe = document.createElement("iframe");
|
||||
window.document.body.prepend(iframe);
|
||||
iframe.setAttribute('src', '#');
|
||||
iframe.setAttribute('name', 'main');
|
||||
iframe.setAttribute('id', 'main');
|
||||
iframe.style.display = 'none';
|
||||
return iframe;
|
||||
}
|
||||
|
||||
var load_virtual_page = (function (path, get_params, anchor) {
|
||||
|
238
src/inject.js
238
src/inject.js
@ -1,158 +1,158 @@
|
||||
var embed_css = function(origin) {
|
||||
Array.from(document.querySelectorAll("link")).forEach( link => {
|
||||
if (link.getAttribute('rel') == 'stylesheet') {
|
||||
const style = document.createElement("style");
|
||||
var href = link.getAttribute('href');
|
||||
let [path, get_parameters, anchor] = split_url(href);
|
||||
path = normalize_path(path);
|
||||
style.innerText = retrieve_file(path);
|
||||
link.replaceWith(style);
|
||||
};
|
||||
});
|
||||
Array.from(document.querySelectorAll("link")).forEach( link => {
|
||||
if (link.getAttribute('rel') == 'stylesheet') {
|
||||
const style = document.createElement("style");
|
||||
var href = link.getAttribute('href');
|
||||
let [path, get_parameters, anchor] = split_url(href);
|
||||
path = normalize_path(path);
|
||||
style.innerText = retrieve_file(path);
|
||||
link.replaceWith(style);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
var embed_js = function(origin) {
|
||||
Array.from(document.querySelectorAll("script")).forEach( oldScript => {
|
||||
const newScript = document.createElement("script");
|
||||
Array.from(oldScript.attributes).forEach( attr => {
|
||||
newScript.setAttribute(attr.name, attr.value);
|
||||
});
|
||||
try {
|
||||
if (newScript.hasAttribute('src') && is_virtual(newScript.getAttribute('src'))) {
|
||||
var src = newScript.getAttribute('src');
|
||||
let [path, get_parameters, anchor] = split_url(src);
|
||||
path = normalize_path(path);
|
||||
var src = retrieve_file(path);
|
||||
newScript.appendChild(document.createTextNode(src));
|
||||
newScript.removeAttribute('src');
|
||||
oldScript.parentNode.replaceChild(newScript, oldScript);
|
||||
Array.from(document.querySelectorAll("script")).forEach( oldScript => {
|
||||
const newScript = document.createElement("script");
|
||||
Array.from(oldScript.attributes).forEach( attr => {
|
||||
newScript.setAttribute(attr.name, attr.value);
|
||||
});
|
||||
try {
|
||||
if (newScript.hasAttribute('src') && is_virtual(newScript.getAttribute('src'))) {
|
||||
var src = newScript.getAttribute('src');
|
||||
let [path, get_parameters, anchor] = split_url(src);
|
||||
path = normalize_path(path);
|
||||
var src = retrieve_file(path);
|
||||
newScript.appendChild(document.createTextNode(src));
|
||||
newScript.removeAttribute('src');
|
||||
oldScript.parentNode.replaceChild(newScript, oldScript);
|
||||
}
|
||||
} catch (e) {
|
||||
// Make sure all scripts are loaded
|
||||
console.error(e);
|
||||
}
|
||||
} catch (e) {
|
||||
// Make sure all scripts are loaded
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
var split_url = 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 path = url.split('#')[0];
|
||||
path = path.split('?')[0];
|
||||
let result = [path, get_parameters, anchor];
|
||||
// console.log("Split URL", url, result);
|
||||
return result;
|
||||
// Return a list of three elements: path, GET parameters, anchor
|
||||
var anchor = url.split('#')[1] || "";
|
||||
var get_parameters = url.split('#')[0].split('?')[1] || "";
|
||||
var path = url.split('#')[0];
|
||||
path = path.split('?')[0];
|
||||
let result = [path, get_parameters, anchor];
|
||||
// console.log("Split URL", url, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
var virtual_click = function(evnt) {
|
||||
// Handle GET parameters and anchors
|
||||
console.log("Virtual click", evnt);
|
||||
var a = evnt.currentTarget;
|
||||
let [path, get_parameters, anchor] = split_url(a.getAttribute('href'));
|
||||
path = normalize_path(path);
|
||||
// Handle GET parameters and anchors
|
||||
console.log("Virtual click", evnt);
|
||||
var a = evnt.currentTarget;
|
||||
let [path, get_parameters, anchor] = split_url(a.getAttribute('href'));
|
||||
path = normalize_path(path);
|
||||
|
||||
window.parent.postMessage({
|
||||
action: "virtual_click",
|
||||
argument: {
|
||||
path: path,
|
||||
get_parameters: get_parameters,
|
||||
anchor: anchor,
|
||||
}
|
||||
}, '*');
|
||||
evnt.preventDefault();
|
||||
evnt.stopPropagation();
|
||||
return false;
|
||||
window.parent.postMessage({
|
||||
action: "virtual_click",
|
||||
argument: {
|
||||
path: path,
|
||||
get_parameters: get_parameters,
|
||||
anchor: anchor,
|
||||
}
|
||||
}, '*');
|
||||
evnt.preventDefault();
|
||||
evnt.stopPropagation();
|
||||
return false;
|
||||
};
|
||||
|
||||
var fix_links = function(origin) {
|
||||
Array.from(document.querySelectorAll("a")).forEach( a => {
|
||||
if (is_virtual(a.getAttribute('href'))) {
|
||||
a.addEventListener('click', virtual_click);
|
||||
}
|
||||
});
|
||||
Array.from(document.querySelectorAll("a")).forEach( a => {
|
||||
if (is_virtual(a.getAttribute('href'))) {
|
||||
a.addEventListener('click', virtual_click);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var fix_forms = function(origin) {
|
||||
Array.from(document.querySelectorAll("form")).forEach( form => {
|
||||
var href = form.getAttribute('action');
|
||||
if (is_virtual(href)) {
|
||||
// TODO test this
|
||||
// let [path, get_parameters, anchor] = split_url(href);
|
||||
// path = normalize_path(path);
|
||||
// var new_href = to_blob(retrieve_file(path), 'text/html');
|
||||
// if (get_parameters) { new_href += '?' + get_parameters; }
|
||||
// if (anchor) { new_href += '?' + anchor; }
|
||||
// form.action = new_href;
|
||||
}
|
||||
});
|
||||
Array.from(document.querySelectorAll("form")).forEach( form => {
|
||||
var href = form.getAttribute('action');
|
||||
if (is_virtual(href)) {
|
||||
// TODO test this
|
||||
// let [path, get_parameters, anchor] = split_url(href);
|
||||
// path = normalize_path(path);
|
||||
// var new_href = to_blob(retrieve_file(path), 'text/html');
|
||||
// if (get_parameters) { new_href += '?' + get_parameters; }
|
||||
// if (anchor) { new_href += '?' + anchor; }
|
||||
// form.action = new_href;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
var embed_img = function(origin) {
|
||||
Array.from(document.querySelectorAll("img")).forEach( img => {
|
||||
if (img.hasAttribute('src')) {
|
||||
const src = img.getAttribute('src');
|
||||
if (is_virtual(src)) {
|
||||
var path = normalize_path(src);
|
||||
const file = retrieve_file(path);
|
||||
// TODO handle mime type
|
||||
if (file.startsWith('<svg')) {
|
||||
img.setAttribute('src', "data:image/svg+xml;charset=utf-8;base64, " + btoa(file));
|
||||
} else {
|
||||
img.setAttribute('src', "data:image/png;base64, " + file);
|
||||
}
|
||||
};
|
||||
};
|
||||
});
|
||||
Array.from(document.querySelectorAll("img")).forEach( img => {
|
||||
if (img.hasAttribute('src')) {
|
||||
const src = img.getAttribute('src');
|
||||
if (is_virtual(src)) {
|
||||
var path = normalize_path(src);
|
||||
const file = retrieve_file(path);
|
||||
// TODO handle mime type
|
||||
if (file.startsWith('<svg')) {
|
||||
img.setAttribute('src', "data:image/svg+xml;charset=utf-8;base64, " + btoa(file));
|
||||
} else {
|
||||
img.setAttribute('src', "data:image/png;base64, " + file);
|
||||
}
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
var is_virtual = function(url) {
|
||||
// Return true if the url should be retrieved from the virtual file tree
|
||||
var _url = url.toString().toLowerCase();
|
||||
return (! (
|
||||
_url == "" ||
|
||||
_url[0] == "#" ||
|
||||
_url.startsWith('https://') ||
|
||||
_url.startsWith('http://') ||
|
||||
_url.startsWith('data:') ||
|
||||
_url.startsWith('blob:')
|
||||
));
|
||||
// Return true if the url should be retrieved from the virtual file tree
|
||||
var _url = url.toString().toLowerCase();
|
||||
return (! (
|
||||
_url == "" ||
|
||||
_url[0] == "#" ||
|
||||
_url.startsWith('https://') ||
|
||||
_url.startsWith('http://') ||
|
||||
_url.startsWith('data:') ||
|
||||
_url.startsWith('blob:')
|
||||
));
|
||||
};
|
||||
|
||||
var retrieve_file = function(path) {
|
||||
// console.log("Retrieving file: " + path);
|
||||
var file_tree = window.data.file_tree;
|
||||
var file = file_tree[path];
|
||||
return file;
|
||||
// console.log("Retrieving file: " + path);
|
||||
var file_tree = window.data.file_tree;
|
||||
var file = file_tree[path];
|
||||
return file;
|
||||
};
|
||||
|
||||
var normalize_path = function(path) {
|
||||
// make relative paths absolute
|
||||
var result = window.data.current_path;
|
||||
result = result.split('/');
|
||||
result.pop();
|
||||
result = result.concat(path.split('/'));
|
||||
// make relative paths absolute
|
||||
var result = window.data.current_path;
|
||||
result = result.split('/');
|
||||
result.pop();
|
||||
result = result.concat(path.split('/'));
|
||||
|
||||
// resolve relative directories
|
||||
var array = [];
|
||||
Array.from(result).forEach( component => {
|
||||
if (component == '..') {
|
||||
if (array) {
|
||||
array.pop();
|
||||
}
|
||||
} else if (component == '.') {
|
||||
} else {
|
||||
if (component) { array.push(component); }
|
||||
}
|
||||
});
|
||||
// resolve relative directories
|
||||
var array = [];
|
||||
Array.from(result).forEach( component => {
|
||||
if (component == '..') {
|
||||
if (array) {
|
||||
array.pop();
|
||||
}
|
||||
} else if (component == '.') {
|
||||
} else {
|
||||
if (component) { array.push(component); }
|
||||
}
|
||||
});
|
||||
|
||||
result = array.join('/');
|
||||
// console.log(`Normalized path: ${path} -> ${result} (@${window.data.current_path})`);
|
||||
return result;
|
||||
result = array.join('/');
|
||||
// console.log(`Normalized path: ${path} -> ${result} (@${window.data.current_path})`);
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user