-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
195 lines (160 loc) · 6.59 KB
/
Copy pathpopup.js
File metadata and controls
195 lines (160 loc) · 6.59 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
// Popup JavaScript for the Chrome extension
document.addEventListener('DOMContentLoaded', async () => {
console.log('Popup loaded');
// Get DOM elements
const checkBlockingBtn = document.getElementById('check-blocking-btn');
const getPageInfoBtn = document.getElementById('get-page-info-btn');
const applyBlockingBtn = document.getElementById('apply-blocking-btn');
const disableBlockingBtn = document.getElementById('disable-blocking-btn');
const clearRuleBtn = document.getElementById('clear-rule-btn');
const blockingStatusSpan = document.getElementById('blocking-status');
const statusText = document.getElementById('status-text');
// Page info elements
const pageInfoDiv = document.getElementById('page-info');
const pageUrlSpan = document.getElementById('page-url');
const pageTitleSpan = document.getElementById('page-title');
const pageLinksSpan = document.getElementById('page-links');
const pageImagesSpan = document.getElementById('page-images');
// Load stored data
const data = await chrome.storage.sync.get(['clickCount', 'extensionEnabled']);
let extensionEnabled = data.extensionEnabled !== undefined ? data.extensionEnabled : true;
// Update blocking status on popup load
updateBlockingStatus();
checkBlockingBtn.addEventListener('click', async () => {
updateBlockingStatus();
});
applyBlockingBtn.addEventListener('click', async () => {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const currentTab = tabs[0];
if (!currentTab) {
statusText.textContent = 'No active tab found';
return;
}
// Send message to background to apply blocking
chrome.runtime.sendMessage({
action: 'applyBlocking',
domain: `${new URL(currentTab.url).hostname}`
});
statusText.textContent = `Applying JavaScript blocking for ${new URL(currentTab.url).hostname}...`;
// Update blocking status after a short delay
setTimeout(async () => {
await updateBlockingStatus();
statusText.textContent = extensionEnabled ? 'Extension is ready!' : 'Extension is disabled';
}, 1000);
});
clearRuleBtn.addEventListener('click', async () => {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const currentTab = tabs[0];
if (!currentTab) {
statusText.textContent = 'No active tab found';
return;
}
// Send message to background to clear rule
chrome.runtime.sendMessage({
action: 'clearRule',
domain: new URL(currentTab.url).hostname
});
statusText.textContent = `Clearing JavaScript rule for ${new URL(currentTab.url).hostname}...`;
// Update blocking status after a short delay
setTimeout(async () => {
await updateBlockingStatus();
statusText.textContent = extensionEnabled ? 'Extension is ready!' : 'Extension is disabled';
}, 1000);
});
disableBlockingBtn.addEventListener('click', async () => {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const currentTab = tabs[0];
if (!currentTab) {
statusText.textContent = 'No active tab found';
return;
}
console.log(`hostname: ${new URL(currentTab.url).hostname}`);
// Send message to background to disable blocking
chrome.runtime.sendMessage({
action: 'disableBlocking',
domain: new URL(currentTab.url).hostname
});
statusText.textContent = `Removing JavaScript blocking for ${new URL(currentTab.url).hostname}...`;
// Update blocking status after a short delay
setTimeout(async () => {
await updateBlockingStatus();
statusText.textContent = extensionEnabled ? 'Extension is ready!' : 'Extension is disabled';
}, 1000);
});
getPageInfoBtn.addEventListener('click', async () => {
try {
// Get current active tab
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const currentTab = tabs[0];
if (!currentTab) {
statusText.textContent = 'No active tab found';
return;
}
// Send message to content script to get page info
const response = await chrome.tabs.sendMessage(currentTab.id, {
action: 'getPageInfo'
});
// Update UI with page info
pageUrlSpan.textContent = response.url;
pageTitleSpan.textContent = response.title;
pageLinksSpan.textContent = response.links;
pageImagesSpan.textContent = response.images;
// Show the page info section
pageInfoDiv.style.display = 'block';
// Show feedback
statusText.textContent = 'Page info retrieved successfully!';
setTimeout(() => {
statusText.textContent = extensionEnabled ? 'Extension is ready!' : 'Extension is disabled';
}, 3000);
} catch (error) {
console.error('Error getting page info:', error);
statusText.textContent = 'Error: Could not get page info';
setTimeout(() => {
statusText.textContent = extensionEnabled ? 'Extension is ready!' : 'Extension is disabled';
}, 3000);
}
});
// Function to update blocking status display
async function updateBlockingStatus() {
try {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const currentTab = tabs[0];
if (!currentTab) {
blockingStatusSpan.textContent = 'No active tab found';
return;
}
const response = await chrome.runtime.sendMessage({
action: 'checkBlocking',
url: currentTab.url
});
if (response.blocked) {
blockingStatusSpan.textContent = `🚫 Blocked`;
blockingStatusSpan.style.color = '#dc2626';
applyBlockingBtn.textContent = 'Already Blocked';
applyBlockingBtn.disabled = true;
disableBlockingBtn.disabled = false;
} else {
blockingStatusSpan.textContent = `✅ Allowed`;
blockingStatusSpan.style.color = '#16a34a';
applyBlockingBtn.textContent = `Block`;
applyBlockingBtn.disabled = false;
disableBlockingBtn.disabled = true;
}
} catch (error) {
console.error('Error checking blocking status:', error);
blockingStatusSpan.textContent = 'Error';
blockingStatusSpan.style.color = '#dc2626';
}
}
// Test communication with background script
try {
const response = await chrome.runtime.sendMessage({ action: 'getStatus' });
console.log('Background script response:', response);
} catch (error) {
console.error('Error communicating with background script:', error);
}
});
// Handle any errors
window.addEventListener('error', (e) => {
console.error('Popup error:', e.error);
});