Your mission, should you chose to accept it, is to beat everyone else to the review tab so you get a chance to help out.
This is a problem I've been running into lately. I go over to review tab, find a post awaiting review, select it, and then after my page loads, it's already been reviewed.
And since this site doesn't have new posts rolling in every second, a post isn't put into the queue for a while.
Or so it seems....
I don't have the time to be hunched over at my desk, constantly clicking "refresh" in hope that I'll catch a post coming in before someone else.
To aid this problem, I created a simple web-based application that does this for me.
As long as I have the webpage that I created open, I'll be sent notifications the second a post is put into the queue.
File tree
Everything is in one directory, as shown below:
review_listener
====> dummy.html
====> get_html.php
====> index.html
====> main.js
====> start_server.sh
Code
dummy.html
There is actually nothing in this file, to start. Read on to find the purpose of this file.
get_html.php
This, using the command line, calls the curl
command and uses the >
to change STDOUT to be dummy.html
. This way, the main.js
file can easily interact with the HTML.
<?php
exec("curl https://codereview.stackexchange.com/review > dummy.html");
?>
If you are familiar with JavaScript, you are probably wondering why I didn't just open the link with window.open
in the JavaScript file. I had already try that, but for some reason it wasn't working, and I had already made this before thinking of just doing it with JavaScript. (Yes, this code does work).
index.html
Nothing interesting here. Kind of pointless to post, but I'd probably get closed if I didn't.
<!DOCTYPE html>
<html>
<head>
<script src = "main.js"></script>
</head>
<body>
</body>
</html>
main.js
Here is where the fun happens!
var xhr;
function GETPHP() {
xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXOBject("Microsoft.XMLHTTP");
xhr.open("GET", "http://localhost:8000/get_html.php", true);
xhr.send();
}
function handleHTML() {
var newhtml = window.open("http://localhost:8000/dummy.html");
newhtml.onload = function () {
var nums = newhtml.document.getElementsByClassName("dashboard-num");
for (var i = 0; i < nums.length; i++) {
if (parseInt(nums[i].innerHTML, 10) > 0) {
sendNotification();
newhtml.close();
break;
}
console.log("Number " + i + " is " + nums[i].innerHTML);
}
newhtml.close();
}
}
function sendNotification() {
//http://stackoverflow.com/questions/2271156/chrome-desktop-notification-example
if (Notification.permission !== "granted") {
Notification.requestPermission();
}
var notif = new Notification("CODE RED: POST IN QUEUE!!!", {
body: "You must be quick before it gets Jamalized! Or worse!"
});
notif.onclick = function () {
window.open("https://codereview.stackexchange.com/review");
}
}
window.setInterval(function () {
GETPHP();
handleHTML();
}, 10000); // ten seconds
The above code does the following:
Tells the PHP file to load the HTML into the
dummy.html
fileOpens up the
dummy.html
fileReads the queue quantity values
If there is a post in the queue, send a notification and go to 5
Else, wait about 10 seconds and go back to 1
start_server.sh
This isn't that really interesting, either.
#!/bin/sh
php -S localhost:8000
Concerns
In the
main.js
file, I am constantly opening and closing a tab. This was the easiest way I think of doing it, but easy isn't always efficient; is there a more efficient way to do this?Is my code portable? Mainly, do most people use a version of PHP that has the built in server, and do most browsers support the
Notification
object?In order from greatest to least, below is my knowledge of the languages used:
JavaScript
HTML
PHP
Shell
That being said, are there some practices I should be following with these languages that I missed?
Any other recommendations of any sort are also welcome.
Disclaimer:
If you do happen to try and run this code (and you are successful), you may notice that you will either...
Get a notification when there are no posts in the queue
Not get a notification when there is a post in the queue
This is not a bug. When the code connects to the Code Review server, it is only given a preview of the review section. This preview is less frequently updated than if you were to manually log on to Code Review; nothing I can control
The code works as I would like it.