Common.js: Difference between revisions
From Blood on the Clocktower Wiki
No edit summary |
No edit summary |
||
(24 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
return false; | return false; | ||
}); | }); | ||
var jinxesOpen = false; | |||
$(document).ready(function () { | $(document).ready(function () { | ||
handleAudioElements(); | handleAudioElements(); | ||
replaceRolesWithAbilities(); | |||
$("#jinxes > table").css("display", "none"); | |||
$("#jinx-toggle").on("click", function() { | |||
jinxesOpen = !jinxesOpen; | |||
$("#jinx-toggle").text(jinxesOpen ? "(Close)" : "(Open)") | |||
$("#jinxes > table").css("display", jinxesOpen ? "block" : "none"); | |||
}); | |||
}); | }); | ||
function replaceRolesWithAbilities() | |||
{ | |||
fetch("https://wiki.bloodontheclocktower.com/roles.json").then(function(resp) | |||
{ | |||
if (resp.ok) | |||
{ | |||
resp.json().then(function(roles) | |||
{ | |||
document.querySelectorAll("[data-role]").forEach(function(e) | |||
{ | |||
const filtered = roles.filter(function(r) | |||
{ | |||
return r.name === e.dataset.role | |||
}); | |||
if (filtered.length > 0) | |||
{ | |||
e.parentElement.title = ""; | |||
e.dataset.role = filtered[0].ability | |||
} | |||
else | |||
{ | |||
delete e.dataset.role; | |||
} | |||
}); | |||
}) | |||
} | |||
else | |||
{ | |||
document.querySelectorAll("[data-role]").forEach(function(e) | |||
{ | |||
delete e.dataset.role; | |||
}); | |||
} | |||
}); | |||
} | |||
function handleAudioElements() | function handleAudioElements() | ||
Line 26: | Line 74: | ||
if (format === 'mp3') { | if (format === 'mp3') { | ||
format = 'mpeg'; | format = 'mpeg'; | ||
} | |||
if (format === 'm4a') { | |||
format = 'mp4'; | |||
} | } | ||
Latest revision as of 08:04, 7 October 2024
$("li:contains('Toolbox')").hide(); $("img").contextmenu(function () { return false; }); var jinxesOpen = false; $(document).ready(function () { handleAudioElements(); replaceRolesWithAbilities(); $("#jinxes > table").css("display", "none"); $("#jinx-toggle").on("click", function() { jinxesOpen = !jinxesOpen; $("#jinx-toggle").text(jinxesOpen ? "(Close)" : "(Open)") $("#jinxes > table").css("display", jinxesOpen ? "block" : "none"); }); }); function replaceRolesWithAbilities() { fetch("https://wiki.bloodontheclocktower.com/roles.json").then(function(resp) { if (resp.ok) { resp.json().then(function(roles) { document.querySelectorAll("[data-role]").forEach(function(e) { const filtered = roles.filter(function(r) { return r.name === e.dataset.role }); if (filtered.length > 0) { e.parentElement.title = ""; e.dataset.role = filtered[0].ability } else { delete e.dataset.role; } }); }) } else { document.querySelectorAll("[data-role]").forEach(function(e) { delete e.dataset.role; }); } }); } function handleAudioElements() { document.querySelectorAll('.html5audio:not(.loaded)').forEach(function (div) { var data = div.dataset; var file = data.file; if (!file) { return; } var format = file.split('.').pop(); var preload = data.preload; var download = data.download; var options = data.options; var opts = { controls: '' }; var volume = Number(Number(data.volume).toFixed(1)); if (format === 'mp3') { format = 'mpeg'; } if (format === 'm4a') { format = 'mp4'; } if (preload !== 'auto' || preload !== 'metadata') { preload = 'none'; } opts.preload = preload; if (download === 'false') { opts.controlsList = 'nodownload'; } if (options) { var valid = ['autoplay', 'loop', 'muted']; options.split(',').forEach(function (el) { el = el.trim(); if (valid.indexOf(el) !== -1) { opts[el] = ''; } }); } var audio = document.createElement('audio'); Object.keys(opts).forEach(function (attr) { var value = opts[attr]; audio.setAttribute(attr, value); }); var source = document.createElement('source'); source.src = file; source.type = 'audio/' + format; audio.append(source); if (volume >= 0 && volume <= 1) { audio.volume = volume; } div.innerHTML = ''; div.appendChild(audio); div.classList.add('loaded'); }) }