Changes Between the Old Script and the Updated Script
Overview
The updated script includes several changes and improvements over the old version. These updates enhance the functionality, especially concerning how popups are handled, and introduce a mechanism for managing a whitelist and blacklist of websites. Below are the detailed changes:
1. Introduction of Blacklist and Whitelist
Old Script
- Only a whitelist was used to manage allowed websites.
- Whitelisted websites were pre-defined and stored locally.
- Users were prompted to add a website to the whitelist if it was not already on the list.
Updated Script
- Introduces both a blacklist and a whitelist.
- Blacklist: Websites on this list will have their popups blocked.
- Whitelist: Websites on this list are allowed to show popups, and it takes precedence over the blacklist.
- Prompts the user to add websites to the blacklist when a popup is blocked.
- If the user selects "Cancel," the website is added to the whitelist.
2. Persistent Storage with GM_getValue
and GM_setValue
Old Script
- Whitelist was managed using
localStorage
.
Updated Script
- Uses Tampermonkey's
GM_getValue
and GM_setValue
for persistent storage of both the blacklist and whitelist.
3. Handling Subdomains
Old Script
- Whitelisted websites were managed as-is without specific handling for subdomains.
Updated Script
- When a website is added to the blacklist or whitelist, subdomains are also considered.
- On removing a website from the lists, all subdomains are also removed.
4. User Prompts
Old Script
- When a popup was blocked, users were prompted to add the site to the whitelist.
Updated Script
- When a popup is blocked, users are prompted to add the site to the blacklist.
- If the user selects "Cancel," the site is added to the whitelist instead.
5. Improved Event Handling
Old Script
- Basic event listeners for click events were in place to manage popups.
Updated Script
- Listens for user-initiated actions (click, submit, keydown) to determine if a popup should be allowed.
- Handles
Alt+0
keypress to remove the current website and its subdomains from both the blacklist and whitelist.
6. Verbose Logging
Old Script
- Logging was implemented for various actions.
Updated Script
- Maintains and extends verbose logging for better debugging and tracking of actions.
Detailed Code Changes
Persistent Storage Changes
function getBlacklistedWebsites() {
try {
return new Set(JSON.parse(GM_getValue('blacklistedWebsites', '[]')));
} catch (e) {
log('Error parsing blacklist from storage: ' + e);
return new Set();
}
}
function getWhitelistedWebsites() {
try {
return new Set(JSON.parse(GM_getValue('whitelistedWebsites', '[]')));
} catch (e) {
log('Error parsing whitelist from storage: ' + e);
return new Set();
}
}
function saveBlacklistedWebsites(blacklistedWebsites) {
GM_setValue('blacklistedWebsites', JSON.stringify([...blacklistedWebsites]));
log('Blacklist saved');
}
function saveWhitelistedWebsites(whitelistedWebsites) {
GM_setValue('whitelistedWebsites', JSON.stringify([...whitelistedWebsites]));
log('Whitelist saved');
}
Prompt Changes
function blockPopup(url) {
try {
const parsedUrl = new URL(url, location.origin);
const hostname = parsedUrl.hostname;
log('Blocked popup: ' + url);
if (!blacklistedWebsites.has(hostname) && !whitelistedWebsites.has(hostname) && !promptedWebsites.has(hostname)) {
promptedWebsites.add(hostname);
setTimeout(() => {
if (confirm(`A popup from ${hostname} was blocked. Do you want to block pop-ups from this site in the future?`)) {
addToBlacklist(hostname);
location.reload();
} else {
addToWhitelist(hostname);
location.reload();
}
}, 0);
}
} catch (e) {
log('Error blocking popup: ' + e);
}
}
Event Handling for Removing Sites from Lists
document.addEventListener('keydown', (event) => {
if (event.altKey && event.key === '0') {
const hostname = location.hostname;
let { foundInBlacklist, foundInWhitelist } = removeFromLists(hostname);
if (foundInBlacklist || foundInWhitelist) {
alert(`${hostname} and its subdomains have been removed from the ${foundInBlacklist ? 'blacklist' : ''} ${foundInWhitelist ? 'whitelist' : ''}.`);
} else {
let subdomainFound = false;
blacklistedWebsites.forEach(site => {
if (hostname.endsWith(site) || site.endsWith(hostname)) {
blacklistedWebsites.delete(site);
subdomainFound = true;
}
});
whitelistedWebsites.forEach(site => {
if (hostname.endsWith(site) || site.endsWith(hostname)) {
whitelistedWebsites.delete(site);
subdomainFound = true;
}
});
saveBlacklistedWebsites(blacklistedWebsites);
saveWhitelistedWebsites(whitelistedWebsites);
if (subdomainFound) {
alert(`Subdomains of ${hostname} were found and removed.`);
} else {
alert(`${hostname} was not found in the blacklist or whitelist, and no subdomains were found.`);
}
}
}
});
Summary
The updated script provides a more robust mechanism for managing popups by introducing a blacklist and a whitelist, improving persistent storage, and handling subdomains more effectively. Additionally, it refines the user prompts and event handling to provide a smoother user experience.