Notifikationer fra metalligaen.dk
if (localStorage.getItem("metalliganot_following") == null)
{
localStorage.setItem("metalliganot_following", "[]");
}
function addFollowButtons()
{
const games = document.querySelectorAll(".live .game");
for (let i=0; i < games.length; i++)
{
const game = games[i];
const gameId = game.getAttribute("id");
let checker = document.createElement("button");
checker.setAttribute("data-game-id", gameId);
checker.innerText = JSON.parse(localStorage.getItem("metalliganot_following")).indexOf(gameId) > -1 ? "Følg ikke" : "Følg"
checker.addEventListener("click", function() {
toggleFollowing(gameId);
this.innerText = this.innerText == "Følg" ? "Følg ikke" : "Følg";
});
game.insertBefore(checker, game.firstChild);
}
}
function toggleFollowing(gameId)
{
var following = JSON.parse(localStorage.getItem("metalliganot_following"));
var indexOfGameId = following.indexOf(gameId);
if (indexOfGameId > -1) {
following.splice(indexOfGameId, 1);
}
else {
following.push(gameId);
}
localStorage.setItem("metalliganot_following", JSON.stringify(following));
}
function isFollowing(gameId)
{
var following = JSON.parse(localStorage.getItem("metalliganot_following"));
return following.indexOf(gameId) > -1;
}
var seenEvents = [];
function initSeenEvents()
{
fetch("/api/FallbackLive/Register?tournamentID=1374&getNews=false&getLiveBlog=false").then(function(response) {
return response.json();
}).then(function(games) {
console.log(games);
for (let i=0; i<games.length; i++)
{
var game = games[i];
for (let e=0; e<game.events.length; e++)
{
var ev = game.events[e];
ev.gameID = game.gameID;
seenEvents.push(JSON.stringify(ev));
}
}
});
}
function hasSeenEvent(gameID, ev)
{
ev.gameID = gameID;
return seenEvents.indexOf(JSON.stringify(ev)) > -1;
}
function notifyEvent(game, ev)
{
if(window.Notification && Notification.permission !== "denied") {
Notification.requestPermission(function(status) { // status is "granted", if accepted by user
new Notification(game.homeTeamName + " - " + game.awayTeamName, {
body: ev.eventText,
icon: 'http://www.hockeyligaen.dk/' + game.gameID + '.png' // optional
});
});
}
}
setInterval(function() {
fetch("/api/FallbackLive/Register?tournamentID=1374&getNews=false&getLiveBlog=false").then(function(response) {
return response.json();
}).then(function(games) {
console.log(games);
for (let i=0; i<games.length; i++)
{
const game = games[i];
if (isFollowing(game.gameID.toString()))
{
console.log("following " + game.gameID);
var events = game.events.reverse().filter(ev => seenEvents.indexOf(JSON.stringify(ev)) < 0);
for (let e=0; e<events.length; e++)
{
var ev = events[e];
if (!hasSeenEvent(game.gameID, ev))
{
console.log(ev);
seenEvents.push(JSON.stringify(events[e]));
if (["Penalty","Goal","Period", "Comment", "Blog", "Spectators"].indexOf(ev.eventType) > -1)
{
notifyEvent(game, ev);
}
}
}
}
}
});
}, 15000);
initSeenEvents();
addFollowButtons();