Skip to content

Commit

Permalink
add: caluclation into the minute (by: imam)
Browse files Browse the repository at this point in the history
  • Loading branch information
k1m0ch1 authored and r17x committed Jul 17, 2023
1 parent 2837d2c commit 485d918
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/// <reference path="chrome.d.ts" />

const noOp = () => { };
const noOp = () => {};
const nuLL = () => null;
const d = new Date();

const toTime = (n) => {
d.setHours(...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;
};

Expand All @@ -15,7 +17,13 @@ const arrayChunks = (array, chunk_size) =>
.map((_, index) => index * chunk_size)
.map((begin) => array.slice(begin, begin + chunk_size));

const calculateCost = ({ hourlyRate, durations }) => hourlyRate * durations;
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" };

Expand Down Expand Up @@ -59,23 +67,33 @@ function Message(props) {
}

const getDurations = (timeStr) => {
// originally using U+2013 `–` and `-` is U+002d
const [d1, d2] = (timeStr || "")
.split("–") // `–` is not equal `-`
.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];
return hasPM && h <= 12 ? [h + 12, m] : [h, m];
})
.map(toTime);

return Math.floor((d2 - d1) / (1000 * 60 * 60));
return Math.floor((d2 - d1) / (1000 * 60));
};

const getOptions = () => chrome.storage.sync.get(["hourlyRate", "currency"]);
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
Expand Down Expand Up @@ -115,7 +133,7 @@ function makeViewCostly() {
// formatCurrency_(options.currency.split('.')[1] || "USD")

const costNode = Message({
title: "Estimated Meeting Cost is ",
title: "Hourly Meeting Cost is ",
cost: formatCurrency(costEstimation),
});

Expand All @@ -137,10 +155,14 @@ function makeViewCostly() {
.catch(noOp);
}

const listener = () =>
setInterval(
() => !document.getElementById(id.costly) && makeViewCostly(),
300
);
let intervalId;

const listener = () => {
if (!document.getElementById(id.costly)) {
makeViewCostly();
} else {
//clearInterval(intervalId);
}
};

listener();
intervalId = setInterval(listener, 50);

0 comments on commit 485d918

Please sign in to comment.