Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balta2ar/407b1cbcb669545c926bb4aa9d4aaeb6 to your computer and use it in GitHub Desktop.
Save balta2ar/407b1cbcb669545c926bb4aa9d4aaeb6 to your computer and use it in GitHub Desktop.
UserScript that helps you duplicate Google Calendar events quickly
// ==UserScript==
// @name google-calendar-automate-duplication
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Automate duplication of events
// @author Yuri Bochkarev
// @match https://calendar.google.com/calendar/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const DELAY = 500;
const STYLE_ACTIVE = "margin-left: 10px; font-size: 20px; font-weight: bold; color: red;";
const STYLE_INACTIVE = "margin-left: 10px; font-size: 20px; font-weight: normal; color: grey;";
//var currentWeek;
var currentDay;
//var toggle;
var isActive = false;
function createToggle() {
let header = document.querySelector('[aria-label="Calendar"]');
let d = document.createElement('a');
d.style = STYLE_INACTIVE;
d.onclick = function() {
isActive = !isActive;
if (isActive) { this.style = STYLE_ACTIVE; }
else { this.style = STYLE_INACTIVE; }
}
d.innerHTML = 'D'
header.appendChild(d);
return d
}
function getCurrentWeek() {
for (let div of document.querySelectorAll('div')) {
if (div.innerText.startsWith('Week ')) {
return div.innerText;
}
}
return null;
}
function getCurrentDay() {
let mini = document.querySelector('[data-month]')
if (mini) return mini.querySelector('[aria-selected=true]');
}
function app() {
createToggle();
var saveClicker = setInterval(function () {
if (!isActive) return;
var saveButton = document.querySelector('div[aria-label=Save]');
if (saveButton == null) return;
saveButton.click();
setTimeout(function() { currentDay.click(); }, DELAY);
//var scrollToCurrentWeek = setInterval(function() {
// if (currentWeek == getCurrentWeek()) { clearInterval(scrollToCurrentWeek); return; }
// var nextWeekButton = document.querySelector('div[aria-label="Next week"]');
// if (nextWeekButton) { nextWeekButton.click(); return; }
// clearInterval(scrollToCurrentWeek);
//}, 500);
}, DELAY);
var simulateClick = function(element) {
// Simulate mouse down
element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, view: window }));
// Simulate mouse release
element.dispatchEvent(new MouseEvent("mouseup", { bubbles: true, cancelable: true, view: window }))
}
var optionsClicker = setInterval(function () {
if (!isActive) return;
var optionsButton = document.querySelector('div[aria-label=Options]');
var duplicateButton = document.querySelector('span[aria-label=Duplicate]');
if (optionsButton != null && duplicateButton == null) {
optionsButton.click();
} else if (optionsButton != null && duplicateButton != null) {
//currentWeek = getCurrentWeek();
currentDay = getCurrentDay();
simulateClick(duplicateButton);
}
}, DELAY);
}
if (document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)
) {
app();
} else {
document.addEventListener("DOMContentLoaded", app);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment