Created
September 6, 2016 12:17
-
-
Save frankkienl/a4130dbd77adf222c3f540546af460e0 to your computer and use it in GitHub Desktop.
Give notifications when A Dark Room's Gather button should be clicked again
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
// ==UserScript== | |
// @name A Dark Room notifier | |
// @namespace http://adarkroom.doublespeakgames.com/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match http://adarkroom.doublespeakgames.com/ | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// request notification permission on page load | |
document.addEventListener('DOMContentLoaded', function () { | |
if (!Notification) { | |
alert('Desktop notifications not available in your browser. Try Chromium.'); | |
return; | |
} | |
if (Notification.permission !== "granted") | |
Notification.requestPermission(); | |
}); | |
// give a little notice when the buttons can be clicked again | |
addClassNameListener("gatherButton", function(){ checkButton("gatherButton","G"); }); | |
addClassNameListener("trapsButton", function(){ checkButton("trapsButton", "T"); }); | |
console.log("A Dark Room - listener started"); | |
})(); | |
function checkButton(elemId, letter) { | |
console.log("checkButton("+elemId+"," + letter + ")"); | |
if (!stringContains(document.getElementById(elemId).className, "disabled")){ | |
notifyMe(letter); | |
} | |
} | |
function stringContains(haystack, needle){ | |
return haystack.indexOf(needle) !== -1; | |
} | |
//http://stackoverflow.com/questions/10612024/event-trigger-on-class-change | |
function addClassNameListener(elemId, callback) { | |
var elem = document.getElementById(elemId); | |
var lastClassName = elem.className; | |
window.setInterval( function() { | |
var className = elem.className; | |
if (className !== lastClassName) { | |
callback(); | |
lastClassName = className; | |
} | |
},100); | |
} | |
//http://stackoverflow.com/questions/2271156/chrome-desktop-notification-example | |
function notifyMe(letter) { | |
if (Notification.permission !== "granted") | |
Notification.requestPermission(); | |
else { | |
var notification = new Notification('A Dark Room', { | |
icon: 'https://raw.githubusercontent.com/doublespeakgames/adarkroom/master/favicon.ico', | |
body: "Hey there! You've been notified!" + letter, | |
}); | |
notification.onclick = function () { | |
//nothing.. | |
}; | |
} | |
} | |
////MEUK | |
function checkButtonOld(elemId, letter){ | |
console.log("checkButton("+elemId+"," + letter + ")"); | |
if (stringContains(document.getElementById(elemId).className, "disabled")){ | |
//button disabled? remove notify-letter | |
if (document.title.startsWith(letter)){ | |
document.title = document.title.substring(1); | |
} | |
} else { | |
//if notify-letter is already added, don't add again | |
if (!document.title.startsWith(letter)){ | |
document.title = letter + document.title; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment