-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
302 lines (236 loc) · 10.4 KB
/
content.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
function findShortcutStoryElementAndAppendTimer(hasTimerStarted) {
const shortcutStoryModalContainerId = "story-dialog-parent";
const storyModal = document.getElementById(shortcutStoryModalContainerId);
if (!storyModal) {
console.log("Story modal not found");
return;
}
const harvestTimerID = "polaris-harvest-timer";
if (storyModal) {
const {
storyId,
storyName,
storyPermaLink
} = getShortcutStoryDetails();
const elementToAppendId = document.getElementById('cid-breadcrumbs-story-dialog');
if (elementToAppendId) {
console.log("Element found to append timer.");
if (!storyId || !storyName || !storyPermaLink) {
console.log("Story details not found.");
return;
}
createTimeActionButton("shortcut", elementToAppendId, harvestTimerID, storyId, storyName, storyPermaLink, hasTimerStarted);
window._harvestPlatformConfig = {
"applicationName": "Crowdlinker",
"permalink": window.location.href,
"skipStyling": true // Override the default styling
};
// Use to dispatch event after loading buttons
const event = new CustomEvent("harvest-event:timers:add", {
detail: { element: document.querySelector(".harvest-timer") }
});
document.querySelector("#harvest-messaging")?.dispatchEvent(event);
}
}
}
function findGoogleCalendarEventAndAppendTimer(hasTimerStarted) {
const googleCalendarEventContainer = document.querySelector('[data-open-edit-note]') || document.querySelector('[data-is-adaptive]');
const eventHeaderElement = document.querySelector('.wv9rPe');
if (!googleCalendarEventContainer || !eventHeaderElement) {
return;
}
const harvestTimerID = "polaris-harvest-timer";
if (!document.getElementById(harvestTimerID)) {
const eventName = googleCalendarEventContainer.querySelector('[data-text]')?.textContent;
// For two different types of google calendar event modals
// 1- Meeting Invites
// 2- Focus Time
const eventId = googleCalendarEventContainer.getAttribute('data-eventid') || googleCalendarEventContainer.querySelector('[data-eventid]')?.getAttribute('data-eventid');
createTimeActionButton("google-calendar", eventHeaderElement, harvestTimerID, eventId, eventName, '', hasTimerStarted);
window._harvestPlatformConfig = {
"applicationName": "Crowdlinker",
"permalink": window.location.href,
"skipStyling": true // Override the default styling
};
// Use to dispatch event after loading buttons
const event = new CustomEvent("harvest-event:timers:add", {
detail: { element: document.querySelector(".harvest-timer") }
});
document.querySelector("#harvest-messaging")?.dispatchEvent(event);
}
}
console.log("Content script loaded.");
window.addEventListener("load", () => {
console.log("Window loaded.", { window });
// TODO: Not working as expected, need to fix this
// function observeDOM() {
// const observer = new MutationObserver(() => {
// console.log("DOM mutation detected.");
// detectStoryAndAppendTimerElement();
// });
// observer.observe(document.body, { childList: true, subtree: true });
// }
setInterval(() => {
chrome.storage.sync.get(['shortcutCheckBox', 'googleCalendarCheckBox', 'harvest_timer_started'], (result) => {
const { shortcutCheckBox, googleCalendarCheckBox, harvest_timer_started } = result;
if(window.location.href.includes(APP_CONSTANTS.SHORTCUT.baseDomain)) {
if(shortcutCheckBox) {
findShortcutStoryElementAndAppendTimer(!!harvest_timer_started?.external_reference);
} else if(!harvest_timer_started) {
const timerElement = document.getElementById("polaris-harvest-timer");
if (timerElement) {
timerElement.remove();
}
}
} else if(window.location.href.includes(APP_CONSTANTS.GOOGLE_CALENDAR.baseDomain)) {
if(googleCalendarCheckBox) {
findGoogleCalendarEventAndAppendTimer(!!harvest_timer_started?.external_reference);
} else if(!harvest_timer_started) {
const timerElement = document.getElementById("polaris-harvest-timer");
if (timerElement) {
timerElement.remove();
}
}
}
});
}, 500);
});
function getShortcutStoryDetails() {
const storyDialogElement = document.querySelector(".story-dialog");
if (!storyDialogElement) {
return null;
}
const storyId = storyDialogElement?.getAttribute("data-id");
const storyName = storyDialogElement?.querySelector(".story-name")?.textContent;
const storyAttributesElement = storyDialogElement?.querySelector(".story-attributes");
const storyPermaLink = storyAttributesElement?.querySelector('.attribute > span+button')?.baseURI;
return {
storyId,
storyName,
storyPermaLink
};
}
// Using Standard JavaScript:
document.body.addEventListener("harvest-event:ready", function (event) {
console.log("Harvest Buttons are ready.");
});
// Listen for messages from the injected script
window.onmessage = (event) => {
if (event.origin !== APP_CONSTANTS.HARVEST.baseDomain) {
return;
}
console.log(`Received message`);
const { type, value } = event.data;
if (type === "timer:started") {
console.log("Timer started.");
handleStartTimeEvent(value);
}
if (type === "timer:stopped") {
console.log("Timer stopped.");
handleStopTimeEvent(value)
}
};
function createTimeActionButton(type, elementToAppend, harvestTimerID, id, name, storyPermaLink, hasTimerStarted) {
let buttonElement = document.getElementById("start_time_btn");
if (!buttonElement) {
buttonElement = document.createElement('button');
}
buttonElement.id = "start_time_btn";
buttonElement.className = "harvest-timer";
buttonElement.setAttribute('data-id', id);
buttonElement.style = "background: transparent; cursor: pointer; padding: 8px; border-radius: 8px; border: 1px solid grey; color: #fff; font-weight: 700; font-size: 14px; border: none;";
buttonElement.innerHTML = hasTimerStarted ? "Stop Timer" : "Start Timer";
buttonElement.style.backgroundColor = hasTimerStarted ? "rgb(189 54 54)" : "#188433";
switch (type) {
case "shortcut":
buttonElement.dataset.item = JSON.stringify({
"id": id,
"name": `${id} - ${name}`,
"permalink": storyPermaLink || '',
"type": "shortcut_app"
});
break;
case "google-calendar":
buttonElement.dataset.item = JSON.stringify({
"id": id,
"name": `Meeting - ${name}`,
"permalink": APP_CONSTANTS.GOOGLE_CALENDAR.baseDomain,
"type": "google_calendar"
});
}
timerElement = document.getElementById("polaris-harvest-timer") || document.createElement('div');
switch (type) {
case "shortcut":
timerElement.style = "float:right; margin: 0px 20px;";
break;
case "google-calendar":
timerElement.style = "float:right; margin: 0px 20px; position: absolute; left: 0; margin-top: 4px;";
break;
}
timerElement.id = harvestTimerID;
timerElement.innerHTML = `
<div class="harvest_timer_btn_div"> </div>
`;
timerElement.appendChild(buttonElement);
elementToAppend.appendChild(timerElement);
}
function handleStartTimeEvent(eventValue) {
const buttonElement = document.getElementById("start_time_btn");
if (buttonElement) {
buttonElement.innerHTML = "Stop Timer";
buttonElement.style.backgroundColor = "rgb(189 54 54)";
if (eventValue?.external_reference?.permalink?.includes(APP_CONSTANTS.SHORTCUT.baseDomain) || eventValue?.external_reference?.permalink?.includes(APP_CONSTANTS.GOOGLE_CALENDAR.baseDomain)) {
const serviceType = getServiceTypeByDomain(eventValue?.external_reference?.permalink);
chrome.storage.sync.set({ 'harvest_timer_started': {
...eventValue,
service_type: serviceType
} });
}
}
}
function getServiceTypeByDomain(domain) {
if (domain.includes(APP_CONSTANTS.SHORTCUT.baseDomain)) {
return "shortcut";
}
if (domain.includes(APP_CONSTANTS.GOOGLE_CALENDAR.baseDomain)) {
return "google_calendar";
}
}
function handleStopTimeEvent(eventValue) {
const buttonElement = document.getElementById("start_time_btn");
if (buttonElement) {
buttonElement.innerHTML = "Start Timer";
buttonElement.style.backgroundColor = "#188433";
if (eventValue?.external_reference?.permalink?.includes(APP_CONSTANTS.SHORTCUT.baseDomain) || eventValue?.external_reference?.permalink?.includes(APP_CONSTANTS.GOOGLE_CALENDAR.baseDomain)) {
chrome.storage.sync.remove('harvest_timer_started');
}
}
}
// Listen for chrome storage changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if(namespace === 'sync'){
if(changes.harvest_timer_started) {
const olderValue = changes?.harvest_timer_started?.oldValue;
const newValue = changes?.harvest_timer_started?.newValue;
// Timer stopped state, since value is removed
if(!newValue && olderValue?.external_reference) {
handleStopTimeEvent(olderValue);
}
}
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'harvestAppDetected') {
// Check if timer is stopped
const stopTimerElement = document.querySelector(".js-stop-timer");
if(!stopTimerElement) {
console.log("No timer is running.");
return;
}
stopTimerElement.addEventListener('click', () => {
console.log("Timer stopped.");
chrome.storage.sync.remove('harvest_timer_started');
});
}
return true;
});