####
"use strict";
$(function () {
if ( !("Notification" in window) ) {
console.warn("You've enabled the notifications plugin, but this browser doesn't seem to support them.");
return;
}
var DEFAULT_FETCHTIME_MS = 30 * 1000;
var cb = $('', { type: 'checkbox' });
if ( !( window["NOTIFICATIONS_ENABLE"] && window["NOTIFICATIONS_HIDECB"] ) )
$('#nodelet_container').before(
$('', { style: "font-size: 12px;" }).append(
$('', { text: "New Node Notifications" }).prepend(cb) ) );
var notifications_timer;
var start_notifications = function () {
var lastfetchtime = Math.round((new Date()).getTime() / 1000);
var fetchit = function () {
$.ajax({ url: "/", dataType: "xml",
data: { node_id: '30175', xmlstyle: 'flat',
sinceunixtime: lastfetchtime, ticker: 'yes' } })
.done(function (data) {
$("NODE", data).each(function () {
var node = $(this);
var type = node.attr("nodetype");
var author = node.attr("authortitle");
if ( type == "user" || author == "NodeReaper" ) return;
var text = author + " posted a new " + type + ": " + node.text(); // node.attr("createtime")
var uri = "/?" + $.param({ node_id: node.attr("node_id") });
console.debug(text, uri);
var notification = new Notification('New PerlMonks Node', {
body: text, icon: "/favicon.ico", badge: "/favicon.ico",
requireInteraction: true });
notification.onclick = function (event) {
window.location = uri;
};
});
notifications_timer = setTimeout(fetchit,
parseInt($("INFO", data).attr("min_poll_seconds")) * 1000 + 1000);
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.warn("AJAX error: " + textStatus + " / " + jqXHR.status + " " + errorThrown);
notifications_timer = setTimeout(fetchit, DEFAULT_FETCHTIME_MS);
});
lastfetchtime = Math.round((new Date()).getTime() / 1000);
};
notifications_timer = setTimeout(fetchit, DEFAULT_FETCHTIME_MS);
console.debug("Notifications initialized. Timer",
notifications_timer, "will fire in", DEFAULT_FETCHTIME_MS, "ms");
}
cb.change(function () {
if ( this.checked ) {
if ( Notification.permission == "default" ) {
cb.attr("disabled", true);
Notification.requestPermission(function (result) {
cb.removeAttr("disabled");
if ( result == "granted" ) start_notifications();
else cb[0].checked = false;
});
}
else if ( Notification.permission == "granted" )
start_notifications();
else {
alert("You've denied this page the ability to send notifications, please change this via your browser's settings.");
this.checked = false;
}
}
else {
clearTimeout(notifications_timer);
console.debug("Notifications stopped.");
}
});
if ( window["NOTIFICATIONS_ENABLE"] )
cb.prop("checked", true).trigger("change");
});