YouTube Live Event to Calendar

YouTube Live Event to Calendar...but semi-automatic

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        YouTube Live Event to Calendar
// @namespace   https://defaultcf.github.io
// @match       https://www.youtube.com/watch*
// @grant       GM_registerMenuCommand
// @grant       GM_openInTab
// @version     0.1.1
// @author      defaultcf
// @description YouTube Live Event to Calendar...but semi-automatic
// ==/UserScript==

'use strict';

(() => {
  const _debug = (...obj) => console.log('[YT Live Event to Calendar]', ...obj);

  class Utility {
    static get_body(url) {
      return new Promise(async resolve => {
        const res = await fetch(url, { cache: 'no-cache' });
        const raw_body = await res.text();
        const domparser = new DOMParser();
        const body = domparser.parseFromString(raw_body, 'text/html');
        resolve(body);
      });
    }

    static async get_metas(body) {
      const metas = {};
      await body.querySelectorAll('meta').forEach(meta => {
        const key = meta.getAttribute('name') || meta.getAttribute('itemprop');
        metas[key] = meta.content;
      });
      return metas;
    }

    static get_date_string(date) {
      return date.toISOString().replace(/[-:]/g, '').replace(/\.\d{3}/, '');
    }

    static create_event(text, date, details, location) {
      date = new Date(date);
      const start_datetime = this.get_date_string(date);
      date.setHours(date.getHours() + 1);
      const end_datetime = this.get_date_string(date);
      const dates = `${start_datetime}/${end_datetime}`;
      const url = new URL('https://www.google.com/calendar/render');
      url.searchParams.append('action', 'TEMPLATE');
      url.searchParams.append('text', text);
      url.searchParams.append('dates', dates);
      url.searchParams.append('details', details);
      url.searchParams.append('location', location);
      GM_openInTab(url.href);
    }
  }

  const menu_event = async () => {
    const body = await Utility.get_body(document.location);
    const metas = await Utility.get_metas(body);
    Utility.create_event(
        metas['title'],
        metas['startDate'],
        metas['description'],
        `https://www.youtube.com/watch?v=${metas['videoId']}`,
    );
  }

  GM_registerMenuCommand('Add Google Calendar', menu_event);
})();