Last active
August 20, 2019 03:04
-
-
Save shotasenga/a5cfb358cc25c9da418519857af3e4a6 to your computer and use it in GitHub Desktop.
Eh+ add to calendar #GMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Eh+ add link to add to calendar | |
// @namespace http://senta.me/ | |
// @version 0.1.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://eh-plus.com/book-online* | |
// @grant none | |
// ==/UserScript== | |
;(() => { | |
const MONTH_MAP = Object.freeze({ | |
Jan: 0, | |
Feb: 1, | |
Mar: 2, | |
Apr: 3, | |
May: 4, | |
Jun: 5, | |
Jul: 6, | |
Aug: 7, | |
Sep: 8, | |
Oct: 9, | |
Nov: 10, | |
Dec: 11 | |
}) | |
// @ts-ignore | |
const $ = jQuery | |
// ensure they use jQuery | |
const CALENDAR_QUERY = ".fa.fa-fw.fa-calendar" | |
if ($(CALENDAR_QUERY).length < 1) { | |
return | |
} | |
$(CALENDAR_QUERY).each(function() { | |
const [parent] = $(this).parents(".panel-heading") | |
let s = $(parent).text() | |
s = s.replace(/^\s*/g, "") | |
s = s.replace(/\r?\n/g, " ") | |
// Thu, 29 Aug 2019 4:30pm Approved | |
// Tue, 20 Aug 2019 3:15pm Vocabulary Class | |
const m = s.match( | |
/(\w{3}), (\d{1,2}) (\w{3}) (\d{4})\s+(\d{1,2}):(\d{1,2})(pm|am)/ | |
) | |
if (!m) { | |
return | |
} | |
const [_, __, day, month, year, hour, minute, period] = m | |
const horuPrefix = period == "pm" ? 12 : 0 | |
const date = new Date( | |
year, | |
MONTH_MAP[month], | |
day, | |
parseInt(hour, 10) + horuPrefix, | |
minute | |
) | |
const link = createLink(date) | |
$(parent).append(link) | |
}) | |
function createLink(date) { | |
const endDate = new Date(date) | |
endDate.setHours(endDate.getHours() + 1) | |
const start = formatDate(date) | |
const end = formatDate(endDate) | |
return ( | |
'<a href="http://www.google.com/calendar/render?\ | |
action=TEMPLATE\ | |
&text=Eh%2B\ | |
&dates=' + | |
start + | |
"/" + | |
end + | |
"\ | |
&ctz=Canda/Vancouver\ | |
&location=568+Seymour+St+%233%2C+Vancouver%2C+BC+V6B+3J5" + | |
'\ | |
&trp=false\ | |
&sprop=\ | |
&sprop=name:"\ | |
target="_blank" rel="nofollow">Add to my calendar</a>' | |
) | |
} | |
function formatDate(date) { | |
const f = | |
date.getFullYear().toString() + | |
zeropad(date.getMonth() + 1) + | |
zeropad(date.getDate()) + | |
"T" + | |
zeropad(date.getHours()) + | |
zeropad(date.getMinutes()) + | |
zeropad(date.getSeconds()) | |
return f | |
} | |
function zeropad(n, length = 2) { | |
const len = n.toString().length | |
if (len >= length) { | |
return n.toString() | |
} | |
return "0".repeat(length - len) + n.toString() | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment