hawk / files / public / js / main.js
main.js
| 1 | // set favicon based on environment |
| 2 | const isLocal = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1"; |
| 3 | if (isLocal) { |
| 4 | document.querySelector("link[rel='icon']").href = "images/favicon-local.png"; |
| 5 | } |
| 6 | |
| 7 | // set version and updated info |
| 8 | const params = { |
| 9 | version: hawkInfo.version, |
| 10 | updated: hawkInfo.updated |
| 11 | }; |
| 12 | document.getElementById("info").innerHTML = createTemplatedHtml('app-info', params) |
| 13 | |
| 14 | // handle redirect from Spotify |
| 15 | const urlParams = new URLSearchParams(window.location.search); |
| 16 | const code = urlParams.get('code'); |
| 17 | const error = urlParams.get('error'); |
| 18 | if (code) { |
| 19 | getToken(code).then(() => { |
| 20 | window.history.replaceState({}, document.title, '/'); |
| 21 | window.location = getSpotifyRedirectURL(); |
| 22 | }) |
| 23 | } else if (error) { |
| 24 | console.error(error); |
| 25 | window.history.replaceState({}, document.title, '/'); |
| 26 | window.location = getSpotifyRedirectURL(); |
| 27 | } |
| 28 | |
| 29 | // determine if the user is signed in or not |
| 30 | // hide and show appropriate elements, based on class names |
| 31 | const accessToken = localStorage.getItem('access_token'); |
| 32 | const userIsSignedIn = !accessToken |
| 33 | if (userIsSignedIn) { |
| 34 | hideOrShow('signedin', false); |
| 35 | hideOrShow('signedout', true); |
| 36 | } else { |
| 37 | hideOrShow('signedin', true); |
| 38 | hideOrShow('signedout', false); |
| 39 | setCurrentPlayingSong(); |
| 40 | setInterval(setCurrentPlayingSong, 5 * 1000); |
| 41 | } |
| 42 | |
| 43 | // periodically lookup the active device |
| 44 | configureDevicesList(); |
| 45 | |
| 46 | // configure times |
| 47 | configureTimesList(); |