Skip to content

Instantly share code, notes, and snippets.

@rschrieken
Last active April 21, 2016 21:11
Show Gist options
  • Save rschrieken/5fa98d53c4e1bd0fcfcb0c00c0eac3ad to your computer and use it in GitHub Desktop.
Save rschrieken/5fa98d53c4e1bd0fcfcb0c00c0eac3ad to your computer and use it in GitHub Desktop.
schedule events in a chatroom
// ==UserScript==
// @name add multiple events
// @namespace http://stackoverflow.com/users/578411/rene
// @version 0.1
// @description Event creator
// @author rene
// @match http://chat.stackoverflow.com/rooms/info/*
// @grant none
// ==/UserScript==
/* jshint -W097 */
/*global confirm:true, console:true, fkey:true, $:true */
'use strict';
var emulate = true,
dur = 60, // minutes
room,
schedules = [];
Date.prototype.addDays = function(days)
{
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
};
function numasstr(n) {
var num = '0' + n.toString();
return num.substring(num.length - 2);
}
function formatDate(next) {
return next.getUTCFullYear() + '/' + numasstr(next.getUTCMonth()+1)+'/' + numasstr(next.getUTCDate());
}
function formatTime(next) {
return numasstr(next.getUTCHours()) + ':' + numasstr(next.getUTCMinutes());
}
function makeitUTC(d) {
var uyr = d.getUTCFullYear(),
umo = d.getUTCMonth(),
uda = d.getUTCDate(),
uhr = d.getUTCHours(),
umi = d.getUTCMinutes();
console.log(new Date(Date.UTC(uyr,umo,uda,uhr,umi,0)));
}
function findNextDate(d, desc) {
var next = new Date(d.getFullYear(), d.getMonth(), d.getDate()).addDays(6),
endMonth = next.getMonth() + 1;
while(next.getMonth() < endMonth) {
for(;next.getDay() === 0 || next.getDay() === 6;next=next.addDays(-1));
console.log(next);
next.setHours(d.getHours());
next.setMinutes(d.getMinutes());
schedules.push({ datetime:next, desc: desc});
next = next.addDays(6);
}
}
function buildDOM() {
$('li.meeting').each(function() {
var _this = $(this),
md = $(_this[0]).find('div.meeting-details span[data-time]'),
gen = $('<div></div>'),
emu = $('<input type="checkbox" />'),
emucnt = $('<label>emulate</label>'),
a = $('<a></a>'),
dt = new Date(parseInt(md.attr('data-time'), 10) * 1000), // date and time
desc = _this.find('h3')[0].childNodes[0].nodeValue; // description
console.log(desc);
gen.append(emucnt);
emucnt.append(emu);
emu.prop('checked', emulate);
emu.on('change', function() { var emuval = $(this); emulate = emuval.prop('checked'); });
gen.append(a);
a.text('Generate next events');
a.prop('href', '#');
a.on('click', function(event) { event.preventDefault(); findNextDate(dt, desc); a.hide(); processSchedule(); });
_this.append(gen);
});
}
function processSchedule() {
var int_hnd = window.setInterval(function () {
var sched = schedules.shift();
if (sched !== undefined) {
if (emulate) {
console.log({
url: '/rooms/schedule/add/'+ room.toString(),
fkey: fkey().fkey,
description: sched.desc,
firstDate: formatDate(sched.datetime),
firstTime: formatTime(sched.datetime),
duration: dur,
recur: '',
expiry: ''
});
} else {
$.post('/rooms/schedule/add/'+ room.toString(), {
fkey: fkey().fkey,
description: sched.desc,
firstDate: formatDate(sched.datetime),
firstTime: formatTime(sched.datetime),
duration: dur,
recur: '',
expiry: ''
}, function() {console.log('OK: ' + sched);});
}
} else {
window.clearInterval(int_hnd);
console.log('all done!');
}
}, 5000);
console.log('processing...');
}
function init() {
var re = /(?:\w|[.\/:])*\/rooms\/info\/(\d+)\/((\w|[-])+)\?tab=(\w+)/,
matches = re.exec(window.location.href); // test room
console.log(matches);
if (matches.length>0) {
room = parseInt(matches[1],10);
console.log(room);
buildDOM();
} else {
window.alert('you\'re not on a schedule');
}
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment