-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
249 lines (224 loc) · 8.81 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright Steve O'Hara
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Where we will expose all the data we retrieve from storage.sync.
const storageCache = {
optionalEvents: "showOptionalEvents",
hideMornings: false,
hideMorningsTime: "08:00",
tabs: [],
totps: []
};
// Asynchronously retrieve data from storage.sync, then cache it.
const initStorageCache = chrome.storage.sync.get().then((items) => {
Object.keys(storageCache).forEach((key) => storageCache[key] = items[key] ? items[key] : storageCache[key]);
chrome.storage.sync.set(storageCache);
});
// A generic onclick callback function
chrome.contextMenus.onClicked.addListener(genericOnClick);
/**
* The menu click callback function
*
* @param info Details of the menu item clicked
*/
function genericOnClick(info) {
storageCache.optionalEvents = info.menuItemId;
chrome.storage.sync.set(storageCache);
}
// On installation, create the context menus
chrome.runtime.onInstalled.addListener(function () {
// Create the menu item list for just the 'page' context
// Others available are; 'selection', 'link', 'editable', 'image', 'video', 'audio'
chrome.contextMenus.create({
title: 'Show optional events',
type: 'radio',
contexts: ['page'],
checked: storageCache.optionalEvents === "hideOptionalEvents",
id: 'showOptionalEvents'
});
chrome.contextMenus.create({
title: 'Hide optional events',
type: 'radio',
contexts: ['page'],
checked: storageCache.optionalEvents === "hideOptionalEvents",
id: 'hideOptionalEvents'
});
chrome.contextMenus.create({
title: 'De-Emphasise optional events',
type: 'radio',
contexts: ['page'],
checked: storageCache.optionalEvents === "deEmphasiseOptionalEvents",
id: 'deEmphasiseOptionalEvents'
});
});
// Catch any changes to the storage whether they come from the context menu or the popup
chrome.storage.onChanged.addListener((changes, namespace) => {
let refreshRequired = true;
for (let [key, {oldValue, newValue}] of Object.entries(changes)) {
if (key === "optionalEvents") {
chrome.contextMenus.update(newValue, {checked: true});
}
storageCache[key] = newValue;
if (key === "tabs") {
refreshRequired = false;
}
}
// Reload any calendar tabs if this is a change that affects them
if (refreshRequired) {
chrome.tabs.query({url: "https://calendar.google.com/calendar/u/0/r*"}, function (tabs) {
tabs.forEach(function (tab) {
chrome.tabs.reload(tab.id);
})
});
}
});
// Catch the loading of pages from scratch
chrome.webNavigation.onCompleted.addListener((details) => {
checkAction(details);
checkLogin(details);
});
// Catch the page updates through the SPA
chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
checkAction(details);
});
// Catch when we have received data for the 'Find a Time' display
chrome.webRequest.onCompleted.addListener((details) => {
checkAction(details);
},
{urls: ["https://calendar.google.com/calendar/u/0/clienthealth"]}
);
/**
* This function is injected into the Calendar document space and carries
* out the manipulation of the DOM based on the action
*
* @param action Type of action to carry out
* @param hideMornings True if we should hide the morning part of the calendar
* @param hideMorningsTime The time we should hide till
*/
function injectOptionalEvents(action, hideMornings, hideMorningsTime) {
// Check if we are changing the optional events
if (action !== "showOptionalEvents") {
let hide = action === "hideOptionalEvents"
document.querySelectorAll("div.GTG3wb.Epw9Dc").forEach(function (element, index) {
if (hide) {
element.style.display = "none";
}
else {
element.style.opacity = 0.2;
}
});
}
// Are we trying to hide the morning
if (hideMornings && hideMorningsTime !== "") {
let hour = hideMorningsTime.replace(/[:].+/g, "") * 1;
let time = hour > 12 ? ((hour - 12) + " PM") : (hour + " AM")
document.querySelectorAll("span.wO6pL.iHNmdb").forEach(function (element, index) {
if (element.innerText === time) {
let offset = element.parentElement.offsetTop + element.offsetTop;
setTimeout(function () {
document.querySelector("div.uEzZIb").style.marginTop = -offset + "px";
}, 500);
document.querySelector("div.uEzZIb").style.marginTop = -offset + "px";
if (new Date().getHours() <= hour) {
document.getElementsByClassName("mDPmMe")[0].scrollTop = "0px";
}
}
});
}
// Calendar builds in phases, so we can't be sure that it's all
// painted at this stage so we will check for the presence of parts of the DOM
// as an indicator that it's ready and keep trying until it is
if (!document.alreadyRun || document.alreadyRun < 5) {
setTimeout(function () {
injectOptionalEvents(action)
document.alreadyRun = document.alreadyRun ? document.alreadyRun + 1 : 1;
}, 500);
}
document.alreadyRun = document.querySelector("span.wO6pL.iHNmdb") ? 5 : document.alreadyRun;
}
/**
* This function is injected into the current document space and carries
* out the auto-login on the Okta Vonage passcode form using the key provided
*
* @param key The key to use
*/
function loginToVonage(key) {
document.vonageLoginTimer = setInterval(function() {
let identifier = document.querySelector('input[name="identifier"]');
if (!identifier) {
let input = document.querySelector('input[name="credentials.passcode"]');
if (input) {
let button = document.querySelector('input.button-primary[type="submit"]');
if (button) {
input.value = key;
input.focus();
clearInterval(document.vonageLoginTimer);
}
}
}
}, 1000);
}
/**
* Called whenever a page is loaded in the browser or it changes
* It checks to see if it's a Google Calendar and if the option is enabled
* and if so, will inject the code that actually does the hiding of screen elements
* @param details The event details containing the URL and tab ID
*/
function checkAction(details) {
if (/https:\/\/calendar.google.com\/calendar\/u\/0\/r.*/.test(details.url)) {
// Check if we need to change the visibility of the optional events
// and/or do something else with the display
if (storageCache.optionalEvents !== "showOptionalEvents" || storageCache.hideMornings) {
chrome.scripting.executeScript({
target: {tabId: details.tabId},
func: injectOptionalEvents,
args: [
storageCache.optionalEvents,
storageCache.hideMornings,
storageCache.hideMorningsTime
]
});
}
}
}
/**
* Called whenever a page is loaded in the browser
* Checks to see if this is the vonage login form and will attempt to
* login automatically if it has Vonage passcode
* @param details The event details containing the URL and tab ID
*/
function checkLogin(details) {
// Check if this is the okta login screen for Vonage
if (/https:\/\/login.okta.vonage.com\/.*/.test(details.url) && OTPAuth) {
// See if we have a vonage TOTP and if we do, try and use it
storageCache.totps.forEach((totp) => {
if (totp.label.match(/vonage/i)) {
// Generate the key
let key = new OTPAuth.TOTP({secret: totp.secret, algorithm: totp.algorithm, digits:totp.digits, period: totp.period}).generate();
// Send it to the UI
chrome.scripting.executeScript({
target: {tabId: details.tabId, allFrames: true},
func: loginToVonage,
args: [key]
});
}
});
}
}
// Import any libraries needed by this service worker
try {
importScripts('libs/otpauth/9.3.6/otpauth.umd.min.js');
}
catch (e) {
console.error(e);
}