Get link to add event to calendar.google.com from afisha.yandex.ru

Yevgeny Petukhov
2018-05-02T13:36:34Z

afishyaru2googlecalendar.js

| 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)