Skip to content

Instantly share code, notes, and snippets.

@szmeku
Forked from deenseth/gist:1514633
Last active December 17, 2015 19:08
Show Gist options
  • Save szmeku/5657766 to your computer and use it in GitHub Desktop.
Save szmeku/5657766 to your computer and use it in GitHub Desktop.
javascript: (function () {
var event,
selection = (function () {
if (window.getSelection) {
return window.getSelection().toString();
} else if (document.getSelection) {
return document.getSelection();
} else {
return document.selection.createRange().text;
}
})();
var title = (selection ? getTitle(selection) : '');
function getTitle(selection) {
var titleMatches = selection.match(/[a-z ]+/gi);
if (titleMatches) {
for (var i = 0; i < titleMatches.length; i++) {
if (titleMatches[i].trim().length == 0) {
titleMatches.splice(i, 1);
i--;
}
}
return titleMatches.join(' - ');
}
}
function dateStringToDate(dateString) {
var dateArray = dateString.match(/[0-9]{1,4}/g);
var date = new Date();
date.setDate(dateArray[0]);
date.setMonth(dateArray[1] - 1);
if (dateArray[2]) {
date.setFullYear(dateArray[2].length == 4 ? dateArray[2] : '20' + dateArray[2]);
} else {
date.setYear(new Date().getFullYear());
}
date.setHours(5);
return date;
}
function getDateString(string) {
var matches = string.match(/([0-9]{1,2}[\-\.\/]){1,2}[0-9]{1,4}/);
return matches ? matches[0] : false;
}
function dateToDateGoogle(date) {
return date.toISOString().split("T")[0].replace(/\-/g, '');
}
try {
var selection = selection
.match(/(([0-9]{1,2}[\-\.\/]){1,2}[0-9]{1,4}){0,1}\-{0,1}(([0-9]{1,2}[\-\.\/]){1,2}[0-9]{1,4})/)[0].split('-');
var startDate,
startDateString = getDateString(selection[0]),
endDate = (selection[1] ? dateStringToDate(getDateString(selection[1]))
: dateStringToDate(getDateString(selection[0])));
startDate = (startDateString ? dateStringToDate(startDateString)
: new Date(endDate.getFullYear(), endDate.getMonth(), selection[0].match(/[0-9]{1,2}/)[0], 5, 5));
/* poprawka dziwoty googlowej */
endDate.setDate(endDate.getDate() + 1);
event = "text=" + title + "&dates=" + dateToDateGoogle(startDate) + "/" + dateToDateGoogle(endDate);
} catch (e) {
event = "ctext=" + title;
}
void(
/*open a new window with this information in the Google Calendar event creation page.*/
window.open(
encodeURI('http://www.google.com/calendar/event?'
+ event + '&action=TEMPLATE&pprop=HowCreated:QUICKADD'
), 'addwindow', 'status=no,toolbar=no,width=520,height=470,resizable=yes'));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment