Skip to content

Instantly share code, notes, and snippets.

@eslam-mahmoud
Last active January 22, 2019 16:28
Show Gist options
  • Save eslam-mahmoud/003e237a482766986028840c17f5566b to your computer and use it in GitHub Desktop.
Save eslam-mahmoud/003e237a482766986028840c17f5566b to your computer and use it in GitHub Desktop.
console.chat
var domain = window.location.hostname;
console.log('Welcome to console.chat! You\'re chatting with other people who visited ' + domain + ' and opened their browser\'s console!');
console.log('To send a message it must be in this format: send(\'your message here\')\nExample: If you want to say "Hey, what\'s up?" send(\'Hey, what\'s up?\')');
console.log('Created by @DaltonEdwards :)');
var messageCount = 0;
function loadMessages() {
var request = new XMLHttpRequest();
request.open('GET', 'https://console.chat/api/read.php?domain=' + domain, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var json = JSON.parse(request.responseText);
for (i in json.messages) {
if (messageCount === 0 || i > messageCount) {
console.log(json.messages[i].message);
messageCount++;
}
}
} else {
console.error('Target server returned an error');
}
};
request.onerror = function() {
console.error('Connection problem');
};
request.send();
}
function send(message) {
var request = new XMLHttpRequest();
request.open('GET', 'https://console.chat/api/send.php?domain=' + domain + '&message=' + message, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var json = JSON.parse(request.responseText);
if (json.error !== false) {
console.error(json.error);
} else {
return true;
}
} else {
// We reached our target server, but it returned an error
return false;
}
};
request.onerror = function() {
// There was a connection error of some sort
return false;
};
request.send();
}
loadMessages();
// load in new messages once every 5 seconds
setInterval(loadMessages, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment