This repository was archived by the owner on Oct 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
201 lines (181 loc) · 7.14 KB
/
popup.js
File metadata and controls
201 lines (181 loc) · 7.14 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
document.addEventListener('DOMContentLoaded', () => {
const rulesTA = document.getElementById('rules');
const prefixTA = document.getElementById('prefixRules');
const selectorTA = document.getElementById('classoridRules');
const urlListTA = document.getElementById('urlList');
const saveBtn = document.getElementById('save');
const warningBox = document.getElementById('warningBox');
const autoGlobalChk = document.getElementById('autoGlobal');
const anchorPrefixChk = document.getElementById('anchorPrefix');
const autoResize = el => {
el.style.height = 'auto';
el.style.height = (el.scrollHeight + 5) + 'px';
};
[rulesTA, prefixTA, selectorTA, urlListTA].forEach(t => {
if (t) t.addEventListener('input', () => autoResize(t));
});
chrome.storage.sync.get(
[
'enable',
'loading',
'masking',
'rules',
'prefixRules',
'classoridRules',
'titleTransform',
'listMode',
'urlList',
'autoGlobal',
'anchorPrefix'
],
data => {
document.querySelector(`input[name="enable"][value="${data.enable || 'on'}"]`).checked = true;
document.querySelector(`input[name="titleTransform"][value="${data.titleTransform || 'on'}"]`).checked = true;
document.querySelector(`input[name="loading"][value="${data.loading || 'smooth'}"]`).checked = true;
document.querySelector(`input[name="masking"][value="${data.masking || 'replaceText'}"]`).checked = true;
document.querySelector(`input[name="listMode"][value="${data.listMode || 'none'}"]`).checked = true;
rulesTA.value = data.rules || [
'김민준 -> 홍길동',
'/\\d{4}-\\d{2}-\\d{2}/g -> [DATE]',
'/visa\\s*\\d{4}/i -> [카드번호]'
].join('\n');
prefixTA.value = data.prefixRules || [
'계좌번호: -> [보안 정보]',
'/^User\\s+ID:/i -> [USER ID 숨김]'
].join('\n');
selectorTA.value = data.classoridRules || '.privacy -> [보안 정보]\n#sensitive-data -> [민감 정보]';
urlListTA.value = data.urlList || 'example.com\nanother-example.net';
autoGlobalChk.checked = (data.autoGlobal || 'on') === 'on';
anchorPrefixChk.checked = (data.anchorPrefix || 'on') === 'on';
[rulesTA, prefixTA, selectorTA, urlListTA].forEach(autoResize);
}
);
const collapsibleHeaders = document.querySelectorAll('.collapsible-header');
collapsibleHeaders.forEach(header => {
header.addEventListener('click', () => {
const content = header.nextElementSibling;
const icon = header.querySelector('.toggle-icon');
if (content.style.display === 'block') {
content.style.display = 'none';
icon.textContent = '▼';
} else {
content.style.display = 'block';
icon.textContent = '▲';
const ta = content.querySelector('textarea');
if (ta) autoResize(ta);
}
});
});
function isRiskyRegex(pattern) {
if (pattern.length > 5000) return true;
if (/\((?:[^()]*?[+*][^()]*?)\)[+*]/.test(pattern)) return true; // (.*)+ / (.+)+
if (/(\.\*){2,}/.test(pattern)) return true; // .*.*.*
const lookAroundCount = (pattern.match(/\(\?=|\(\?!/g) || []).length;
if (lookAroundCount > 5) return true;
return false;
}
function validateRuleLines(rawText, { isPrefix = false }) {
const lines = rawText.split('\n');
const invalid = [];
const risky = [];
lines.forEach((line, idx) => {
const l = line.trim();
if (!l) return;
const parts = l.split('->');
if (parts.length !== 2) return; // 구조 안맞으면 content.js 에서도 skip
const left = parts[0].trim();
if (!left) return;
if (left.startsWith('/')) {
const m = left.match(/^\/(.+)\/([a-zA-Z]*)$/);
if (!m) {
invalid.push({ line: idx + 1, value: left, reason: '정규식 구문 오류 (구조)' });
return;
}
let pattern = m[1];
const flags = m[2] || '';
if (isPrefix && anchorPrefixChk.checked && !pattern.startsWith('^')) {
pattern = '^' + pattern;
}
try {
if (isRiskyRegex(pattern)) {
risky.push({ line: idx + 1, value: left, reason: '잠재적 성능 문제 (중첩/폭발 가능성)' });
return;
}
// autoGlobal 체크되어 있고 g 없으면 가상으로 g 추가해 테스트
let testFlags = flags;
if (autoGlobalChk.checked && !testFlags.includes('g')) testFlags += 'g';
// 실제 생성
// eslint-disable-next-line no-new
new RegExp(pattern, testFlags);
} catch (e) {
invalid.push({ line: idx + 1, value: left, reason: '정규식 생성 실패: ' + e.message });
}
}
});
return { invalid, risky };
}
function showWarnings(warnList) {
if (!warnList.length) {
warningBox.style.display = 'none';
warningBox.textContent = '';
return;
}
warningBox.style.display = 'block';
warningBox.textContent =
'경고:\n' +
warnList
.map(
w =>
`[${w.type}] ${w.line}행 "${w.value}" -> ${w.reason}`
)
.join('\n');
}
saveBtn.addEventListener('click', () => {
warningBox.style.display = 'none';
warningBox.textContent = '';
const enable = document.querySelector('input[name="enable"]:checked').value;
const titleTransform = document.querySelector('input[name="titleTransform"]:checked').value;
const loading = document.querySelector('input[name="loading"]:checked').value;
const masking = document.querySelector('input[name="masking"]:checked').value;
const listMode = document.querySelector('input[name="listMode"]:checked').value;
const rules = rulesTA.value;
const prefixRules = prefixTA.value;
const classoridRules = selectorTA.value;
const urlList = urlListTA.value;
const warn = [];
const v1 = validateRuleLines(rules, { isPrefix: false });
v1.invalid.forEach(i => warn.push({ ...i, type: 'INVALID' }));
v1.risky.forEach(i => warn.push({ ...i, type: 'RISKY' }));
const v2 = validateRuleLines(prefixRules, { isPrefix: true });
v2.invalid.forEach(i => warn.push({ ...i, type: 'INVALID' }));
v2.risky.forEach(i => warn.push({ ...i, type: 'RISKY' }));
// 경고가 있어도 저장은 진행하지만 사용자에게 알림
if (warn.length) {
showWarnings(warn);
}
chrome.storage.sync.set(
{
enable,
titleTransform,
loading,
masking,
listMode,
rules,
prefixRules,
classoridRules,
urlList,
autoGlobal: autoGlobalChk.checked ? 'on' : 'off',
anchorPrefix: anchorPrefixChk.checked ? 'on' : 'off'
},
() => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
if (tabs[0]?.id) {
chrome.tabs.reload(tabs[0].id, { bypassCache: true }, () => window.close());
} else {
window.close();
}
});
}
);
});
});