initial program commit
This commit is contained in:
File diff suppressed because one or more lines are too long
+520
File diff suppressed because one or more lines are too long
+168
@@ -0,0 +1,168 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>ha.mr - link compressor</title>
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Hammersmith+One&display=swap');
|
||||
html, body {
|
||||
margin: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
body {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
font-size: 120%;
|
||||
}
|
||||
* {
|
||||
font-family: "Hammersmith One", sans-serif;
|
||||
}
|
||||
#loader {
|
||||
position: absolute;
|
||||
border: 0.5em solid lightgray;
|
||||
border-radius: 50%;
|
||||
border-top: 0.5em solid gray;
|
||||
width: 3em;
|
||||
height: 3em;
|
||||
-webkit-animation: spin 1.5s linear infinite;
|
||||
animation: spin 1.5s linear infinite;
|
||||
opacity: 1;
|
||||
pointer-events: none;
|
||||
transition: opacity 1s;
|
||||
}
|
||||
@-webkit-keyframes spin {
|
||||
0% { -webkit-transform: rotate(0deg); }
|
||||
100% { -webkit-transform: rotate(360deg); }
|
||||
}
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
#content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
transition: opacity 1s;
|
||||
}
|
||||
h1 {
|
||||
margin: 0;
|
||||
}
|
||||
p {
|
||||
margin-top: 0;
|
||||
}
|
||||
label {
|
||||
font-size: 0.9em;
|
||||
}
|
||||
span {
|
||||
text-align: center;
|
||||
}
|
||||
.title {
|
||||
font-size: 2.5em;
|
||||
}
|
||||
.subtitle {
|
||||
font-size: 0.6em;
|
||||
margin-top: -10px;
|
||||
font-style: italic;
|
||||
opacity: 0.8;
|
||||
}
|
||||
#input-link {
|
||||
padding: 5px 8px;
|
||||
border: 0;
|
||||
outline: 0;
|
||||
border-bottom: 2px solid darkgray;
|
||||
border-radius: 10px;
|
||||
margin: 10px 0;
|
||||
font-size: 16px;
|
||||
background-color: rgb(230, 230, 230);
|
||||
}
|
||||
#output-link-container {
|
||||
margin: 10px 0 0 0;
|
||||
text-align: center;
|
||||
}
|
||||
#output-link {
|
||||
color: rgb(50, 100, 255);
|
||||
word-break: break-all;
|
||||
}
|
||||
#output-ratio {
|
||||
transition: color 0.5s;
|
||||
color: gray;
|
||||
user-select: none;
|
||||
}
|
||||
#qrcode {
|
||||
display: none;
|
||||
}
|
||||
#qr-correct-level-container {
|
||||
display: none;
|
||||
}
|
||||
#query-warning {
|
||||
display: none;
|
||||
font-size: 1rem;
|
||||
text-align: justify;
|
||||
text-align-last: center;
|
||||
max-width: 80vmin;
|
||||
background-color: gold;
|
||||
padding: 5px 10px;
|
||||
border-radius: 10px;
|
||||
margin: 5px 0 15px 0;
|
||||
}
|
||||
#query-warning summary {
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
<script src="/qrcode.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="loader"></div>
|
||||
|
||||
<div id="content">
|
||||
|
||||
<h1 class="title">ha.mr</h1>
|
||||
<p class="subtitle">(read: "hammer")</p>
|
||||
|
||||
<input type="text" id="input-link" placeholder="https://some-long.link/" autofocus>
|
||||
<details id="query-warning">
|
||||
<summary>Consider removing parameters...</summary>
|
||||
<br>
|
||||
Some links include data that isn't necessary for the link to function,
|
||||
such as tracking information or page state. This data comes in the
|
||||
form of <i>query parameters</i>, marked by "?" and "&" characters.
|
||||
<br><br>
|
||||
Removing these segments can sometimes significantly shorten the link
|
||||
without breaking it. For example, an Amazon product link contains a lot
|
||||
of tracking information. Everything past the "?" symbol can be safely
|
||||
removed, making the link much shorter.
|
||||
</details>
|
||||
<p id="output-link-container">
|
||||
<a id="output-link" target="_blank">Enter a link above to compress</a>
|
||||
<br><span id="output-ratio"> </span>
|
||||
</p>
|
||||
|
||||
<image width="256" height="256" id="qrcode"></image>
|
||||
|
||||
<span id="qr-correct-level-container">
|
||||
<label>QR code error correction threshold</label>
|
||||
<br>
|
||||
<input type="range" name="qr-correct-level" id="qr-correct-level" autocomplete="off" min="0" max="3" value="1">
|
||||
</span>
|
||||
|
||||
<span style="margin-top:20px">
|
||||
<input type="checkbox" id="settings-emoji" name="settings-emoji" autocomplete="off">
|
||||
<label for="settings-emoji"> Use emoji in text output</label><br>
|
||||
</span>
|
||||
<span>
|
||||
<input type="checkbox" id="settings-qr" name="settings-qr" autocomplete="off">
|
||||
<label for="settings-qr"> Output QR code</label><br>
|
||||
</span>
|
||||
<br><br>
|
||||
|
||||
</div>
|
||||
|
||||
<script type="module" src="/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,162 @@
|
||||
import { compress, decompress } from "./compress.js";
|
||||
import {
|
||||
outputAlphabetASCII,
|
||||
outputAlphabetQR,
|
||||
outputAlphabetEmoji
|
||||
} from "./alphabets.js";
|
||||
|
||||
var settings = {
|
||||
emoji: false,
|
||||
qr: false
|
||||
};
|
||||
|
||||
const settingsElements = {
|
||||
emoji: "#settings-emoji",
|
||||
qr: "#settings-qr"
|
||||
};
|
||||
|
||||
for (const setting in settingsElements) {
|
||||
const element = document.querySelector(settingsElements[setting]);
|
||||
settings[setting] = element.checked;
|
||||
element.addEventListener("change", (event) => {
|
||||
settings[setting] = element.checked;
|
||||
updateOutput();
|
||||
});
|
||||
}
|
||||
|
||||
function countSymbols (string, alphabet) {
|
||||
let count = 0;
|
||||
while (string) {
|
||||
const symbol = alphabet.find(c => string.endsWith(c));
|
||||
string = string.slice(0, symbol ? -symbol.length : -1);
|
||||
count ++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
const inputLinkElement = document.querySelector("#input-link");
|
||||
const outputLinkElement = document.querySelector("#output-link");
|
||||
const outputRatioElement = document.querySelector("#output-ratio");
|
||||
const queryWarningElement = document.querySelector("#query-warning");
|
||||
|
||||
const qrCodeImage = document.querySelector("#qrcode");
|
||||
const qrCodeCorrectionLevelContainer = document.querySelector("#qr-correct-level-container");
|
||||
const qrCodeCorrectionLevelElement = document.querySelector("#qr-correct-level");
|
||||
qrCodeCorrectionLevelElement.addEventListener("change", updateOutput);
|
||||
|
||||
function updateOutput () {
|
||||
const input = inputLinkElement.value.trim();
|
||||
try {
|
||||
const alphabet = settings.emoji ? outputAlphabetEmoji : outputAlphabetASCII;
|
||||
const output = compress(input, alphabet);
|
||||
let inputNormalized = input;
|
||||
if (input.startsWith("https://")) {
|
||||
inputNormalized = input.slice(8);
|
||||
} else if (input.startsWith("http://")) {
|
||||
inputNormalized = input.slice(7);
|
||||
}
|
||||
let excessiveParams = false;
|
||||
if (URL.canParse("http://" + inputNormalized)) {
|
||||
const url = new URL("http://" + inputNormalized);
|
||||
if (url.searchParams.size > 1) {
|
||||
excessiveParams = true;
|
||||
}
|
||||
}
|
||||
if (excessiveParams) {
|
||||
queryWarningElement.style.display = "inline";
|
||||
} else {
|
||||
queryWarningElement.style.display = "none";
|
||||
}
|
||||
const ratio = (1 - (countSymbols(output, alphabet) + 6) / inputNormalized.length) * 100;
|
||||
if (ratio < -300) {
|
||||
outputRatioElement.textContent = `Output is much larger than the input`;
|
||||
outputRatioElement.style.color = "rgb(255, 50, 50)";
|
||||
} else if (ratio < 0) {
|
||||
outputRatioElement.textContent = `Output is ${Math.floor(-ratio)}% larger than the input`;
|
||||
outputRatioElement.style.color = "rgb(255, 50, 50)";
|
||||
} else if (ratio > 0) {
|
||||
outputRatioElement.textContent = `Output is ${Math.ceil(ratio)}% smaller than the input`;
|
||||
outputRatioElement.style.color = "rgb(15, 190, 15)";
|
||||
} else {
|
||||
outputRatioElement.textContent = "Output is the same length as the input";
|
||||
outputRatioElement.style.color = "gray";
|
||||
}
|
||||
outputLinkElement.textContent = `http://ha.mr#${output}`;
|
||||
outputLinkElement.href = `http://ha.mr#${output}`;
|
||||
outputLinkElement.style.color = "";
|
||||
if (settings.qr) {
|
||||
const errorCorrection = ["L", "M", "Q", "H"][qrCodeCorrectionLevelElement.value];
|
||||
qrCodeImage.style.display = "inline";
|
||||
qrCodeCorrectionLevelContainer.style.display = "inline";
|
||||
let qrCodeLink = `HTTP://HA.MR/${compress(input, outputAlphabetQR)}`;
|
||||
QRCode.toDataURL(qrCodeLink, {
|
||||
errorCorrectionLevel: errorCorrection,
|
||||
scale: 8
|
||||
}, (err, url) => {
|
||||
if (err) {
|
||||
qrCodeImage.style.display = "none";
|
||||
qrCodeCorrectionLevelContainer.style.display = "none";
|
||||
return;
|
||||
}
|
||||
qrCodeImage.src = url;
|
||||
qrCodeImage.title = qrCodeLink;
|
||||
});
|
||||
} else {
|
||||
qrCodeImage.style.display = "none";
|
||||
qrCodeCorrectionLevelContainer.style.display = "none";
|
||||
}
|
||||
} catch (e) {
|
||||
if (!input.trim()) {
|
||||
outputLinkElement.textContent = "Enter a link above to compress";
|
||||
} else {
|
||||
outputLinkElement.textContent = "Invalid link";
|
||||
outputLinkElement.style.color = "rgb(255, 50, 50)";
|
||||
console.error(e);
|
||||
}
|
||||
qrCodeImage.style.display = "none";
|
||||
qrCodeCorrectionLevelContainer.style.display = "none";
|
||||
outputRatioElement.style.color = "rgba(255, 255, 255, 0)";
|
||||
outputLinkElement.removeAttribute("href");
|
||||
queryWarningElement.style.display = "none";
|
||||
}
|
||||
}
|
||||
inputLinkElement.addEventListener("input", updateOutput);
|
||||
|
||||
(() => {
|
||||
let payload = null;
|
||||
let alphabet = outputAlphabetASCII;
|
||||
|
||||
// Get hash value of current address bar
|
||||
if (window.location.hash) {
|
||||
// Decode hash value in case it's non-ASCII
|
||||
payload = decodeURIComponent(window.location.hash.slice(1));
|
||||
// Remove all whitespace - we never use whitespace when encoding hash values
|
||||
payload = payload.replaceAll(" ", "");
|
||||
// Check if input is pure ASCII - potentially unreliable?
|
||||
const useEmoji = Array.from(payload).some(c => !outputAlphabetASCII.includes(c));
|
||||
alphabet = useEmoji ? outputAlphabetEmoji : outputAlphabetASCII;
|
||||
} else {
|
||||
// If no hash value, we're likely reading a QR code
|
||||
// For that, use the path instead
|
||||
payload = decodeURIComponent(window.location.pathname.slice(1));
|
||||
alphabet = outputAlphabetQR;
|
||||
}
|
||||
|
||||
if (payload && payload.trim()) {
|
||||
try {
|
||||
const target = decompress(payload, alphabet);
|
||||
window.location.href = target;
|
||||
return;
|
||||
} catch (e) {
|
||||
console.warn(`Redirect failed. Could not decode input.`);
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
updateOutput();
|
||||
|
||||
document.querySelector("#loader").style.opacity = 0;
|
||||
document.querySelector("#content").style.opacity = 1;
|
||||
document.querySelector("#content").style.pointerEvents = "auto";
|
||||
|
||||
})();
|
||||
@@ -0,0 +1,46 @@
|
||||
import { compress, decompress } from "./compress.js";
|
||||
import {
|
||||
outputAlphabetASCII,
|
||||
outputAlphabetQR,
|
||||
outputAlphabetEmoji
|
||||
} from "./alphabets.js";
|
||||
|
||||
const input = process.argv[2]?.trim();
|
||||
const alphabetName = process.argv[3]?.trim() || "ascii";
|
||||
if (!input) {
|
||||
console.error(`Usage: hamr <link> [ascii|qr|emoji]`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let payload = "";
|
||||
if (input.toLowerCase().startsWith("http://ha.mr")) {
|
||||
payload = input.slice(12);
|
||||
} else if (input.toLowerCase().startsWith("https://ha.mr")) {
|
||||
payload = input.slice(13);
|
||||
} else if (input.toLowerCase().startsWith("ha.mr")) {
|
||||
payload = input.slice(5);
|
||||
}
|
||||
|
||||
if (payload) {
|
||||
const isQRCode = input[0] === "/";
|
||||
payload = payload.slice(1);
|
||||
const useEmoji = Array.from(payload).some(c => !outputAlphabetASCII.includes(c));
|
||||
if (isQRCode) console.log(decompress(payload, outputAlphabetQR));
|
||||
else console.log(decompress(payload, useEmoji ? outputAlphabetEmoji : outputAlphabetASCII));
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
let alphabet = outputAlphabetASCII;
|
||||
if (alphabetName === "qr") alphabet = outputAlphabetQR;
|
||||
else if (alphabetName === "emoji") alphabet = outputAlphabetEmoji;
|
||||
else if (alphabetName !== "ascii") {
|
||||
console.error(`Unknown alphabet "${alphabetName}".`);
|
||||
console.error("Select one of: ascii, qr, emoji");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
if (alphabetName === "qr") {
|
||||
console.log("HTTP://HA.MR/" + compress(input, alphabet));
|
||||
} else {
|
||||
console.log("http://ha.mr#" + compress(input, alphabet));
|
||||
}
|
||||
Reference in New Issue
Block a user