-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
87 lines (77 loc) · 2.24 KB
/
background.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
// 백그라운드 메시지 수신부분
chrome.runtime.onMessage.addListener(function (request, sender, sendResopnse) {
if (!request.name) {
return sendResopnse(false);
}
if (request.name === "alarm") {
if (request.minutes === 10) return sendResopnse({ longTime: true });
createAlarm({ minutes: request.minutes });
return sendResopnse(true);
} else if (request.name === "clear") {
clearAlarms();
clearNotifications();
return sendResopnse(true);
} else if (request.name === "inject") {
injectScriptCode();
return sendResopnse(true);
}
});
// 알람 생성
const createAlarm = (alarmInfo) => {
chrome.action.setBadgeText({ text: "ON" });
chrome.alarms.create("notiAlarm", {
delayInMinutes: alarmInfo.minutes,
});
};
// 알람 이벤트 리스너
chrome.alarms.onAlarm.addListener((eventAlarm) => {
chrome.action.setBadgeText({ text: "" });
if (eventAlarm.name === "notiAlarm") {
createNotification();
}
});
// 노티 생성
const createNotification = (notificationInfo) => {
chrome.notifications.create({
type: "basic",
iconUrl: "./images/icon-cyclist-24.png",
title: "Time to Rest!",
message: "Need a break time",
buttons: [{ title: "Go?" }],
priority: 0,
});
};
// 노티 옵션 클릭 이벤트 리스너
chrome.notifications.onButtonClicked.addListener(async () => {
const item = await chrome.storage.sync.get(["minutes"]);
createAlarm({ minutes: item.minutes });
});
// 알람 삭제
const clearAlarms = () => {
chrome.alarms.clearAll();
};
// 노티 삭제
const clearNotifications = () => {
chrome.notifications.getAll((notifications) => {
Object.keys(notifications).forEach((notificationId) => {
if (notifications[notificationId]) {
chrome.notifications.clear(notificationId);
}
});
});
};
// 스크립트 코드 주입하기
const injectScriptCode = async () => {
const curTab = await getCurrentTab();
console.log(curTab);
chrome.scripting.executeScript({
target: { tabId: curTab.id },
files: ["./script.js"],
});
};
// 현재 탭 위치 알아내기
const getCurrentTab = async () => {
const queryOptions = { active: true, lastFocusedWindow: true };
const [tab] = await chrome.tabs.query(queryOptions);
return tab;
};