diff --git a/techdemos/encode/index.markdown b/techdemos/encode/index.markdown index e93055f..ab5aa3d 100644 --- a/techdemos/encode/index.markdown +++ b/techdemos/encode/index.markdown @@ -22,4 +22,4 @@ Mode: - \ No newline at end of file + \ No newline at end of file diff --git a/techdemos/encode/js/encoders/url.js b/techdemos/encode/js/encoders/url.js new file mode 100644 index 0000000..d9ec6b0 --- /dev/null +++ b/techdemos/encode/js/encoders/url.js @@ -0,0 +1,29 @@ +const encodeURIAggressive = text => "%" + text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("%").toUpperCase(); +const decodeURIAggressive = text => text.split("%").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join(""); + +function encodeURINonDestructive(text) { + var encoded_text = encodeURIAggressive(text); + + if (encoded_text.includes("%3A%2F%2F")) { + encoded_text = encoded_text.replace("%3A%2F%2F", "://"); + const protocol = decodeURIAggressive(encoded_text.split("://")[0]) + encoded_text = protocol + "://" + encoded_text.split("://")[1]; + } + + encoded_text = encoded_text.replaceAll("%2F", "/").replaceAll("%3F", "?").replaceAll("%26", "&").replaceAll("%2B", "+").replaceAll("%3D", "="); + + return encoded_text; +} + +function decodeURINonDestructive(text) { + if (text.includes("://")) { + const protocol = encodeURIAggressive(text.split("://")[0]); + text = protocol + "%3A%2F%2F" + text.split("://")[1]; + } + + text = text.replaceAll("/", "%2F").replaceAll("?", "%3F").replaceAll("&", "%26").replaceAll("+", "%2B").replaceAll("=", "%3D"); + + return decodeURIAggressive(text); +} + +export {encodeURIAggressive, decodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive} \ No newline at end of file diff --git a/techdemos/encode/js/main.js b/techdemos/encode/js/main.js index 32ccb52..0499f0a 100644 --- a/techdemos/encode/js/main.js +++ b/techdemos/encode/js/main.js @@ -1,9 +1,12 @@ +import {encodeURIAggressive, decodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive} from "./encoders/url.js"; + const mode = document.getElementById("mode"); const encode = document.getElementById("encode"); const decode = document.getElementById("decode"); const livemode = document.getElementById("livemode"); const input = document.getElementById("inputText"); const output = document.getElementById("outputText"); + const codes = { "Base64": { encode: text => btoa(text), @@ -13,6 +16,10 @@ const codes = { encode: text => text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("").toUpperCase(), decode: text => text.split(/(\w\w)/g).filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join("") }, + "Base10 (Decimal)": { + encode: text => text.split("").map(c => c.charCodeAt(0).toString(10).padStart(3, "0")).join(" ").toUpperCase(), + decode: text => text.split(" ").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 10))).join("") + }, "Base2 (Binary)": { encode: text => text.split("").map(c => c.charCodeAt(0).toString(2).padStart(8, "0")).join(" "), decode: text => text.split(" ").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 2))).join("") @@ -26,10 +33,15 @@ const codes = { decode: text => decodeURIComponent(text) }, "URL (aggressive)": { - encode: text => "%" + text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("%").toUpperCase(), - decode: text => text.split("%").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join("") + encode: encodeURIAggressive, + decode: decodeURIAggressive + }, + "URL (aggressive, non destructive)": { + encode: encodeURINonDestructive, + decode: decodeURINonDestructive }, } + for (const code in codes) { let element = document.createElement("option"); element.value = code;