ΔΙΑΦΗΜΙΣΗ
` + adArray[ad_number % 3].replaceAll(‘gpt-passback-liveblog-1’, ‘gpt-passback-liveblog-‘ + ad_number) + `
`;
 if (i === 0){
 events[i].insertAdjacentElement(’afterend’, adBlock);
 } else {
 events[i].insertAdjacentElement(’beforebegin’, adBlock);
 }
 adBlock.querySelectorAll(’script’).forEach(oldScript => {
 const newScript = document.createElement(’script’);
 if (oldScript.src) {
 newScript.src = oldScript.src;
 newScript.async = oldScript.async;
 } else {
 newScript.textContent = oldScript.textContent;
 }
 oldScript.replaceWith(newScript);
 });
 }
 }
 });

 function playSound(filePath) {
 const audio = new Audio(filePath);
 audio.play()
 .then(() => console.log(`Playing sound: ${filePath}`))
 .catch(error => console.error(`Error playing sound: ${error}`));
 }

 function update_event_timeline(contentID, limit = null, format = null) {
 let url = ‘/wp-admin/admin-ajax.php?action=gem_get_post_timeline&id=’+contentID;
 if (limit){
 url += ‘&limit=’+limit;
 }
 if (format){
 url += ‘&format=1’;
 }
 fetch(url)
 .then(response => {
 if (!response)
 return;
 response.text().then(function (text) {
 const parser = new DOMParser();
 const response_doc = parser.parseFromString(text, “text/html”);
 const newTimeline = response_doc.querySelector(’.timeline’);
 const liveTimeline = document.querySelector(’.timeline’);

 let count = 0;
 Array.from(liveTimeline.children).forEach(existingChild => {
 const foundInNewTimeline = Array.from(newTimeline.children).some(newChild => newChild.id === existingChild.id);
 if (!foundInNewTimeline && !existingChild.classList.contains(’advert-block’)) {
 liveTimeline.removeChild(existingChild);
 }
 });

 Array.from(newTimeline.children).reverse().forEach(newChild => {
 let not_exists = true;
 Array.from(liveTimeline.children).some(existingChild => {
 if (existingChild.id === newChild.id) {
 if (existingChild.dataset.time === newChild.dataset.time) {
 if (existingChild.dataset.updated < newChild.dataset.updated) {
 liveTimeline.replaceChild(newChild.cloneNode(true), existingChild);
 }
 not_exists = false;
 }
 return true; // break loop
 }
 return false;
 });
 const clonedNode = newChild.cloneNode(true);
 if (not_exists) {
 const pinnedElements = liveTimeline.querySelectorAll(’[data-event-pinned=”1″]’);
 const lastPinned = pinnedElements[pinnedElements.length – 1];
 if (newChild.dataset.eventPinned === “1”) {
 liveTimeline.prepend(clonedNode);
 } else {
 if (lastPinned) {
 lastPinned.insertAdjacentElement(’afterend’, clonedNode);
 } else {
 liveTimeline.prepend(clonedNode);
 }
 }
 if (format && limit && liveTimeline.childElementCount > limit) {
 liveTimeline.removeChild(liveTimeline.lastChild);
 }
 const eventCount = liveTimeline.querySelectorAll(’.timeline-item’);
 if (eventCount.length % 20 === 5 || eventCount.length === 15){
 const adverts = liveTimeline.querySelectorAll(’.advert-block’);
 const adNumber = adverts.length % 3;
 if (Number.isInteger(adNumber)) {
 const adBlock = document.createElement(’div’);
 ad_number++;
 adBlock.className = ‘advert-block text-center fw my-2’;
 adBlock.innerHTML = `
ΔΙΑΦΗΜΙΣΗ
`+adArray[ad_number%3].replaceAll(‘gpt-passback-liveblog-1’, ‘gpt-passback-liveblog-‘ + ad_number)+`
`;
 if (lastPinned) {
 lastPinned.insertAdjacentElement(’afterend’, adBlock);
 } else {
 clonedNode.insertAdjacentElement(’afterend’, adBlock);
 }
 adBlock.querySelectorAll(’script’).forEach(oldScript => {
 const newScript = document.createElement(’script’);
 if (oldScript.src) {
 newScript.src = oldScript.src;
 newScript.async = oldScript.async;
 } else {
 newScript.textContent = oldScript.textContent;
 }
 oldScript.replaceWith(newScript);
 });
 }
 }
 count++;
 }
 });
 if (count > 0) {
 const events = Array.from(liveTimeline.children);
 const scripts = Array.from(document.body.querySelectorAll(’body > script’));
 const newScripts = [];

 events.forEach(ch => {
 ch.querySelectorAll(’script’).forEach(script => {
 if (script.src) {
 if (!newScripts.some(s => s.src === script.src)) {
 newScripts.push(script);
 }
 }
 });
 });
 console.info(”Added ” + count + ” timeline events”);
 playSound(’/wp-content/themes/generatepress_child/assets/sounds/notification.mp3’);
 newScripts.forEach(newScript => {
 scripts.forEach(script => {
 if ((script.src && script.src === newScript.src) ||
 (!script.src && script.textContent === newScript.textContent)) {
 document.body.removeChild(script);
 }
 });
 const scriptTag = document.createElement(’script’);
 Array.from(newScript.attributes).forEach(attr => {
 scriptTag.setAttribute(attr.name, attr.value);
 });
 if (newScript.src) scriptTag.src = newScript.src;
 if (newScript.textContent) scriptTag.textContent = newScript.textContent;
 document.body.appendChild(scriptTag);
 });
 }
 });
 })
 .catch(error => {
 console.error(’Error:’, error);
 });
 }

 function update_match_score(contentID) {
 let url = ‘/wp-admin/admin-ajax.php?action=gem_get_timeline_header&id=’+contentID;
 fetch(url)
 .then(response => {
 if (!response)
 return;
 response.json().then(function (json) {
 const match_data = json?.data;
 if (match_data?.match_score_desc){
 const period = document.querySelector(’.match_score_desc’);
 if (period){
 period.innerText = match_data.match_score_desc;
 }
 }
 if (match_data?.match_type === ‘match’){
 const homeScoreEl = document.querySelector(’.home_score’);
 if (homeScoreEl && match_data?.score1Teama) {
 homeScoreEl.innerText = match_data.score1Teama;
 }
 const awayScoreEl = document.querySelector(’.away_score’);
 if (awayScoreEl && match_data?.score1Teamb) {
 awayScoreEl.innerText = match_data.score1Teamb;
 }
 } else if (match_data?.match_type === ‘tennis’){
 let scoreList = [];
 for (let i = 1; i<=5; i++){
 let home = match_data[’score’ + i + ‘Teama’];
 let away = match_data[’score’ + i + ‘Teamb’];
 if (Number.isInteger(home) || Number.isInteger(away)){
 let round = {’home_score’: Number.isInteger(home) ? home : 0,
 ‘away_score’: Number.isInteger(away) ? away : 0,
 ‘winner’: home > away ? ‘home’ : ‘away’};
 scoreList.push(round);
 }
 }
 const scoreboard = document.querySelector(’.scoreboard-tennis’);
 if (!scoreboard) return;
 const homeScoresParent = scoreboard.querySelectorAll(’.score’)[0];
 const awayScoresParent = scoreboard.querySelectorAll(’.score’)[1];
 const matchEnded = document.querySelector(’[data-event-type=”match_ended”]’);
 for (let i = 1; i<=scoreList.length; i++){
 let home_scores = homeScoresParent.getElementsByClassName(’home_round_score’);
 let away_scores = awayScoresParent.getElementsByClassName(’away_round_score’);
 let round = scoreList[i-1];
 if (round){
 let home_score = home_scores[i-1];
 let away_score = away_scores[i-1];
 if (Number.isInteger(round.home_score) || Number.isInteger(round.away_score)) {
 if (home_score){
 home_score.innerText = round.home_score;
 } else {
 home_score = document.createElement(’strong’);
 home_score.classList.add(’home_round_score’);
 home_score.innerText = round.home_score;
 homeScoresParent.appendChild(home_score);
 }
 if (away_score){
 away_score.innerText = round.away_score;
 } else {
 away_score = document.createElement(’strong’);
 away_score.classList.add(’away_round_score’);
 away_score.innerText = round.away_score;
 awayScoresParent.appendChild(away_score);
 }
 if (scoreList.length !== i) {
 if (round.winner === ‘home’) {
 away_score.classList.add(’disabled’);
 } else if (round.winner === ‘away’) {
 home_score.classList.add(’disabled’);
 }
 } else {
 if (matchEnded){
 home_score.classList.remove(’disabled’);
 away_score.classList.remove(’disabled’);
 home_score.classList.add(’text-red’);
 away_score.classList.add(’text-red’);
 }
 }
 }
 }
 }
 }
 });
 })
 .catch(error => {
 console.error(’Error:’, error);
 });
 }

 document.addEventListener(’DOMContentLoaded’, function () {
 update_match_score(’4809172’);
 update_event_timeline(’4809172’);

 document.querySelector(’#timeline-loader’)?.classList.add(’hidden’);
 });
 setInterval(() => update_match_score(’4809172’), 5000);
 setInterval(() => update_event_timeline(’4809172’), 5000);