-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
114 lines (96 loc) · 4.05 KB
/
popup.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
document.addEventListener('DOMContentLoaded', function () {
const filterWordsTextarea = document.getElementById('filterWords');
const applyButton = document.getElementById('applyFilter');
const clearButton = document.getElementById('clearFilter');
const statusDiv = document.getElementById('status');
filterWordsTextarea.focus();
chrome.storage.local.get(['filterWords'], function (result) {
filterWordsTextarea.value = result.filterWords || '';
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
applyButton.click();
}
if (e.key === 'Delete' && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
clearButton.click();
}
});
applyButton.addEventListener('click', async function () {
const words = filterWordsTextarea.value
.split(/[\n\s]+/)
.map(word => word.trim())
.filter(word => word !== '');
await chrome.storage.local.set({
filterWords: filterWordsTextarea.value
});
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
try {
const response = await chrome.tabs.sendMessage(tab.id, {
action: "applyFilter",
config: {
selector: 'div[data-asin]',
words: words,
removeSelectors: ['.AdHolder', '[data-asin]:not([data-component-type="s-search-result"])']
}
});
const visibleCount = await chrome.tabs.sendMessage(tab.id, {
action: "getVisibleCount"
});
showStatus(`Filter applied successfully! (${visibleCount} items shown)`, 'success');
} catch (messageError) {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
await chrome.tabs.sendMessage(tab.id, {
action: "applyFilter",
config: {
selector: 'div[data-asin]',
words: words,
removeSelectors: ['[data-asin]:not([data-component-type="s-search-result"])']
}
});
const visibleCount = await chrome.tabs.sendMessage(tab.id, {
action: "getVisibleCount"
});
showStatus(`Filter applied successfully! (${visibleCount} items shown)`, 'success');
}
} catch (error) {
showStatus(`Error: ${error.message}. Please refresh the page and try again.`, 'error');
}
});
clearButton.addEventListener('click', async function () {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
try {
await chrome.tabs.sendMessage(tab.id, {
action: "clearFilter"
});
} catch (messageError) {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
await chrome.tabs.sendMessage(tab.id, {
action: "clearFilter"
});
}
showStatus('Filter cleared successfully!', 'success');
} catch (error) {
showStatus(`Error: ${error.message}. Please refresh the page and try again.`, 'error');
}
});
function showStatus(message, type) {
if (message.includes('items shown')) {
const [mainMessage, countPart] = message.split('(');
statusDiv.innerHTML = `${mainMessage}<strong>(${countPart}</strong>`;
} else {
statusDiv.textContent = message;
}
statusDiv.className = 'status ' + type;
statusDiv.style.display = 'block';
}
});