From a34cfa602760bde84438468c38750651d33242b9 Mon Sep 17 00:00:00 2001 From: Adrian Vollmer Date: Sun, 25 Sep 2022 10:39:58 +0200 Subject: [PATCH] Harmonize indentation --- src/init.js | 48 +++++----- src/inject.js | 238 +++++++++++++++++++++++++------------------------- 2 files changed, 143 insertions(+), 143 deletions(-) diff --git a/src/init.js b/src/init.js index fe62982..96edcae 100644 --- a/src/init.js +++ b/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) { diff --git a/src/inject.js b/src/inject.js index 70d9fd1..a5d717d 100644 --- a/src/inject.js +++ b/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(' { + 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(' { - 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; };