| 7 | } |
| 8 | |
| 9 | async function checkNotifications() { |
| 10 | con.log('checkNotifications'); |
| 11 | const url = 'https://kissanimelist.firebaseio.com/Data2/Notification/Current.json'; |
| 12 | const response = await api.request.xhr('GET', url); |
| 13 | const current = parseInt(JSON.parse(response.responseText)); |
| 14 | if (Number.isNaN(current)) con.error('Could not read current Notification number'); |
| 15 | |
| 16 | con.log('Current Notification', current); |
| 17 | const last = parseInt(await api.storage.get('firebaseNotification')); |
| 18 | const next = last + 1; |
| 19 | |
| 20 | if (typeof last === 'undefined' || Number.isNaN(last)) { |
| 21 | api.storage.set('firebaseNotification', current); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | if (current < next) { |
| 26 | con.log('No new notifications'); |
| 27 | |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const notificationUrl = `https://kissanimelist.firebaseio.com/Data2/Notification/list/N${next}.json`; |
| 32 | const resNotification = await api.request.xhr('GET', notificationUrl); |
| 33 | |
| 34 | if (!resNotification) return; |
| 35 | |
| 36 | const message = JSON.parse(resNotification.responseText); |
| 37 | if (!message) { |
| 38 | con.info('Notification empty', resNotification.responseText); |
| 39 | api.storage.set('firebaseNotification', next).then(function () { |
| 40 | checkNotifications(); |
| 41 | }); |
| 42 | |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | j.$(document).ready(function () { |
| 47 | const flashm = utils.flashm( |
| 48 | `<div style="text-align: left;">${message}</div><button class="okChangelog" style="background-color: transparent; border: none; color: rgb(255,64,129);margin-top: 10px;cursor: pointer;">Close</button>`, |
| 49 | { permanent: true, position: 'top' }, |
| 50 | ); |
| 51 | flashm.find('.okChangelog').click(function () { |
| 52 | flashm.remove(); |
| 53 | api.storage.set('firebaseNotification', next).then(function () { |
| 54 | checkNotifications(); |
| 55 | }); |
| 56 | }); |
| 57 | }); |
| 58 | } |