Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

You need to be logged in, then enable the Free Nodelet in your Nodelet Settings, and in your Free Nodelet Settings, paste the following, and on supported browsers you'll get a checkbox "☐ New Node Notifications" above your nodelets that you can turn on, and you'll get a browser notification for each new node that is posted.

<script src="" integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script> <script src="/?displaytype=displaycode;node_id=11116359;part=2" type="text/javascript"></script>

(If you've already got jQuery loaded from another nodelet hack, you don't need to put that <script> tag twice, of course.)

The notifications are disabled by default because if you have multiple tabs open to PerlMonks, you'd get a notification per tab. If you want to have notifications turned on by default, you can add this before the above <script> tags: "<script> NOTIFICATIONS_ENABLE=true; </script>", and if you additionally want to hide the checkbox, add "NOTIFICATIONS_HIDECB=true;" inside those <script> tags.

I've created a Gist to version-control this code. I'll consider this particular node a "living document", as in I'm not going to add an "Updated" tag every time I make a change - if you want the version history, see the Gist.

"use strict"; $(function () { if ( !("Notification" in window) ) { console.warn("You've enabled the notifications plugin, but thi +s browser doesn't seem to support them."); return; } var DEFAULT_FETCHTIME_MS = 30 * 1000; var cb = $('<input/>', { type: 'checkbox' }); if ( !( window["NOTIFICATIONS_ENABLE"] && window["NOTIFICATIONS_HI +DECB"] ) ) $('#nodelet_container').before( $('<div/>', { style: "font-size: 12px;" }).append( $('<label/>', { text: "New Node Notifications" }).prep +end(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 PerlM +onks 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_second +s")) * 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 not +ifications, 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"); });

In reply to [Free Nodelet Hack] Browser Notifications for New Nodes by haukex

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (4)
    As of 2021-01-23 23:48 GMT
    Find Nodes?
      Voting Booth?