-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathweb_timer.js
546 lines (491 loc) · 14.6 KB
/
web_timer.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import { UfsGlobal } from "./content-scripts/ufs_global.js";
import { BADGES } from "./helpers/badge.js";
const graphIcon = '<i class="fa-solid fa-chart-pie"></i>';
function openGraph() {
window.open("/scripts/web_timer.html", "_self");
}
export default {
icon: '<i class="fa-solid fa-hourglass-half fa-lg fa-spin-pulse"></i>',
name: {
en: "Web timer",
vi: "Thời gian lướt web",
},
description: {
en: `Keep track of how you spend your time on the web.<br/>
<h3>CLICK ${graphIcon} TO OPEN GRAPH.</h3>`,
vi: `Lưu lại / Kiểm tra thời gian lướt web của bạn cho từng trang web.<br/>
<h3>BẤM NÚT ${graphIcon} ĐỂ XEM BIỂU ĐỒ.</h3>`,
img: "/scripts/web_timer.png",
},
badges: [BADGES.hot],
buttons: [
{
icon: graphIcon,
name: {
en: "Open Graph",
vi: "Xem biểu đồ",
},
onClick: openGraph,
},
{
icon: '<i class="fa-regular fa-clock"></i>',
name: {
en: "For developers - Coding timer",
vi: "Cho developer - Thời gian code",
},
onClick: () => window.open("https://wakatime.com/", "_blank"),
},
],
changeLogs: {
["2024-05-10"]: "init",
},
popupScript: {
onEnable: async () => {
const { t } = await import("../popup/helpers/lang.js");
const { runScriptInTabWithEventChain, getAllTabs } = await import(
"./helpers/utils.js"
);
const tabs = await getAllTabs();
let count = 0;
for (let tab of tabs) {
try {
runScriptInTabWithEventChain({
target: {
tabId: tab.id,
allFrames: true,
},
scriptIds: ["web_timer"],
eventChain: "contentScript.onDocumentStart_",
world: "ISOLATED",
});
count++;
} catch (e) {
console.error(e);
}
}
if (count)
await Swal.fire({
icon: "success",
title: t({
vi: "Đã bật",
en: "Enabled",
}),
html: t({
vi: "Đã BẬT bộ đếm thời gian cho " + count + " tab đang mở",
en: "Enabled timer for " + count + " opening tabs",
}),
});
},
onDisable: async () => {
const { t } = await import("../popup/helpers/lang.js");
const { runScriptInTab, getAllTabs } = await import("./helpers/utils.js");
const tabs = await getAllTabs();
let count = 0;
for (let tab of tabs) {
try {
let res = await runScriptInTab({
target: {
tabId: tab.id,
allFrames: true,
},
func: () => {
if (typeof window.ufs_web_timer_cleanup === "function") {
window.ufs_web_timer_cleanup();
return true;
}
return false;
},
world: "ISOLATED",
});
if (res) count++;
} catch (e) {
console.error(e);
}
}
if (count)
await Swal.fire({
icon: "success",
title: t({
vi: "Đã tắt",
en: "Disabled",
}),
html: t({
vi: "Đã TẮT bộ đếm thời gian cho " + count + " trang tab",
en: "Disabled timer for " + count + " tabs",
}),
});
},
onClick: openGraph,
},
contentScript: {
onDocumentStart_: run,
},
};
function run() {
if (typeof window.ufs_web_timer_cleanup === "function") {
window.ufs_web_timer_cleanup();
}
const isMainFrame = window === window.top;
const cleanupFn = [];
function addEventListener(target, event, func, options) {
target.addEventListener(event, func, options);
cleanupFn.push(() => target.removeEventListener(event, func));
}
window.ufs_web_timer_cleanup = () => {
if (typeof saveTimer === "function") saveTimer();
cleanupFn.forEach((fn) => fn());
};
// track user events: mouse, keyboard, touch, ...
let needUpdateLastActive = true;
let updateLastActive = throttle(function (e, mainframe) {
needUpdateLastActive = true;
}, 1000 / 24);
const allEvents = [
// https://javascript.info/pointer-events
"pointerover",
"pointerenter",
"pointerdown",
"pointermove",
"pointerup",
"pointerleave",
"click",
"contextmenu",
"touchstart",
"touchmove",
"touchend",
"keydown",
"keyup",
"keypress",
"wheel",
"scroll",
"blur",
// "focusin",
// "focusout",
"focus",
];
const updateLastActiveMsg = "ufs_web_timer_updateLastActive";
const overlayId = "ufs-web-timer-overlay";
allEvents.forEach((event) => {
// main frame => update last active directly
if (isMainFrame) {
addEventListener(window, event, (e) => {
if (e.target?.id == overlayId) return;
updateLastActive?.(event, true);
});
} else {
// iframes / subframes => postMessage to main frame
addEventListener(
window,
event,
UfsGlobal.Utils.throttle((e) => {
// iframe's content_script cannot call window.top in mainframe's content_script
// SO only solution is use postMessage
window.top.postMessage(
{
type: updateLastActiveMsg,
event: event,
},
"*"
);
}, 1000 / 24)
);
}
});
const checkFocusMsg = "ufs_web_timer_checkFocus";
if (!isMainFrame) {
addEventListener(window, "message", (e) => {
if (e.data?.type === checkFocusMsg) {
window.top.postMessage(
{
type: checkFocusMsg + "result",
uuid: e.data?.uuid,
focused: document.hasFocus(),
},
"*"
);
}
});
}
// iframe / subframe stop here, all logic below are for main frame
if (!isMainFrame) return;
// ======================================================
// ===================== MAIN FRAME =====================
// ======================================================
addEventListener(
window,
"message",
(e) => {
if (e.data?.type === updateLastActiveMsg) {
updateLastActive(e.data?.event, false);
}
},
false
);
// only check visibilityChange for outermost windows
addEventListener(document, "visibilitychange", () => {
if (!document.hidden) {
updateLastActive();
}
});
const INTERVAL_UPDATE = 1;
const INTERVAL_SAVE = 10;
const IDLE_TIME = 60;
const IDLE_TIME_IF_BLUR = 10;
const SHOW_OVERLAY = true;
const invisible = "\u200b";
let lastActive = 0;
let savedTimerValue = 0,
currentTimerValue = 0,
focusTimerValue = 0;
// get saved timer
getTodayTimer().then((todayTimer) => {
savedTimerValue = todayTimer.value;
});
const overlay = document.createElement("div");
overlay.id = overlayId;
overlay.style.cssText = `
position: fixed;
top: -100vh;
left: 0;
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.35);
z-index: 2147483646;
transition: top 0.5s ease;
`;
["mousemove", "click", "touchstart"].forEach((event) => {
addEventListener(overlay, event, updateLastActive);
});
function setShowOverlay(show) {
if (show) {
if (!document.body.contains(overlay)) {
// document.body.appendChild(overlay);
}
overlay.style.top = "0";
} else {
overlay.style.top = "-100vh";
}
}
let intervalUpdate = setInterval(async () => {
if (needUpdateLastActive) {
lastActive = performance.now();
needUpdateLastActive = false;
}
let idleState = await getIdleState();
if (idleState.isIdle) {
// UfsGlobal.DOM.notify({
// msg: `Useful script - Webtimer - IDLE state (${idleState.reason})`,
// duration: 2000,
// });
if (SHOW_OVERLAY && !document.hidden) setShowOverlay(true);
} else {
setShowOverlay(false);
}
if (!document.hidden && !idleState.isIdle) {
focusTimerValue++;
currentTimerValue = savedTimerValue + focusTimerValue;
makeTitle();
}
let now = new Date();
let isMidnight =
(now.getHours() === 23 &&
now.getMinutes() === 59 &&
now.getSeconds() === 59) ||
(now.getHours() === 0 &&
now.getMinutes() === 0 &&
now.getSeconds() === 0);
if (isMidnight) {
saveTimer();
}
}, INTERVAL_UPDATE * 1000);
cleanupFn.push(() => {
clearInterval(intervalUpdate);
setTimeout(() => makeTitle(true), INTERVAL_UPDATE * 1000);
});
let intervalSave = setInterval(() => {
saveTimer();
}, INTERVAL_SAVE * 1000);
cleanupFn.push(() => clearInterval(intervalSave));
addEventListener(window, "beforeunload", saveTimer);
// functions
function isFocused() {
// main frame focus
if (document.hasFocus()) return true;
// check iframes focus
let iframes = Array.from(document.querySelectorAll("iframe"));
if (!iframes.length) return false;
return new Promise((resolve) => {
let uuid = Math.random().toString(36);
setTimeout(() => {
window.removeEventListener("message", onReceiveMsg);
resolve(false);
}, 500);
// post message to all iframes to check
iframes.forEach((iframe) => {
iframe.contentWindow.postMessage(
{
type: checkFocusMsg,
uuid,
},
"*"
);
});
// window.postMessage({ type: checkFocusMsg, uuid }, "*");
addEventListener(window, "message", onReceiveMsg);
let msgReceivedCount = 0;
function onReceiveMsg(e) {
if (
e.data?.type === checkFocusMsg + "result" &&
e.data?.uuid === uuid
) {
if (e.data?.focused === true) {
window.removeEventListener("message", onReceiveMsg);
resolve(true);
}
msgReceivedCount++;
if (msgReceivedCount >= iframes.length) {
window.removeEventListener("message", onReceiveMsg);
resolve(false);
}
}
}
});
}
async function getIdleState() {
let timeToCheck = (await isFocused()) ? IDLE_TIME : IDLE_TIME_IF_BLUR;
// if not enough time passed since last active? => not idle
let timePassed = ~~(performance.now() - lastActive);
if (timePassed < timeToCheck * 1000)
return {
isIdle: false,
reason: "not enough time passed since last active " + timePassed,
};
// if any video / audio playing? => not idle
let allMedia = document.querySelectorAll("video, audio");
for (let media of allMedia) {
if (media.duration > 0 && !media.paused)
return {
isIdle: false,
reason: "video / audio playing",
};
}
// if this tab audible? => not idle
let tabs = await UfsGlobal.Extension.runInBackground("chrome.tabs.query", [
{
active: true,
audible: true,
url: location.href,
},
]);
let hasAudioPlaying = tabs?.length > 0;
if (hasAudioPlaying)
return {
isIdle: false,
reason: "this tab is audible ",
};
// if chrome.idle.queryState == active? => not idle
let t = Math.max(15, IDLE_TIME);
let state = await UfsGlobal.Extension.runInBackground(
"chrome.idle.queryState",
[t]
);
if (state != "active")
return {
isIdle: true,
reason: "no system events in " + t + " secs",
};
return {
isIdle: true,
reason: "no active events in " + timeToCheck + " secs",
};
}
async function saveTimer() {
let { web_timer, host, today, value } = await getTodayTimer();
let newValue = value + focusTimerValue;
web_timer[today][host] = newValue;
savedTimerValue = newValue;
focusTimerValue = 0;
await chrome.storage.local.set({ web_timer });
}
async function getTodayTimer() {
let host =
location.hostname ||
location.pathname?.split("/")?.at(-1) ||
location.href;
let today = getToday();
let web_timer = (await chrome.storage.local.get(["web_timer"]))?.web_timer;
if (typeof web_timer !== "object") web_timer = {};
if (typeof web_timer[today] !== "object") web_timer[today] = {};
if (!web_timer[today][host]) web_timer[today][host] = 0;
let value = Math.floor(web_timer[today][host]);
return { web_timer, host, today, value };
}
function getToday() {
return formatDate(new Date());
}
function formatDate(date) {
let year = date.getFullYear();
let month = padZero(date.getMonth() + 1);
let day = padZero(date.getDate());
return `${year}-${month}-${day}`;
}
function padZero(num) {
return num.toString().padStart(2, "0");
}
function secondsToTime(secs) {
let hours = Math.floor(secs / 3600);
let minutes = Math.floor((secs - hours * 3600) / 60);
let seconds = secs - hours * 3600 - minutes * 60;
if (hours == 0) hours = "";
else hours = `${hours}:`;
if (hours == 0 && minutes == 0) minutes = "";
else minutes = `${minutes}:`;
if (hours == 0 && minutes == 0 && seconds == 0) seconds = "";
return `${hours}${minutes}${seconds}`;
}
function makeTitle(revert) {
let time = secondsToTime(currentTimerValue) + " " + invisible;
let curTitle = document.title;
if (curTitle.includes(invisible)) {
curTitle = curTitle.split(invisible)[1];
}
let newTitle = revert ? curTitle : time + curTitle;
document.title = newTitle;
}
}
// https://dev.to/jeetvora331/throttling-in-javascript-easiest-explanation-1081
function throttle(mainFunction, delay) {
let timerFlag = null; // Variable to keep track of the timer
// Returning a throttled version
return (...args) => {
if (timerFlag === null) {
// If there is no timer currently running
mainFunction(...args); // Execute the main function
timerFlag = setTimeout(() => {
// Set a timer to clear the timerFlag after the specified delay
timerFlag = null; // Clear the timerFlag to allow the main function to be executed again
}, delay);
}
};
}
const backup = () => {
// export data from https://chrome.google.com/webstore/detail/ppaojnbmmaigjmlpjaldnkgnklhicppk
(async () => {
chrome.storage.local.get(["domains"], (domains) => {
let result = {};
Object.entries(JSON.parse(domains.domains)).forEach(([website, data]) => {
if (data.days) {
for (let day in data.days) {
if (!result[day]) result[day] = {};
if (!result[day][website]) result[day][website] = 0;
result[day][website] += data.days[day].seconds;
}
}
});
console.log(result);
});
})();
(() => {
chrome.storage.local.set({ web_timer });
})();
};