Skip to content

Instantly share code, notes, and snippets.

@yepninja
Last active May 2, 2018 18:30
Show Gist options
  • Save yepninja/2685989ba8e8fe75fd50b280f32892cc to your computer and use it in GitHub Desktop.
Save yepninja/2685989ba8e8fe75fd50b280f32892cc to your computer and use it in GitHub Desktop.
Get link to add event to calendar.google.com from afisha.yandex.ru
function getDetails() {
const link = document.location.href
return `Link: ${link}`
}
function getAddress() {
const city = document.querySelector('.city-button__text').innerText || 'Москва'
const {
content,
innerText,
} = document.querySelector('[itemprop="address"]')
const address = content || innerText
return city + ', ' + address
}
function getTimeZone() {
return JSON.parse(document.body.dataset.bem)["i-global"].city.timezone
}
function getGoogleDate(date) {
const offset = date.getTimezoneOffset()
date.setMinutes(date.getMinutes() + offset)
return date.toISOString().replace(/-|:|\.\d\d\d/g, '')
}
function getDate() {
const dateStr = document
.querySelector('[itemprop="startDate"]')
.content
const timeStr = (
document.querySelector('.event-heading2__cities')
||
document.querySelector('.event-concert-description__cities')
||
document.querySelector('.event-heading__cities')
)
.innerText
.replace(/.+, /, '')
const startDate = new Date(`${dateStr}T${timeStr}:00Z`)
const endDate = new Date(startDate)
endDate.setHours(endDate.getHours() + 3)
return getGoogleDate(startDate) + '/' + getGoogleDate(endDate)
}
function getTitle() {
return document.querySelector('[itemprop="name"]').innerText
}
function getEvent() {
return {
text: getTitle(),
dates: getDate(),
location: getAddress(),
details: getDetails(),
ctz: getTimeZone(),
output: 'xml'
}
}
function getEventLink() {
const data = getEvent()
const query = new URLSearchParams()
Object.keys(data).forEach(key => query.set(key, data[key]))
return 'https://calendar.google.com/calendar/r/eventedit?' + query.toString()
}
var eventLink = getEventLink()
console.log(eventLink)
copy(eventLink)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment