Convert to old javascript for compatibility

This commit is contained in:
lovasoa 2021-04-01 20:40:55 +02:00
parent 4439457520
commit b0a428ab29
No known key found for this signature in database
GPG key ID: AC8DB8E033B44AB8
3 changed files with 26 additions and 30 deletions

View file

@ -75,6 +75,6 @@
</footer>
</body>
<script src="../js/index.js"></script>
<script src="../js/index.js" async defer></script>
</html>

View file

@ -94,29 +94,26 @@ Tools.boardName = (function () {
Tools.socket.emit("getboard", Tools.boardName);
function saveBoardNametoLocalStorage() {
const boardName = Tools.boardName;
var boardName = Tools.boardName;
if (boardName.toLowerCase() === 'anonymous') return;
var recentBoards, key = "recent-boards";
try {
const key = "recent-boards";
let recentBoards = JSON.parse(localStorage.getItem(key)) || [];
const nameIndex = recentBoards.findIndex((name) => {
return name.toLowerCase() === boardName.toLowerCase();
});
if (nameIndex > -1) recentBoards.splice(nameIndex, 1);
if (recentBoards.length === 20) recentBoards.pop();
recentBoards = [boardName, ...recentBoards];
localStorage.setItem(key, JSON.stringify(recentBoards));
} catch (e) {
console.error("Unable to update localStorage.", e);
recentBoards = JSON.parse(localStorage.getItem(key));
if (!Array.isArray(recentBoards)) throw new Error("Invalid type");
} catch(e) {
// On localstorage or json error, reset board list
recentBoards = [];
console.log("Board history loading error", e);
}
recentBoards = recentBoards.filter(function (name) {
return name !== boardName;
});
recentBoards.unshift(boardName);
recentBoards = recentBoards.slice(0, 20);
localStorage.setItem(key, JSON.stringify(recentBoards));
}
saveBoardNametoLocalStorage();
// Refresh recent boards list on each page show
window.addEventListener("pageshow", saveBoardNametoLocalStorage);
Tools.HTML = {
template: new Minitpl("#tools > .tool"),

View file

@ -1,21 +1,18 @@
window.addEventListener("pageshow", () => showRecentBoards());
function showRecentBoards() {
const parent = document.getElementById("recent-boards");
const ul = document.querySelector("#recent-boards ul");
var parent = document.getElementById("recent-boards");
var ul = document.querySelector("#recent-boards ul");
ul && parent.removeChild(ul);
parent.classList.add("hidden");
const recentBoards = JSON.parse(localStorage.getItem("recent-boards")) || [];
var recentBoards = JSON.parse(localStorage.getItem("recent-boards")) || [];
if (recentBoards.length === 0) return;
const list = document.createElement("ul");
var list = document.createElement("ul");
recentBoards.forEach(function(name) {
const listItem = document.createElement("li");
const link = document.createElement("a");
link.setAttribute("href", `/boards/${name}`);
var listItem = document.createElement("li");
var link = document.createElement("a");
link.setAttribute("href", `/boards/${encodeURIComponent(name)}`);
link.textContent = name;
listItem.appendChild(link);
list.appendChild(listItem);
@ -24,3 +21,5 @@ function showRecentBoards() {
parent.appendChild(list);
parent.classList.remove("hidden");
}
window.addEventListener("pageshow", showRecentBoards);