Google Calendar Go To Date

By Tilman Vogel Last update Jun 25, 2008 — Installed 711 times.
// ==UserScript==
// @name           Google Calendar Go To Date
// @namespace      tag:tilman.vogel@web.de,2007:userscripts
// @description    Allows to jump to a specific date by clicking on the date field
// @include        http://www.google.com/calendar/render*
// @include        https://www.google.com/calendar/render*
// ==/UserScript==

function gotoDate() {
  var val = this.yyyymmdd.value;
  
  if(val.length <= 4)
    // only four digits: need to add month
    val = val+"01";
  if(val.length <= 6)
    // only six digits: need to add day
    val = val+"01";

  // pre 20080215 location.href="javascript:void(z.O(oa('"+val+"')))";
  //
  //location.href="javascript:void(X.Da(Sl('"+val+"')))";
  location.href = "javascript:void(tvGOTO('"+val+"'))";
  
  return false;
}


function addDateInput() {
  if(this.getElementsByTagName('FORM').length > 0)
    return;

  // pre 20080215 var current = window.getGlobalValue('Wa');
  // var current = window.getGlobalValue('Yv');
  var current = window.getGlobalValue(currentdatevariable);
  olddate = this.innerHTML;

  this.innerHTML = "\
<form style='margin:0px; display:inline;' action='' method='get'>\
<input title='specify date as day (20071231), month (200712) or year (2007)' \
 id='userscript-yyyymmdd' name='yyyymmdd' value='"+current+"' size=8 maxlength=8> \
</form>\
";
  this.firstChild.addEventListener('submit',gotoDate,false);

  var field = document.getElementById('userscript-yyyymmdd');
  field.addEventListener('blur', removeDateInput, false);
  field.select();
  field.focus();
}

function removeDateInput() {
  datefield.innerHTML = olddate;
}


// init()

// function to get values of global variables using the "location hack"
window.GM_getGlobalElement = null;
window.getGlobalValue = function(name) {
  if (GM_getGlobalElement == null) {
    GM_getGlobalElement = document.createElement("textarea");
    GM_getGlobalElement.id = "GM_getGlobalElement";
    GM_getGlobalElement.style.visibility = "hidden";
    GM_getGlobalElement.style.display = "none";
    document.body.appendChild(GM_getGlobalElement);
  }
  location.href = "javascript:void(typeof("+ name + ")!=\"undefined\"?document.getElementById(\"GM_getGlobalElement\").value=" + name + ":null)";
  return(GM_getGlobalElement.value);
}

// main()

var datefield = document.getElementById('dateunderlay');
var olddate;
var currentdatevariable;
var definedGOTO;

if(!datefield) {
  GM_log("didn't find datefield - fatal");
  return;
}

// try to "understand" google calendar
// this will probably fail in some future version of google calendar
for(var f in unsafeWindow )
  if( typeof unsafeWindow[f] === 'function' ) {
    var fSrc = unsafeWindow[f].toString();

    // try to find "current date" variable
    if(fSrc.match('"day"')) {
      var match = fSrc.match(/(\w+) = \w+ = \w+/);
      if(match) {
	currentdatevariable = match[1];
	GM_log("currentdatevariable: "+currentdatevariable);
      }
    }

    // try to find "go to month function" and transform it to generic "go to" function
    if(fSrc.match('"month"') && ! fSrc.match('"week"')) {
      // rename it
      fSrc = fSrc.replace(/^function [^\(]*/,'function tvGOTO');
      // remove mode switch
      fSrc = fSrc.replace(/.*"month".*/,'');
      GM_log(fSrc);
      // define it using the location hack
      fSrc = "javascript:"+encodeURI(fSrc);
      //GM_log(fSrc);
      location.href = fSrc;
      definedGOTO = true;
    }
  }

if(!currentdatevariable) {
  GM_log("didn't find current date variable - fatal");
  return;
}

if(!definedGOTO) {
  GM_log("couldn't define GOTO function - fatal");
  return;
}

datefield.style.cursor='pointer';
datefield.addEventListener('click', addDateInput, false );