patternjavascriptMinor
Mission impossible: beat everyone else to the queue
Viewed 0 times
theelsebeatimpossiblemissioneveryonequeue
Problem
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
If you are familiar with JavaScript, you are probably wondering why I didn't just open the link with
index.html
Nothing interesting here. Kind of pointless to post, but I'd probably get closed if I didn't.
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);
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. 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.
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);
Solution
I don't really see the point to create with PHP a file that then you have to read in javascript. Can't you just return the content of dummy.html when you make your Ajax request, which will give you a direct access to the data you want in javascript ?
Two other (minor) things :
Two other (minor) things :
- I would execute the analysis of the HTML on the PHP side, there are great functions to do so. Just because it will be executed server-side, so presumably faster (even if, in that case it's the same computer)
- setInterval is bad : what will happen if your Ajax call is slow to execute (like so slow that it'll take more than 10s) ? A setTimeOut with a recursive call to itself would be better I think (that way, you're sure that your functions did end before calling them back again).
Context
StackExchange Code Review Q#81793, answer score: 8
Revisions (0)
No revisions yet.