2024-01-17 11:13:01 +00:00
|
|
|
import {Base64} from "js-base64";
|
2022-11-22 13:17:30 +00:00
|
|
|
|
2024-01-17 11:13:01 +00:00
|
|
|
const fragmentPrefix = "#json=";
|
2022-11-22 13:17:30 +00:00
|
|
|
|
|
|
|
const exampleRedirect: Redirect = {
|
2024-01-17 11:13:01 +00:00
|
|
|
matcherType: "hostname",
|
|
|
|
matcherValue: "example.com",
|
|
|
|
redirectType: "hostname",
|
|
|
|
redirectValue: "example.org",
|
2022-11-22 13:17:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function decodeBase64<T>(base64: string): T {
|
|
|
|
return JSON.parse(Base64.decode(base64)) as T;
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeBase64(source: any): string {
|
|
|
|
return Base64.encode(JSON.stringify(source), true);
|
|
|
|
}
|
|
|
|
|
2024-01-17 11:13:01 +00:00
|
|
|
window.addEventListener("DOMContentLoaded", () => {
|
|
|
|
window["Re-Nav"] = {
|
2022-11-22 13:17:30 +00:00
|
|
|
decodeBase64,
|
|
|
|
encodeBase64,
|
|
|
|
share,
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
"Want to manually create a share link? Use the window['Re-Nav'].share",
|
2024-01-17 11:13:01 +00:00
|
|
|
"function, where redirect is an object with matcherType, matcherValue,",
|
|
|
|
"redirectType and redirectValue properties. Like this:",
|
2022-11-22 13:17:30 +00:00
|
|
|
);
|
|
|
|
console.log(JSON.stringify(exampleRedirect, undefined, 2));
|
2024-01-17 11:13:01 +00:00
|
|
|
console.log(window["Re-Nav"].share);
|
2022-11-22 13:17:30 +00:00
|
|
|
|
|
|
|
if (!location.hash.startsWith(fragmentPrefix)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const decoded = decodeBase64<Redirect>(
|
|
|
|
location.hash.slice(fragmentPrefix.length),
|
|
|
|
);
|
|
|
|
for (const [key, value] of Object.entries(decoded)) {
|
|
|
|
insertText(key as keyof Redirect, value);
|
|
|
|
}
|
|
|
|
|
2024-01-17 11:13:01 +00:00
|
|
|
for (const element of document.querySelectorAll(".hidden-by-default")) {
|
|
|
|
element.classList.remove("hidden-by-default");
|
2022-11-22 13:17:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 11:13:01 +00:00
|
|
|
document.querySelector(".subtitle")!.textContent =
|
|
|
|
"Someone shared a redirect with you!";
|
2022-11-22 13:17:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function insertText(key: keyof Redirect, value: string): void {
|
|
|
|
const insert = (selector: string, text: string) => {
|
|
|
|
document.querySelector(selector)!.textContent = text;
|
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line unicorn/prefer-switch
|
2024-01-17 11:13:01 +00:00
|
|
|
if (key === "matcherType") {
|
|
|
|
insert(".matcher-type", value);
|
|
|
|
} else if (key === "matcherValue") {
|
|
|
|
insert(".matcher-value", value);
|
|
|
|
} else if (key === "redirectType") {
|
|
|
|
insert(".redirect-type", value);
|
|
|
|
} else if (key === "redirectValue") {
|
|
|
|
insert(".redirect-value", value);
|
2022-11-22 13:17:30 +00:00
|
|
|
} else {
|
|
|
|
console.warn(`Unknown key: ${key as string}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function share(redirect: Redirect): string {
|
|
|
|
const encoded = encodeBase64(redirect);
|
|
|
|
const url = new URL(window.location.href);
|
|
|
|
url.hash = `${fragmentPrefix}${encoded}`;
|
|
|
|
return url.href;
|
|
|
|
}
|