-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome_scripts.js
175 lines (157 loc) · 4.83 KB
/
chrome_scripts.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
const mainMemoryKey = "cdbm_timers_storage";
const getFromChromeSyncStorage = async (key=mainMemoryKey) => {
return new Promise((resolve) => {
chrome.storage.sync.get([key], (result) => {
const dataString = result[key] || "[]";
const data = JSON.parse(dataString);
resolve(data);
});
});
};
const saveToChromeSyncStorage = async (data, { key = mainMemoryKey, array = true } = {}) => {
if(!Array.isArray(data) && array){
data = [];
}
return new Promise((resolve) => {
chrome.storage.sync.set({ [key]: JSON.stringify(data) }, () => {
console.log(`Data (${key}) is saved`);
resolve(true);
});
});
};
const removeFromChromeSyncStorage = async (key=mainMemoryKey) => {
return new Promise((resolve) => {
chrome.storage.sync.remove([key], () => {
console.log(`Data (${key}) is removed`);
resolve(true);
});
});
};
const getFromChromeLocalStorage = async (key=mainMemoryKey) => {
return new Promise((resolve) => {
chrome.storage.local.get([key], (result) => {
const dataString = result[key] || "[]";
const data = JSON.parse(dataString);
resolve(data);
});
});
};
const saveToChromeLocalStorage = async (data, key=mainMemoryKey) => {
if(!Array.isArray(data)){
data=[];
}
return new Promise((resolve) => {
chrome.storage.local.set({ [key]: JSON.stringify(data) }, () => {
console.log(`Data (${key}) is saved in local storage`);
resolve(true);
});
});
};
const removeFromChromeLocalStorage = async (key=mainMemoryKey) => {
return new Promise((resolve) => {
chrome.storage.local.remove([key], () => {
console.log(`Data (${key}) is removed from local storage`);
resolve(true);
});
});
};
const triggerChromeNotification = (notificationMessage) => {
const options = {
type: 'basic',
title: 'Countdown timer by Michal',
message: notificationMessage,
iconUrl: 'logos/logo128.png', // Zmień ścieżkę do odpowiedniego obrazka ikony
};
chrome.notifications.create(options, (notificationId) => {
if (chrome.runtime.lastError) {
console.error('Error creating notification:', chrome.runtime.lastError.message);
return false;
} else {
console.log('Notification created with ID:', notificationId);
return true;
}
});
};
const createTab = async (url = "") => {
return new Promise((resolve) => {
if (url === "") {
chrome.tabs.create({}, (tab) => {
resolve(tab);
});
} else {
chrome.tabs.create({ url }, (tab) => {
resolve(tab);
});
}
});
};
const closeTab = async (url = "") => {
return new Promise((resolve) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tabToClose = tabs[0];
if (url === "") {
chrome.tabs.remove(tabToClose.id, () => {
resolve(true);
});
} else {
chrome.tabs.query({ url }, (matchingTabs) => {
if (matchingTabs.length > 0) {
chrome.tabs.remove(matchingTabs[0].id, () => {
resolve(true);
});
} else {
resolve(false);
}
});
}
});
});
};
const checkIfAlarmExists = async (name) => {
return new Promise((resolve) => {
chrome.alarms.get(name, (alarm) => {
resolve(!!alarm);
});
});
};
const createAlarm = async (name, delayInMinutes, periodInMinutes) => {
try {
await chrome.alarms.create(name, { delayInMinutes, periodInMinutes });
return true;
} catch (error) {
console.error('Error creating alarm:', error);
return false;
}
};
const setAlarmIfNotExist = () => {
if(!checkIfAlarmExists("timers")){
createAlarm("timers", 1, 1);
console.log("Alarm is set");
}
}
const removeAlarm = (name) => {
return new Promise((resolve) => {
chrome.alarms.clear(name, (wasCleared) => {
resolve(wasCleared);
});
});
};
//Update JSON
const updateTimers = async () => {
const timers = await getFromChromeSyncStorage();
const updated = [];
timers.forEach(e => {
if(!e.hasOwnProperty('recurring')){
e["recurring"] = {
"active": true,
"every": 1,
"time_unit": "week"
}
updated.push(true);
}
})
if(updated.includes(true)){
console.log(timers);
saveToChromeSyncStorage(timers);
}
}