/* * Copyright (C) 2016-2020 phantombot.github.io/PhantomBot * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ $(function () { var webSocket = getWebSocket(), queryMap = getQueryMap(), isPlaying = false, isDebug = localStorage.getItem('phantombot_alerts_debug') === 'true' || false; queue = []; /* * @function Gets a new instance of the websocket. * * @return {ReconnectingWebSocket} */ function getWebSocket() { let socketUri = ((window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + '/ws/alertspolls'), // URI of the socket. reconnectInterval = 5000; // How often in milliseconds we should try reconnecting. return new ReconnectingWebSocket(socketUri, null, { reconnectInterval: reconnectInterval }); } /* * @function Parses the query params in the URL and puts them into a map. * * @return {Map} */ function getQueryMap() { let queryString = window.location.search, // Query string that starts with ? queryParts = queryString.substr(1).split('&'), // Split at each &, which is a new query. queryMap = new Map(); // Create a new map for save our keys and values. for (let i = 0; i < queryParts.length; i++) { let key = queryParts[i].substr(0, queryParts[i].indexOf('=')), value = queryParts[i].substr(queryParts[i].indexOf('=') + 1, queryParts[i].length); if (key.length > 0 && value.length > 0) { queryMap.set(key, value); } } return queryMap; } /* * @function Prints debug logs. * * @param {String} message */ function printDebug(message, force) { if (isDebug || force) { console.log('%c[PhantomBot Log]', 'color: #6441a5; font-weight: 900;', message); } } /* * @function Toggles the debug mode. * * @param {String} toggle */ window.toggleDebug = function (toggle) { localStorage.setItem('phantombot_alerts_debug', toggle.toString()); // Refresh the page. window.location.reload(); } /* * @function Checks if the query map has the option, if not, returns default. * * @param {String} option * @param {String} def * @return {String} */ function getOptionSetting(option, def) { if (queryMap.has(option)) { return queryMap.get(option); } else { return def; } } /* * @function Sends a message to the socket * * @param {String} message */ function sendToSocket(message) { try { webSocket.send(JSON.stringify(message)); } catch (ex) { printDebug('Failed to send a message to the socket: ' + ex.stack); } } //Copied from https://davidwalsh.name/detect-supported-audio-formats-javascript function supportsAudioType(type) { let audio; // Allow user to create shortcuts, i.e. just "mp3" let formats = { mp3: 'audio/mpeg', aac: 'audio/aac', ogg: 'audio/ogg; codecs="vorbis"' }; if (!audio) { audio = document.createElement('audio'); } let ret = audio.canPlayType(formats[type] || type); if (getOptionSetting('show-debug', 'false') === 'true') { $('.main-alert').append('
supportsAudioType(' + type + '): ' + ret); } return ret; } //Copied from https://davidwalsh.name/detect-supported-video-formats-javascript function supportsVideoType(type) { let video; // Allow user to create shortcuts, i.e. just "webm" let formats = { ogg: 'video/ogg; codecs="theora"', ogv: 'video/ogg; codecs="theora"', webm: 'video/webm; codecs="vp8, vorbis"', mp4: 'video/mp4' }; if (!video) { video = document.createElement('video'); } let ret = video.canPlayType(formats[type] || type); if (getOptionSetting('show-debug', 'false') === 'true') { $('.main-alert').append('
supportsVideoType(' + type + '): ' + ret); } return ret; } /* * @function Handles the user interaction for the page. */ function handleBrowserInteraction() { const audio = new Audio(); // Try to play to see if we can interact. audio.play().catch(function (err) { // User need to interact with the page. if (err.toString().startsWith('NotAllowedError')) { $('.main-alert').append($('