forked from co3moz/socialimiter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
81 lines (64 loc) · 2.38 KB
/
main.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
!function () {
var check = ["facebook.com", "twitter.com"].find(function (e) {
return new RegExp(e).test(location.hostname);
});
if (!check) return;
var time, t, clock, stopMe, sync;
chrome.storage.sync.get(function (data) {
(data || (data = {}));
(data[check] || (data[check] = {}));
t = today();
time = data[check][t] || (data.settings && data.settings.allowedTime)|| 1800;
clock = document.createElement("span");
clock.className = "socialimiterSpan";
document.body.appendChild(clock);
stopMe = setInterval(everyLittleSecond, 1000);
sync = setInterval(syncData, 2100);
});
function everyLittleSecond () {
if (isNaN(time)) {
return location.reload();
}
if(document.hidden) return;
if (--time <= 0) {
createNopeScreen();
clearInterval(stopMe);
return clearInterval(sync);
}
clock.style.display = "block";
clock.innerText = intToTime(time) + chrome.i18n.getMessage("left");
}
function syncData () {
if(document.hidden) return;
chrome.storage.sync.get(function (data) {
(data || (data = {}));
(data[check] || (data[check] = {}));
data[check][t] = time;
if (!isNaN(time)) {
chrome.storage.sync.set(data);
}
});
}
function createNopeScreen () {
document.body.innerHTML = "";
var span = document.createElement("span");
var imagespan = document.createElement("span");
imagespan.className = "socialimiterNope";
span.className = "socialimiterText";
span.innerHTML = chrome.i18n.getMessage("nomore").replace("%%", check.replace(/\.com|www\.|\.net|\.org/, ""));
document.body.style.backgroundColor = "#e9eaed";
document.body.appendChild(imagespan);
document.body.appendChild(span);
}
function intToTime (time) {
if (time <= 0) return "";
if (time < 60) return (time | 0) + chrome.i18n.getMessage("seconds");
if (time < 3600) return (time / 60 | 0) + chrome.i18n.getMessage("minutes") + intToTime(time % 60);
if (time < 86400) return (time / 3600 | 0) + chrome.i18n.getMessage("hours") + intToTime(time % 3600);
if (time < 2592000) return (time / 86400 | 0) + chrome.i18n.getMessage("days") + intToTime(time % 86400);
return (time / 2592000 | 0) + chrome.i18n.getMessage("months") + intToTime(time % 86400);
}
function today (x) {
return (x || Date.now()) / (1000 * 60 * 60 * 24) | 0
}
}();