Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://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="https://code.jquery.com/jquery-3.5.0.min.js" 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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found