-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
57 lines (48 loc) · 1.74 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
document.addEventListener('DOMContentLoaded', () => {
const toggleButton = document.getElementById('toggleButton');
const urlInput = document.getElementById('urlInput');
const cleanButton = document.getElementById('cleanButton');
const result = document.getElementById('result');
// Initialize button state
chrome.storage.local.get('enabled', (data) => {
toggleButton.textContent = data.enabled ? 'Click to Disable' : 'Click to Enable';
});
toggleButton.addEventListener('click', () => {
chrome.runtime.sendMessage({
action: 'toggleEnabled'
}, (response) => {
toggleButton.textContent = response.enabled ? 'Click to Disable' : 'Click to Enable';
});
});
cleanButton.addEventListener('click', () => {
const urlInputValue = urlInput.value;
if (!urlInputValue) {
result.textContent = 'Please enter a URL.';
return;
}
const cleanedUrl = cleanUrl(urlInputValue);
if (cleanedUrl) {
urlInput.value = cleanedUrl;
result.textContent = 'URL is Cleaned';
} else {
result.textContent = 'Invalid URL';
}
});
function cleanUrl(url) {
try {
const urlObj = new URL(url);
// If it's a Google search URL, only retain the 'q' parameter
if (urlObj.hostname.includes("google.com") && urlObj.pathname === "/search") {
const searchParams = new URLSearchParams(urlObj.search);
const query = searchParams.get('q');
// Encode the query parameter with spaces replaced by '+'
const encodedQuery = encodeURIComponent(query).replace(/%20/g, '+');
return `${urlObj.origin}${urlObj.pathname}?q=${encodedQuery}`;
}
// For other URLs, return only origin and pathname
return `${urlObj.origin}${urlObj.pathname}`;
} catch (e) {
return null;
}
}
});