90 lines
2.4 KiB
JavaScript
90 lines
2.4 KiB
JavaScript
function isHexPubkey(value) {
|
|
return /^[a-f0-9]{64}$/i.test(String(value || '').trim());
|
|
}
|
|
|
|
function decodePubkeyInput(value) {
|
|
const raw = String(value || '').trim();
|
|
if (!raw) return '';
|
|
if (isHexPubkey(raw)) return raw.toLowerCase();
|
|
if (raw.startsWith('npub1')) {
|
|
try {
|
|
const decoded = window?.NostrTools?.nip19?.decode?.(raw);
|
|
if (decoded?.type === 'npub' && isHexPubkey(decoded?.data)) {
|
|
return String(decoded.data).toLowerCase();
|
|
}
|
|
} catch {
|
|
return '';
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function encodeNpub(pubkeyHex) {
|
|
const normalized = isHexPubkey(pubkeyHex) ? String(pubkeyHex).toLowerCase() : '';
|
|
if (!normalized) return '';
|
|
try {
|
|
return window?.NostrTools?.nip19?.npubEncode
|
|
? window.NostrTools.nip19.npubEncode(normalized)
|
|
: '';
|
|
} catch {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
function getTargetPubkeyFromUrl() {
|
|
const params = new URLSearchParams(window.location.search || '');
|
|
const npub = String(params.get('npub') || '').trim();
|
|
const pubkey = String(params.get('pubkey') || '').trim();
|
|
return decodePubkeyInput(npub || pubkey);
|
|
}
|
|
|
|
function getExplicitAuthModeFromUrl() {
|
|
const params = new URLSearchParams(window.location.search || '');
|
|
const explicitAuth = String(params.get('auth') || '').trim().toLowerCase();
|
|
if (explicitAuth === 'required' || explicitAuth === 'optional' || explicitAuth === 'none') {
|
|
return explicitAuth;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function getShowFromUrl() {
|
|
const params = new URLSearchParams(window.location.search || '');
|
|
return String(params.get('show') || '').trim().toLowerCase();
|
|
}
|
|
|
|
function getEpisodeFromUrl() {
|
|
const params = new URLSearchParams(window.location.search || '');
|
|
return String(params.get('episode') || '').trim();
|
|
}
|
|
|
|
function updateUrlSearchParams(updates = {}, { replace = true } = {}) {
|
|
const url = new URL(window.location.href);
|
|
Object.entries(updates).forEach(([key, value]) => {
|
|
const safeKey = String(key || '').trim();
|
|
if (!safeKey) return;
|
|
const safeValue = String(value || '').trim();
|
|
if (safeValue) {
|
|
url.searchParams.set(safeKey, safeValue);
|
|
} else {
|
|
url.searchParams.delete(safeKey);
|
|
}
|
|
});
|
|
|
|
if (replace) {
|
|
window.history.replaceState({}, '', url.toString());
|
|
} else {
|
|
window.history.pushState({}, '', url.toString());
|
|
}
|
|
}
|
|
|
|
export {
|
|
isHexPubkey,
|
|
decodePubkeyInput,
|
|
encodeNpub,
|
|
getTargetPubkeyFromUrl,
|
|
getExplicitAuthModeFromUrl,
|
|
getShowFromUrl,
|
|
getEpisodeFromUrl,
|
|
updateUrlSearchParams,
|
|
};
|