mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-22 03:29:31 -07:00
Add comment about not needing to use safeJson()
- Fix indentation in media.mjs
This commit is contained in:
@@ -126,6 +126,7 @@ const init = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const autocompleteOnSelect = async (suggestion) => {
|
const autocompleteOnSelect = async (suggestion) => {
|
||||||
|
// Note: it's fine that this uses json instead of safeJson since it's infrequent and user-initiated
|
||||||
const data = await json('https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find', {
|
const data = await json('https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find', {
|
||||||
data: {
|
data: {
|
||||||
text: suggestion.value,
|
text: suggestion.value,
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ class AutoComplete {
|
|||||||
|
|
||||||
let result = this.cachedResponses[search];
|
let result = this.cachedResponses[search];
|
||||||
if (!result) {
|
if (!result) {
|
||||||
// make the request
|
// make the request; using json here instead of safeJson is fine because it's infrequent and user-initiated
|
||||||
const resultRaw = await json(url);
|
const resultRaw = await json(url);
|
||||||
|
|
||||||
// use the provided parser
|
// use the provided parser
|
||||||
|
|||||||
@@ -20,45 +20,44 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const scanMusicDirectory = async () => {
|
const scanMusicDirectory = async () => {
|
||||||
const parseDirectory = async (path, prefix = "") => {
|
const parseDirectory = async (path, prefix = '') => {
|
||||||
const listing = await text(path);
|
const listing = await text(path);
|
||||||
const matches = [...listing.matchAll(/href="([^\"]+\.mp3)"/gi)];
|
const matches = [...listing.matchAll(/href="([^"]+\.mp3)"/gi)];
|
||||||
return matches.map((m) => `${prefix}${m[1]}`);
|
return matches.map((m) => `${prefix}${m[1]}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let files = await parseDirectory("music/");
|
let files = await parseDirectory('music/');
|
||||||
if (files.length === 0) {
|
if (files.length === 0) {
|
||||||
files = await parseDirectory("music/default/", "default/");
|
files = await parseDirectory('music/default/', 'default/');
|
||||||
}
|
}
|
||||||
return { availableFiles: files };
|
return { availableFiles: files };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Unable to scan music directory");
|
console.error('Unable to scan music directory');
|
||||||
console.error(e);
|
console.error(e);
|
||||||
return { availableFiles: [] };
|
return { availableFiles: [] };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const getMedia = async () => {
|
const getMedia = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('playlist.json');
|
const response = await fetch('playlist.json');
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
playlist = await response.json();
|
playlist = await response.json();
|
||||||
} else if (response.status === 404
|
} else if (response.status === 404
|
||||||
&& response.headers.get('X-Weatherstar') === 'true') {
|
&& response.headers.get('X-Weatherstar') === 'true') {
|
||||||
console.warn("Couldn't get playlist.json, falling back to directory scan");
|
console.warn("Couldn't get playlist.json, falling back to directory scan");
|
||||||
playlist = await scanMusicDirectory();
|
playlist = await scanMusicDirectory();
|
||||||
} else {
|
} else {
|
||||||
console.warn(`Couldn't get playlist.json: ${response.status} ${response.statusText}`);
|
console.warn(`Couldn't get playlist.json: ${response.status} ${response.statusText}`);
|
||||||
playlist = { availableFiles: [] };
|
playlist = { availableFiles: [] };
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn("Couldn't get playlist.json, falling back to directory scan");
|
console.warn("Couldn't get playlist.json, falling back to directory scan");
|
||||||
playlist = await scanMusicDirectory();
|
playlist = await scanMusicDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
enableMediaPlayer();
|
enableMediaPlayer();
|
||||||
};
|
};
|
||||||
|
|
||||||
const enableMediaPlayer = () => {
|
const enableMediaPlayer = () => {
|
||||||
@@ -219,11 +218,11 @@ const playerEnded = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const setTrackName = (fileName) => {
|
const setTrackName = (fileName) => {
|
||||||
const baseName = fileName.split('/').pop();
|
const baseName = fileName.split('/').pop();
|
||||||
const trackName = decodeURIComponent(
|
const trackName = decodeURIComponent(
|
||||||
baseName.replace(/\.mp3/gi, '').replace(/(_-)/gi, '')
|
baseName.replace(/\.mp3/gi, '').replace(/(_-)/gi, ''),
|
||||||
);
|
);
|
||||||
document.getElementById('musicTrack').innerHTML = trackName;
|
document.getElementById('musicTrack').innerHTML = trackName;
|
||||||
};
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
|||||||
Reference in New Issue
Block a user