Enquirer Home Page | Twitter | Back to Improbable Island
 

ii-distract-notify

ii-distract-notify.user.js
// ==UserScript==
// @name        ii-distractalert
// @namespace   http://thedarkworld.net/projects/ii-distractnotify/
// @description Pops up a Desktop Notification when you have a new Distraction. Also plays the sound of a Chatot. Because Chatot are cute.
// @include     http://www.improbableisland.com/*
// @include     http://improbableisland.com/*
// @version     1
// ==/UserScript==
 
 
localStorage["iida-lastupdate"] = localStorage["iida-lastupdate"] || "0";
 
function runUpdate() {
    var ifr;
    ifr = document.createElement('iframe');
    ifr.style.display = "none";
    document.body.appendChild(ifr);
    ifr.onload = function() {
        var l;
        l = ifr.contentDocument.querySelectorAll(".trlight input[type=checkbox][name='markedmessages[]']").length;
        if( l > 0) {
            notifyUser(l);
        }
    };
    ifr.src = "/mail.php";
}
 
function notifyUser(cnt) {
    notifyUser.cntcache = notifyUser.cntcache || 0;
    if( !notifyUser.snd) {
        notifyUser.snd = document.createElement('audio');
        notifyUser.snd.src = "http://static.pokefarm.org/_mp3/cries/441.mp3";
    }
    if( notifyUser.cntcache < cnt) {
        if( notifyUser.notify) notifyUser.notify.close();
        notifyUser.notify = webkitNotifications.createNotification("","New distraction! ("+location.hostname+")","You have "+(cnt==1?"a Distraction":cnt+" Distractions")+"!");
        notifyUser.notify.onclick = function() {window.open('/mail.php');};
        notifyUser.notify.show();
        notifyUser.snd.play();
    }
    if( cnt == 0 && notifyUser.notify) notifyUser.notify.close();
    notifyUser.cntcache = cnt;
}
 
function startPolling() {
    setInterval(function() {
        var lastupdate, now;
        lastupdate = parseInt(localStorage["iida-lastupdate"],10);
        now = Date.now();
        if( lastupdate+10000 < now) { // 10 second intervals across all tabs - magic!
            localStorage["iida-lastupdate"] = now;
            runUpdate();
        }
    },1000);
}
 
function askForPermission() {
    var div, a;
    div = document.createElement('div');
    div.style.cssText = "position:fixed;left:8px;top:8px;background:black;color:white;border:1px solid white;padding:4px";
    div.appendChild(document.createElement('p')).appendChild(document.createTextNode("You need to grant permissions for notifications to work."));
    a = div.appendChild(document.createElement('p')).appendChild(document.createElement('a'));
    a.appendChild(document.createTextNode("Click here"));
    a.style.color = "#3cf";
    a.href = "#";
    a.onclick = function() {
        document.body.removeChild(div);
        webkitNotifications.requestPermission(startPolling);
        return false;
    };
    document.body.appendChild(div);
}
 
if( webkitNotifications.checkPermission()) {askForPermission();}
else {startPolling();}

Version 1 by Rentoraa (archive.org link)

If you're like me, you're always doing a fuckton at once. Sometimes, even ii-distractmore isn't enough to tell you that someone's talking to you, simply because you haven't loaded any II pages in a little while.

This script quietly auto-reloads a page in a hidden iframe, and if you have a new Distraction? Boom! A little notification appears on your Desktop telling you so.

WARNING: This script will make you appear online just by having a tab open, even if you aren't actually there loading any pages. This can be confusing for others, especially if you fall asleep in the middle of Story. Like that'd ever happen... *hides*

IMPORTANT: This script ONLY works in Chrome and other Webkit-based browsers. I will try to update for Firefox support in the near future.

TODO:

Add a time limit. Make it so that after 30 minutes of not loading a page, the script stops making you appear online. It's not perfect, since you will appear online for a further half-hour after that, but it'll help!
Make it work in Firefox.
 
Logged in as: Guest (Guest)
greasemonkey/ii-distract-notify.txt · Last modified: 2019/12/30 18:58 by Full Metal Lion