// ==UserScript== // @name TV Recording Interface // @namespace calliope // @include http://tvlistings.guardian.co.uk/* // ==/UserScript== // Parse the date from the header var heading = document.getElementById("_ctl0_h1_heading"); var date_text = heading.childNodes[1].childNodes[0].nodeValue; if(date_text == "Today") { var date = new Date(); } else if(date_text == "Tomorrow") { var date = new Date(); date.setDate(date.getDate() + 1); } else { var date_array = date_text.split(" "); date_text = date_array[1].match(/\d+/)[0]; date_text += " "; date_text += date_array[2]; var date = new Date(); date_text += " " + date.getFullYear(); date = new Date(date_text); } date.setHours(0, 0, 0, 0); // Parse the channel name from the header var channel = document.getElementsByClassName("tvg_channelname")[0].childNodes[0].nodeValue; if(channel.match(/^BBC 1\s/)) channel = "BBC 1"; var channelid = -1; var num_replace = [ "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" ]; // Retrieve the list of channels var xhr = new XMLHttpRequest(); xhr.open("GET", "http://ruthven:1731/api/channels/all"); xhr.onreadystatechange = function() { if(xhr.readyState == 4) { if(xhr.status >= 200 && xhr.status < 300) { var chans = JSON.parse(xhr.responseText); var mchan = channel.toLowerCase().replace(/\s/g, ""); for(var i=0; i 0) { var sermatch = /^Series\s+(\d+).*Episode\s+(\d+)(\/\d+)?:?(.*)/; var epmatch = /^Episode\s+(\d+)(\/\d+)?:\s(.*)/; var text = details[0].childNodes[0].nodeValue; var res = text.match(sermatch); if(res != null && res.length > 0) { series = parseInt(res[1]); episode = parseInt(res[2]); subtitle = res[4].trim(); } else { res = text.match(epmatch); if(res != null && res.length > 0) { episode = parseInt(res[1]); subtitle = res[3].trim(); } else { subtitle = text.trim(); } } } // Add recording buffer start_time.setTime(start_time.getTime() - 5*60*1000); end_time.setTime(end_time.getTime() + 10*60*1000); var rec = document.createElement("span"); // The outer anonymous function here creates a closure that binds // the *current values* of t, s and e. If we didn't do this, then // we would bind the variables title, start_time, and end_time by // reference instead of by value, and every (rec) element would do // the same thing. (function () { var t = title; var s = start_time; var e = end_time; var st = subtitle; var ser = series; var ep = episode; rec.addEventListener("click", function(ev) { //alert("Recording " + t + "\nFrom " + s + "\nTo " + e); var rec = { "start": Math.floor(s.getTime()/1000), "end": Math.floor(e.getTime()/1000), "title": t, "chanid": channelid, "channel_label": channel, }; if(st != null && st != "") rec.subtitle = st; if(ser != 0) rec.series = ser; if(ep != 0) rec.episode = ep; var xhr = new XMLHttpRequest(); xhr.open("POST", "http://ruthven:1731/api/recordings"); xhr.onreadystatechange = function() { ajax_handler(xhr); }; xhr.setRequestHeader("Content-type", "application/json"); xhr.send(JSON.stringify(rec)); }); })(); rec.innerHTML = "(rec) "; x.childNodes[0].insertBefore(rec, x.childNodes[0].childNodes[0]); } } function ajax_handler(req) { if(req.readyState == 4) { if(req.status >= 200 && req.status < 300) { // Success } else { // Failure } } }