-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
179 lines (161 loc) · 4.83 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
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
176
177
178
179
importScripts('src/filters.js');
importScripts('src/selection.js');
let isSelecting = false;
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'viewFilters',
title: chrome.i18n.getMessage("viewFilterList"),
contexts: ['action']
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'viewFilters') {
chrome.runtime.openOptionsPage();
}
});
// 액션 클릭 핸들러
chrome.action.onClicked.addListener(async (tab) => {
if (isSelecting) {
// 선택 모드 종료
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
function: cleanupSelection
});
} catch (error) {
console.error('Cleanup script execution failed:', error);
}
isSelecting = false;
// 필터 수를 다시 표시
updateFilterBadge(tab.id);
} else {
// 선택 모드 시작
isSelecting = true;
await chrome.action.setBadgeText({
text: 'ON',
tabId: tab.id
});
await chrome.action.setBadgeBackgroundColor({
color: '#4CAF50',
tabId: tab.id
});
await chrome.action.setTitle({
title: '${chrome.i18n.getMessage("startSelection")}',
tabId: tab.id
});
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
function: startSelection
});
} catch (error) {
console.error('Error when script execution:', error);
isSelecting = false;
await chrome.action.setBadgeText({
text: 'ERR',
tabId: tab.id
});
await chrome.action.setBadgeBackgroundColor({
color: '#F44336',
tabId: tab.id
});
}
}
});
// 필터 수 업데이트 함수
async function updateFilterBadge(tabId) {
try {
// 선택 모드가 활성화되어 있다면 업데이트하지 않음
if (isSelecting) return;
const tab = await chrome.tabs.get(tabId);
const url = new URL(tab.url);
const domain = url.hostname;
const { filters = [] } = await chrome.storage.local.get('filters');
const domainFilters = filters.filter(f => f.domain === domain);
if (domainFilters.length > 0) {
await chrome.action.setBadgeText({
text: domainFilters.length.toString(),
tabId: tabId
});
await chrome.action.setBadgeBackgroundColor({
color: '#2196F3',
tabId: tabId
});
await chrome.action.setTitle({
title: chrome.i18n.getMessage("activeFiltersCount", [domainFilters.length.toString()]),
tabId: tabId
});
} else {
await chrome.action.setBadgeText({
text: '',
tabId: tabId
});
await chrome.action.setTitle({
title: chrome.i18n.getMessage("startElementSelection"),
tabId: tabId
});
}
} catch (error) {
console.error('배지 업데이트 중 오류:', error);
}
}
chrome.webNavigation.onCompleted.addListener(async (details) => {
// 페이지 로드 완료 시 필터 적용
if (details.frameId === 0) {
try {
const { filters = [] } = await chrome.storage.local.get('filters');
const url = new URL(details.url);
const domain = url.hostname;
const domainFilters = filters.filter(f => f.domain === domain);
if (domainFilters.length > 0) {
// 선택 모드가 아닐 때만 필터 수 표시
if (!isSelecting) {
updateFilterBadge(details.tabId);
}
await chrome.scripting.executeScript({
target: { tabId: details.tabId },
func: applyFilters,
args: [domainFilters]
});
} else {
if (!isSelecting) {
await chrome.action.setBadgeText({
text: '',
tabId: details.tabId
});
}
}
} catch (error) {
console.error('필터 적용 중 오류:', error);
}
}
});
// ESC 키나 저장 완료 후 배지 업데이트
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'selectionCanceled' || message.action === 'filterSaved') {
isSelecting = false;
updateFilterBadge(sender.tab.id);
}
});
// 필터 저장 메시지 리스너
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'getCurrentTabId') {
sendResponse({ tabId: sender.tab.id });
return true;
}
if (message.action === 'saveFilter') {
const filter = message.filter;
filter.tabId = message.tabId;
saveFilter(filter)
.then(() => {
sendResponse({ success: true });
// 저장 후 즉시 필터 적용
return applyNewFilter(filter);
})
.catch(error => {
console.error('필터 저장/적용 중 오류:', error);
sendResponse({ success: false, error: error.message });
});
return true;
}
});