Remove jQuery dependency

This commit is contained in:
Iacopo Spalletti 2016-06-05 22:49:54 +02:00
parent d9dd192ca8
commit 3c4eebdd02
No known key found for this signature in database
GPG key ID: BDCBC2EB289F60C6

View file

@ -1,27 +1,24 @@
$(function () { document.addEventListener("DOMContentLoaded", function() {
// Correctly decide between ws:// and wss:// // Correctly decide between ws:// and wss://
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws"; var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var ws_path = ws_scheme + '://' + window.location.host + "/liveblog/liveblog/" + liveblog_apphook + "/" + liveblog_language + "/" + liveblog_post + "/"; var ws_path = ws_scheme + '://' + window.location.host + "/liveblog/liveblog/" + liveblog_apphook + "/" + liveblog_language + "/" + liveblog_post + "/";
console.log("Connecting to " + ws_path);
var socket = new ReconnectingWebSocket(ws_path); var socket = new ReconnectingWebSocket(ws_path);
// Handle incoming messages // Handle incoming messages
socket.onmessage = function (message) { socket.onmessage = function (message) {
// Decode the JSON // Decode the JSON
console.log("Got message " + message.data);
var data = JSON.parse(message.data); var data = JSON.parse(message.data);
// See if there's a div to replace it in, or if we should add a new one // See if there's a div to replace it in, or if we should add a new one
var existing = $("div[data-post-id=" + data.id + "]"); var existing = document.querySelectorAll("div[data-post-id*='" + data.id + "']");
if (existing.length) { if (existing.length) {
existing.replaceWith(data.content); existing.parentNode.replaceChild(data.content, existing);
} else { } else {
$("#liveblog-posts").prepend(data.content); var item = document.createElement('div');
item.innerHTML = data.content;
document.getElementById("liveblog-posts").insertBefore(
item.children[0], document.getElementById("liveblog-posts").children[0]
);
} }
}; };
// Helpful debugging
socket.onopen = function () { }, false);
console.log("Connected to notification socket");
}
socket.onclose = function () {
console.log("Disconnected to notification socket");
}
});