Created
March 17, 2018 15:37
-
-
Save bodia-uz/2f0369033f4e1efce6c1998bcef7c4d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NOTIFICATION_GRUNTED = "granted"; | |
const NOTIFICATION_DENIED = "denied"; | |
const NOTIFICATION_DENIED_MESSAGE = "Notification permission denied"; | |
const NOTIFICATION_UNSUPPORTED_MESSAGE = | |
"Desktop notifications not available in your browser. Try Chromium."; | |
function initNotifications() { | |
return new Promise((resove, reject) => { | |
switch (Notification && Notification.permission) { | |
case undefined: | |
reject(NOTIFICATION_UNSUPPORTED_MESSAGE); | |
return; | |
case NOTIFICATION_DENIED: | |
reject(NOTIFICATION_DENIED_MESSAGE); | |
return; | |
case NOTIFICATION_GRUNTED: | |
resove(); | |
return; | |
default: | |
Notification.requestPermission(permission => { | |
if (permission === NOTIFICATION_GRUNTED) { | |
resove(); | |
} else { | |
reject(NOTIFICATION_DENIED_MESSAGE); | |
} | |
}); | |
} | |
}); | |
} | |
function notify(title, message, favicon) { | |
initNotifications() | |
.then(() => { | |
let notification = new Notification(title, { | |
icon: favicon || "/favicon.ico", | |
body: message | |
}); | |
notification.onclick = function() { | |
notification.close(); | |
self.focus(); | |
}; | |
}) | |
.catch(error => { | |
console.warn(error); | |
}); | |
} | |
if (typeof _onListResult === "undefined") { | |
var _onListResult = _onListResult || UITrainList._instance._onListResult; | |
UITrainList._instance._onListResult = function() { | |
customOnListResult.apply(this, arguments); | |
_onListResult.apply(this, arguments); | |
}; | |
} | |
var prevResult; | |
function customOnListResult(result) { | |
var resultStr = JSON.stringify(result.data.list); | |
console.log(result.data.list); | |
if (resultStr !== prevResult) { | |
prevResult = resultStr; | |
notify(resultStr); | |
} | |
} | |
var POLLING_INTERVAL = 1 * 10 * 1000; | |
setInterval( | |
UITrainSearch._instance._onSubmit, | |
POLLING_INTERVAL, | |
UITrainSearch._instance | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment