Fix variable scope

TIL: Variables are global by default

/me facepalms
main
Jerry Aldrich 5 years ago
parent eec76e8e30
commit 8f61668d1c
  1. 72
      player.html

@ -103,7 +103,7 @@
<script>
function loadStreamChoices(data) {
var selectItem = document.getElementById('streamSelection')
streams = data["streams"]
var streams = data["streams"]
for(let i in streams) {
var opt = document.createElement('option')
if(data.https_streams == true) {
@ -133,8 +133,8 @@
function buildTitleLine(width, title) {
width = width - 2 // Subtracting 2 here to account for "|"
spacing = Math.floor((width - title.length)/2)
line = "|" + " ".repeat(spacing) + title + " ".repeat(spacing)
var spacing = Math.floor((width - title.length)/2)
var line = "|" + " ".repeat(spacing) + title + " ".repeat(spacing)
if(spacing + title.length + spacing != width) {
line = line + " |" // Handle title not being even
} else {
@ -144,10 +144,10 @@
}
function getSourceInfo(data) {
sources = data["icestats"]["source"]
var sources = data["icestats"]["source"]
for(let i in sources) {
selectedStream = document.getElementById('streamSelection').value.replace(/^.*[\\\/]/, '')
icecastStream = sources[i]["listenurl"].replace(/^.*[\\\/]/, '')
var selectedStream = document.getElementById('streamSelection').value.replace(/^.*[\\\/]/, '')
var icecastStream = sources[i]["listenurl"].replace(/^.*[\\\/]/, '')
if(selectedStream == icecastStream) {
sources[i]["streamurl"] = document.getElementById('streamSelection').value
return sources[i]
@ -156,8 +156,8 @@
}
function fetchText(statusData, key) {
text = ""
streamInfo = getSourceInfo(statusData)
var text = ""
var streamInfo = getSourceInfo(statusData)
if(streamInfo && streamInfo[key]) {
text = String(streamInfo[key])
}
@ -165,54 +165,54 @@
}
function renderedLength(text) {
regEx = /&#\d+;/g
var regEx = /&#\d+;/g
if (text.match(regEx) !== null) {
return text.match(/&#\d+;|./g).length
}
return text.length
}
function trimLine(text, width) {
// If text has unicode char, a different trim is needed due to rendering
function trimLine(line, width) {
// If line has unicode char, a different trim is needed due to rendering
// unicode characters like "&#38754;" as one "character".
containsUnicode = text.match(/&#\d+;/)
if (containsUnicode !== null) {
chars = text.match(/&#\d+;|./g)
numUnicodes = text.match(/&#\d+;/g).length
var unicodeChars = line.match(/&#\d+;/)
if (unicodeChars !== null) {
var chars = line.match(/&#\d+;|./g)
var numUnicodes = line.match(/&#\d+;/g).length
chars.splice(-1, 1) // Remove "|"
text = chars.join("")
fudge = 0.69 // TODO: Fix this...non-monospaced fonts may kill me
spacing = width - renderedLength(text) - (numUnicodes * fudge)
var text = chars.join("")
var fudge = 0.69 // TODO: Fix this...non-monospaced fonts may kill me
var spacing = width - renderedLength(text) - (numUnicodes * fudge)
if (spacing < 0) {
spacing = 0
}
text = text + " ".repeat(spacing) + "|"
return text + " ".repeat(spacing) + "|"
} else {
if (text.length > width) {
end = (width - text.length)
start = width - 2 // Subtract 2 for " |"
text = text.substring(end, start) + " |"
if (line.length > width) {
var end = (width - line.length)
var start = width - 2 // Subtract 2 for " |"
return line.substring(end, start) + " |"
}
}
return text
return line
}
function buildLine(width, text, prefix) {
prefix = "|" + prefix
// Subtract 2 for " |"
spacing = (width - prefix.length - text.length - 2)
var spacing = (width - prefix.length - text.length - 2)
if(spacing < 0) {
spacing = 0
}
line = prefix + text + " ".repeat(spacing) + " |"
var line = prefix + text + " ".repeat(spacing) + " |"
return trimLine(line, width)
}
function buildDownloadLine(width, url) {
line = "| Download: "
var line = "| Download: "
if (url != "") {
line = line + "<a href='" + url + ".m3u" + "'>M3U</a>"
line = line + "/"
@ -229,9 +229,9 @@
statusXMLHTTP.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var streamData = JSON.parse(this.responseText);
songName = fetchText(streamData, "title")
listeners = fetchText(streamData, "listeners")
streamURL = fetchText(streamData, "streamurl")
var songName = fetchText(streamData, "title")
var listeners = fetchText(streamData, "listeners")
var streamURL = fetchText(streamData, "streamurl")
renderStreamInfo(songName, listeners, streamURL)
}
};
@ -240,18 +240,18 @@
}
function renderStreamInfo(songName="", listeners="", streamURL="") {
width = 60
var width = 60
// Use an empty title while config.json is being loaded
if (typeof CONFIG_DATA !== 'undefined') {
titleLine = buildTitleLine(width, CONFIG_DATA["title"])
var titleLine = buildTitleLine(width, CONFIG_DATA["title"])
} else {
titleLine = buildTitleLine(width, "")
var titleLine = buildTitleLine(width, "")
}
songNameLine = buildLine(width, songName, " Song Title: ")
listenersLine = buildLine(width, listeners, " Listeners: ")
downloadLine = buildDownloadLine(width, streamURL)
var songNameLine = buildLine(width, songName, " Song Title: ")
var listenersLine = buildLine(width, listeners, " Listeners: ")
var downloadLine = buildDownloadLine(width, streamURL)
var streamLines = []
streamLines.push("/" + "-".repeat(width - 2) + "\\")

Loading…
Cancel
Save