-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
80 lines (73 loc) · 2.59 KB
/
utility.js
File metadata and controls
80 lines (73 loc) · 2.59 KB
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
export function AlarmData() {
this.totalUsingTime = 0;
this.lastAlarmTime = 0;
this.updateTime = -1;
};
export function ValueHandler(key, value)
{
console.debug(`init <${key}> : ${JSON.stringify(value)}`);
this.key = key;
this.value = value;
this.listeners = new Array();
let valueHandler = this;
chrome.storage.local
.onChanged
.addListener(
function(changes)
{
let targetChange = changes[key];
if (targetChange !== undefined)
{
valueHandler.value = targetChange.newValue;
valueHandler.listeners.forEach(function(callBack)
{
callBack(targetChange.oldValue, targetChange.newValue);
});
}
});
};
ValueHandler.prototype =
{
addListener: function(newListener)
{
if (this.listeners.indexOf(newListener) != -1)
return;
this.listeners.push(newListener);
},
removeListener: function(target)
{
var listenerIndex = this.listeners.indexOf(target);
if (listenerIndex == -1)
return;
this.listeners.slice(listenerIndex, 1);
},
saveValue: function(newValue)
{
console.debug(`save <${this.key}> : ${JSON.stringify(newValue)}`);
return chrome.storage.local.set({[this.key] : newValue});
}
};
export const timeSaverInitializationPromise = (
async function () {
async function createValueHandler(key, defaultValue)
{
let value = (await chrome.storage.local.get(key))[key];
return new ValueHandler(key, value ?? defaultValue);
}
let dataHandlers = await Promise.all(
[
createValueHandler("alarmPeriod", 10),
createValueHandler("rawData", []),
createValueHandler("snsAlarmData", new AlarmData())
]);
return {
alarmName: "mySnsAlarm",
dataToInfoString: (data, alarmPeriod) => `Total spending time : ${data.totalUsingTime.toFixed(0)} minutes.\nNext alarm time : ${(data.lastAlarmTime + alarmPeriod).toFixed(0)} minutes`,
dataHandler:
{
alarmPeriod: dataHandlers[0],
urlList: dataHandlers[1],
alarmData: dataHandlers[2]
},
};
})();