Skip to content

Instantly share code, notes, and snippets.

@pdaug
Last active August 22, 2023 20:02
Show Gist options
  • Save pdaug/464d6acb5947bc6835168f7b1784d70b to your computer and use it in GitHub Desktop.
Save pdaug/464d6acb5947bc6835168f7b1784d70b to your computer and use it in GitHub Desktop.
<head>
<title> JSON to URL </title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
</head>
<body style="font-family: monospace; display: flex; align-items: center; justify-content: center; text-align: center; width: 100vw; margin: 0; height: 100vh;">
<div style="display: flex; gap: 32px;">
<div style="width: 500px;">
<h2> JSON -> URL </h2>
<div style="display: flex; flex-direction: column; gap: 16px;">
<input type="text" name="imei" placeholder="Digite o IMEI" required/>
<input type="date" name="dataInicial" required/>
<input type="date" name="dataFinal" required/>
<div style="display: flex; gap: 16px; width: 100%">
<input style="flex: 1;" type="text" name="key" placeholder="CHAVE"/>
<input style="flex: 1;" type="text" name="value" placeholder="VALOR"/>
</div>
<button onclick="ConverterEntradas();"> Converter! </button>
<button onclick="AbrirPagina('history');"> Abrir Página History </button>
<button onclick="AbrirPagina('alert');"> Abrir Página Alert </button>
<button onclick="AbrirPagina('trip');"> Abrir Página Trip </button>
<button onclick="AbrirPagina('overspeed');"> Abrir Página Overspeed </button>
</div>
<div style="word-wrap: break-word; margin: 32px 0;" class="fieldInputEncrypt"></div>
<div style="word-wrap: break-word; margin: 32px 0;" class="fieldInputDecrypt"></div>
</div>
<div style="width: 500px;">
<h2> URL -> JSON </h2>
<div style="display: flex; flex-direction: column; gap: 16px; width: 100%;">
<textarea class="url" rows="10"></textarea>
<button onclick="ConverterSaidas();"> Converter! </button>
</div>
<div style="word-wrap: break-word; margin: 32px 0;" class="fieldOutputDecrypt"></div>
</div>
</div>
<script type="text/javascript">
const Security = {
CRYPTO_SECRET: "HERE_CRYPTO_SECRET_CODE",
};
const stringToHex = function(text) {
let hex = "";
for (let counter = 0; counter < text.length; counter++) {
const charCode = text.charCodeAt(counter);
const hexValue = charCode.toString(16);
hex += hexValue.padStart(2, "0");
}
return hex;
};
const hexToString = function(hex) {
let text = "";
for (let counter = 0; counter < hex.length; counter += 2) {
const hexValue = hex.substr(counter, 2);
const decimalValue = parseInt(hexValue, 16);
text += String.fromCharCode(decimalValue);
}
return text;
};
const Encrypt = function(message) {
const key = Security.CRYPTO_SECRET;
const encryptRaw = CryptoJS.AES.encrypt(message, key);
const encryptBase64 = encryptRaw.toString();
const encryptHex = stringToHex(encryptBase64);
return encryptHex;
};
const Decrypt = function(message) {
const text = hexToString(message);
const key = Security.CRYPTO_SECRET;
const decryptRaw = CryptoJS.AES.decrypt(text, key);
const decryptString = decryptRaw.toString(CryptoJS.enc.Utf8);
return decryptString;
};
const DateFormatter = function(date) {
if (date instanceof Date) {
const locale = "pt-BR";
const option = { day: "2-digit", month: "2-digit", year: "numeric" };
//const formatted = date.toLocaleDateString(locale, option);
const formatted = `${ date.getUTCDate() }/${ (date.getUTCMonth() + 1).toString().padStart(2, "0") }/${ date.getUTCFullYear() }` ;
console.log(formatted);
return formatted;
}
return null;
};
const AbrirPagina = function(type) {
const inputImei = document.querySelector("input[name=imei]");
const inputDateStart = document.querySelector("input[name=dataInicial]");
const inputDateEnd = document.querySelector("input[name=dataFinal]");
const inputKey = document.querySelector("input[name=key]");
const inputValue = document.querySelector("input[name=value]");
const inputKeyText = inputKey.value ? inputKey.value : null;
const inputValueText = inputValue.value ? inputValue.value : null;
const inputImeiValue = inputImei.value;
const inputDateStartValue = inputDateStart.value ? new Date(inputDateStart.value) : null;
const inputDateEndValue = inputDateEnd.value ? new Date(inputDateEnd.value) : null;
const inputDateStartFormatted = DateFormatter(inputDateStartValue);
const inputDateEndFormatted = DateFormatter(inputDateEndValue);
const structure = { imei: inputImeiValue, dataInicial: inputDateStartFormatted, dataFinal: inputDateEndFormatted, [inputKeyText]: inputValueText };
const structureString = JSON.stringify(structure);
//const urlBase = "https://tracker-net.web.app/legacy/${type}/";
const urlBase = `http://localhost:5173/legacy/${type}/`;
const urlParams = Encrypt(structureString);
const url = urlBase + urlParams;
window.open(url, "Relatórios", "width=1280,height=720")
};
const ConverterEntradas = function() {
const inputImei = document.querySelector("input[name=imei]");
const inputDateStart = document.querySelector("input[name=dataInicial]");
const inputDateEnd = document.querySelector("input[name=dataFinal]");
const inputImeiValue = inputImei.value;
const inputDateStartValue = inputDateStart.value ? new Date(inputDateStart.value) : null;
const inputDateEndValue = inputDateEnd.value ? new Date(inputDateEnd.value) : null;
const inputDateStartFormatted = DateFormatter(inputDateStartValue);
const inputDateEndFormatted = DateFormatter(inputDateEndValue);
const inputKey = document.querySelector("input[name=key]");
const inputValue = document.querySelector("input[name=value]");
const inputKeyText = inputKey.value ? inputKey.value : null;
const inputValueText = inputValue.value ? inputValue.value : null;
const structure = { imei: inputImeiValue, dataInicial: inputDateStartFormatted, dataFinal: inputDateEndFormatted, [inputKeyText]: inputValueText };
const structureString = JSON.stringify(structure);
const fieldEncrypt = document.querySelector(".fieldInputEncrypt");
const fieldEncryptValue = Encrypt(structureString);
fieldEncrypt.innerHTML = fieldEncryptValue;
const message = Decrypt(fieldEncryptValue);
const fieldDecrypt = document.querySelector(".fieldInputDecrypt");
fieldDecrypt.innerHTML = message;
};
const ConverterSaidas = function() {
const inputUrl = document.querySelector(".url");
const inputUrlValue = inputUrl.value;
const message = Decrypt(inputUrlValue);
const fieldDecrypt = document.querySelector(".fieldOutputDecrypt");
fieldDecrypt.innerHTML = message;
};
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment