-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimerwin.js
101 lines (81 loc) · 3.18 KB
/
timerwin.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
/// <reference path="intellisense.js" />
var g_bLoaded = false;
var g_bSentLoadedMsg = false;
function sendOnceLoadedMessage() {
if (g_bSentLoadedMsg)
return;
g_bSentLoadedMsg = true;
var params = getUrlParams();
var idCard = params.idCard;
var bClearAndMinimize = (params.minimized != "0");
sendExtensionMessage({ method: "timerWindowLoaded", idCard: idCard, bClearAndMinimize: bClearAndMinimize }, function (response) { });
}
var g_msStartWindow=0; //uninitialized
var g_bReadyForRestore = false;
const DELAY_START=300;
window.addEventListener("click", function (event) {
handleRestoreWindow(true);
});
window.addEventListener("load", function (event) {
g_msStartWindow= Date.now();
var params = getUrlParams();
var idCard = params.idCard;
//try to wait until the window is fully painted, Windows needs it so its minimized preview shows the content.
//this method isnt perfect. I also tried changing an img src and detect its onload but that also didnt always work,
//so the current approach is to wait extra
setTimeout(function () {
sendOnceLoadedMessage();
setTimeout(function () {
g_bReadyForRestore=true;
}, 100);
}, DELAY_START);
});
function handleRestoreWindow(bClicked) {
var params = getUrlParams();
var idCard = params.idCard;
sendOnceLoadedMessage();
var bLoadCard = true;
//workarround temporarily for Mac, https://bugs.chromium.org/p/chromium/issues/detail?id=928735
if (navigator.userAgent.indexOf("Mac OS")!= -1 && !bClicked)
bLoadCard = false;
if (bLoadCard)
sendExtensionMessage({ method: "timerWindowRestored", idCard: idCard }, function (response) {
});
}
function checkNeedHandleRestoreWindow() {
if (!g_bReadyForRestore || g_msStartWindow==0 || document.visibilityState != "visible")
return;
if (Date.now()-g_msStartWindow<DELAY_START)
return;
g_msStartWindow= Date.now();
handleRestoreWindow();
}
window.onfocus = function () {
checkNeedHandleRestoreWindow();
};
document.addEventListener('DOMContentLoaded', function () {
if (g_bLoaded)
return;
g_bLoaded = true;
var params = getUrlParams();
var idCard = params.idCard;
var hash = getCardTimerSyncHash(idCard);
$("#cardText").text(params.nameCard);//.prop("title", params.nameCard);
$("#boardText").text(params.nameBoard);
function update() {
getCardTimerData(hash, function (objTimer) {
var stored = objTimer.stored;
if (stored === undefined || stored.msStart == null || stored.msEnd != null) {
document.title = "00:00m";
sendOnceLoadedMessage();
window.close();
}
else {
document.title = getTimerElemText(stored.msStart, Date.now(), false, true);
checkNeedHandleRestoreWindow();
}
});
}
update();
setInterval(update, 1000); //review this could be re-done with timeouts with minute-step as now the windows is always minimized
});