-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.js
166 lines (141 loc) · 4.92 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
/// <reference path="chrome.d.ts" />
const noOp = () => {};
const nuLL = () => null;
const d = new Date();
const toTime = (n) => {
const [hours, minutes] = n;
const adjustedHours = hours >= 12 ? hours - 12 : hours; // Adjust hours if greater than or equal to 12
const adjustedMinutes = minutes || 0;
d.setHours(adjustedHours, adjustedMinutes);
return d.getTime() | 0;
};
const arrayChunks = (array, chunk_size) =>
Array(Math.ceil(array.length / chunk_size))
.fill()
.map((_, index) => index * chunk_size)
.map((begin) => array.slice(begin, begin + chunk_size));
const calculateCost = ({ hourlyRate, durations }) => {
if (durations < 1) {
return 0;
}
const hourlyRateInMinutes = hourlyRate / 60; // Convert hourly rate to minutes
return hourlyRateInMinutes * durations;
};
const id = { costly: "mCostly" };
function Message(props) {
const element = document.createElement("div");
const iconWrapper = document.createElement("div");
const contentWrapper = document.createElement("div");
iconWrapper.style.paddingLeft = "28px";
iconWrapper.style.width = "40px";
iconWrapper.style.maxHeight = "52px";
const icon = document.createElement("i");
icon.setAttribute("class", "google-material-icons");
icon.innerHTML = "attach_money";
iconWrapper.appendChild(icon);
element.id = id.costly;
element.style.width = "100%";
element.style.display = "flex";
element.style.alignItems = "center";
element.style.minHeight = "40px";
element.style.paddingRight = "16px";
element.style.color = "rgb(60,64,67)";
element.appendChild(iconWrapper);
contentWrapper.style.letterSpacing = ".2px";
Object.entries(props).forEach(([k, v]) => {
const tag = document.createElement(k === "cost" ? "strong" : "span");
if (k === "cost") {
tag.style.color = "rgb(234,67,53)";
tag.style.padding = "0 0 0 0.1em";
}
tag.appendChild(document.createTextNode(v));
contentWrapper.appendChild(tag);
});
element.appendChild(contentWrapper);
return element;
}
const getDurations = (timeStr) => {
// originally using U+2013 `–` and `-` is U+002d
const [d1, d2] = (timeStr || "")
.split("–")
.map((s) => {
const re = /(am|pm)/;
const hasPM = /pm/.test(s);
const [h, m] = s
.replace(re, "")
.split(":")
.map((n) => n | 0);
return hasPM && h <= 12 ? [h + 12, m] : [h, m];
})
.map(toTime);
return Math.floor((d2 - d1) / (1000 * 60));
};
const getOptions = () =>
new Promise((resolve, reject) => {
chrome.storage.sync.get(["hourlyRate", "currency"], (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(result);
}
});
});
function makeViewCostly() {
// when user click event at calendar.google.com
const selector = () => document.querySelector("[data-open-edit-note]");
const getNode = () => selector().childNodes[0].childNodes[2];
const getTimeNode = () =>
getNode().childNodes[0].childNodes[1].childNodes[0].childNodes[1]
.childNodes[2].firstChild.textContent;
const getParticipantsNode = () =>
document.querySelector("[data-show-working-location-actions]")
.firstElementChild.childNodes[1].firstChild;
// get options first
getOptions()
.then((options) => ({
options,
node: getNode(),
timeNode: getTimeNode(),
participantsNode: getParticipantsNode(),
}))
.then(({ options, node, timeNode, participantsNode }) => {
// participantsNode.childNodes.forEach((n) => console.log(n.textContent));
const durations = getDurations(timeNode);
const costEstimation = calculateCost({
hourlyRate: options.hourlyRate | 0,
durations: durations | 0,
});
const [currency, countryCode] = options.currency.split(".");
const formatCurrency = new Intl.NumberFormat(countryCode || "en-US", {
style: "currency",
currency: currency || "USD",
currencyDisplay: "symbol",
}).format;
// formatCurrency_(options.currency.split('.')[1] || "USD")
const costNode = Message({
title: "Meeting cost per-person ",
cost: formatCurrency(costEstimation),
});
node.childNodes[1].append(costNode);
if (participantsNode.firstChild.textContent) {
const participants =
(participantsNode.firstChild.textContent || "").replace(/\D+/, "") |
1;
const participantNode = document.createElement("strong");
participantNode.style.color = "rgb(234,67,53)";
participantNode.textContent = formatCurrency(
costEstimation * participants
);
participantsNode.firstChild.textContent += " - ";
participantsNode.firstChild.append(participantNode);
}
})
.catch(noOp);
}
let intervalId;
const listener = () => {
if (!document.getElementById(id.costly)) {
makeViewCostly();
}
};
intervalId = setInterval(listener, 50);