Skip to content

Instantly share code, notes, and snippets.

@cincodenada
Created May 23, 2012 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cincodenada/2777335 to your computer and use it in GitHub Desktop.
Save cincodenada/2777335 to your computer and use it in GitHub Desktop.
Modified script for the "Add Facebook Events to Google Calendar" Chrome extension
postpone();
function postpone()
{
init();
window.setTimeout(postpone,500);
}
function init()
{
if($('#gcalLink')[0]) return;
if(!$('.fbEventHeadline')[0]) return;
$('.fbEventHeadline + div').append(" · <span class='fsm'><a href='#' id='gcalLink' title='Add to Google Calendar'>Add to Calendar</a></span>");
$('#gcalLink').click(linkClick);
}
function getLang()
{
var m = $('body').attr('class').match(/_(\w\w)_/);
return m[1];
}
function getTitle()
{
return $('.fbEventHeadline').text();
}
function getLocation()
{
// map overlay -> take first two lines
var $location = $('#contentArea .uiImageBlockContent .uiInlineBlock');
if($location[0])
{
var locationText = '';
var div1 = $location.find('div div')[0];
if(div1) locationText = div1.innerText;
//alert(locationText);
var div2 = $location.find('div div')[1];
if(div2 && div2.innerText) locationText += ', ' + div2.innerText;
//alert(locationText);
if(locationText != '')
return locationText;
}
// text only version
var whereEl = findElement("//tr[td/div/i[contains(@title,'Where')]]/td[2]");
if(whereEl)
return whereEl.innerText;
return '';
}
function getDescription()
{
var descriptionText = '';
var descriptionEl = findElement("//tr[td/div[contains(@class,'fbDescriptionIcon')]]/td[2]");
if(descriptionEl)
descriptionText = descriptionEl.innerText;
// shorten
while(encodeURIComponent(descriptionText).length > 1400) { descriptionText = descriptionText.substring(0, descriptionText.length - 100); }
descriptionText += '\n\n' + location.toString();
descriptionText = descriptionText.replace(/^\n+/,'');
return descriptionText;
}
function getDates()
{
var dateFrom, dateTo;
if($('.fbInfoDateTime span').length == 2)
{
dateFrom = $('.fbInfoDateTime span')[0].innerText;
dateTo = $('.fbInfoDateTime span')[1].innerText;
}
else
{
//This new code handles three cases:
//Start/end times on the same day
//Start/end times on the different day
//Just a start time
whenEl = $('div i[title="When"]').parent();
timeEl = $('div i[title="Time"]').parent();
if(timeEl.length)
{
//This is a separate time/day event
fromto = timeEl.siblings('div').find('span');
if(fromto.length)
{
//There's a from/to
dateFrom = dayText + ' ' + fromto.eq(0).text();
dateTo = dayText + ' ' + fromto.eq(1).text();
}
else
{
//It's just a time
dateFrom = dayText + ' ' + timeEl.siblings().eq(0).text();
dateTo = dateFrom;
}
}
else if(whenEl.length)
{
//It's a combo time/day event
timeDiv = whenEl.siblings('div')
if(timeDiv.length) {
fromto = timeDiv.find('span');
if(fromto.length) {
dateFrom = fromto.eq(0).text();
dateTo = fromto.eq(1).text();
}
} else {
//This is a weird case I came across...not sure how it got there
combotext = whenEl.siblings('span').text();
combovals = combotext.split(' - ');
dateFrom = combovals[0];
dateTo = combovals[1];
}
}
else
{
//If my new code can't handle it, default to the original code
var dayEl = findElement("//div[div[contains(@class,'fbInfoIcon')]][1]/div[2]"); // When
if(dayEl)
{
var dayText = dayEl.innerText;
timeEl = $($(dayEl).parents('ul').find('li')[1]).find('div div.fsm')[0];
dateFrom = dayText + ' ' + $(timeEl).find('span')[0].innerText;
dateTo = dayText + ' ' + $(timeEl).find('span')[1].innerText;
}
else
{
var dayEl = findElement("//div[div/i[contains(@class,'sx_a94bdb')]]/span[1]"); //When
if(dayEl)
{
dateFrom = dayEl.innerText.split('until')[0];
dateTo = dayEl.innerText.split('until')[1];
}
}
}
}
return { from: dateFrom, to:dateTo };
}
function linkClick()
{
var dates = getDates();
/*
alert('TITLE: ' + getTitle()
+ '\n\nFROM: ' + dates.from
+ '\n\nTO: ' + dates.to
+ '\n\nDESCRIPTION: ' + getDescription()
+ '\n\nLOCATION: ' + getLocation());
*/
var href = 'http://fb.praetoris.cz/'
+ '?title=' + encodeURIComponent(getTitle())
+ '&from='+ encodeURIComponent(dates.from)
+ '&to='+ encodeURIComponent(dates.to)
+ '&location=' + encodeURIComponent(getLocation())
+ '&description=' + encodeURIComponent(getDescription())
+ '&offset=' + new Date().getTimezoneOffset()
+ '&lang=' + getLang();
window.open(href);
return false;
}
function findElement(xpath)
{
return document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null ).iterateNext();
}
function findElements(xpath)
{
return document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment