MediaWiki:Common.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');
function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
if (t.total <= 0) {
clearInterval(timeinterval);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
/* only run this on the page that there is a clock div... */
// Or just a plain access for comparison
// (no need to check exists first, it falls back to null)
$(function () {
if (mw.config.get('wgPageName') === 'Template:Countdown' || mw.config.get('wgPageName') === 'Welcome' ) {
var deadline = new Date(2025, 07, 8, 13, 37, 42, 0); /* base 0 months */
initializeClock('clockdiv', deadline);
}
});
var mqttinitstarted = false;
var mqttClient = null;
function initMqtt() {
if(typeof mqtt == "undefined" && !mqttinitstarted) {
mqttinitstarted = true;
(function(d, script) {
script = d.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.onload = initMqttStep2;
script.src = 'https://unpkg.com/mqtt@5.10.3/dist/mqtt.min.js';
d.getElementsByTagName('head')[0].appendChild(script);
}(document));
}
}
function initMqttStep2(callback) {
const url = 'wss://mqtt.why2025.org/'
const options = {
// Clean session
clean: true,
connectTimeout: 4000,
// Authentication
clientId: 'whywiki_' + Math.floor(Math.random() * 9999999),
}
mqttClient = mqtt.connect(url, options);
mqttClient.on('connect', function () {
var feedelements = document.getElementsByClassName("mqttfeed");
for(x = 0; x < feedelements.length; x++) {
var elementtopic = feedelements[x].attributes['data-topic'].value;
mqttClient.subscribe(elementtopic);
console.log("MQTT sub on:" + elementtopic);
if(feedelements[x].attributes['data-topic2'] && feedelements[x].attributes['data-topic2'].value) {
var elementtopic2 = feedelements[x].attributes['data-topic2'].value;
mqttClient.subscribe(elementtopic2);
console.log("MQTT sub on:" + elementtopic2);
}
}
// Receive messages
mqttClient.on('message', function (topic, message) {
// message is Buffer
console.log("MQTT msg:"+message.toString())
var feedelements = document.getElementsByClassName("mqttfeed");
for(x = 0; x < feedelements.length; x++) {
var elementtopic = feedelements[x].attributes['data-topic'].value;
var elementtopic2 = feedelements[x].attributes['data-topic2'] ? feedelements[x].attributes['data-topic2'].value : "null";
var elementOperation = feedelements[x].attributes['data-operation'] ? feedelements[x].attributes['data-operation'].value : "none";
if(elementtopic == topic) {
feedelements[x].attributes['data-value'] = message.toString();
}
if(elementtopic2 == topic) {
feedelements[x].attributes['data-value2'] = message.toString();
}
if(elementtopic == topic || elementtopic2 == topic) {
var finalValue = "";
switch(elementOperation) {
case "sum":
finalValue = feedelements[x].attributes['data-value']*1 + feedelements[x].attributes['data-value2']*1;
break;
case "sub":
finalValue = feedelements[x].attributes['data-value']*1 - feedelements[x].attributes['data-value2']*1;
break;
case "mul":
finalValue = feedelements[x].attributes['data-value']*1 * feedelements[x].attributes['data-value2']*1;
break;
case "div":
finalValue = feedelements[x].attributes['data-value']*1 / feedelements[x].attributes['data-value2']*1;
break;
default:
finalValue = feedelements[x].attributes['data-value'];
}
feedelements[x].innerText = finalValue;
}
}
});
});
}
// Only make a MQTT connection for pages that use it.
if (document.getElementsByClassName("mqttfeed").length > 0) {
initMqtt();
}