Block pop-ups while preserving normal button functionality
当前为
// ==UserScript==
// @name Block Pop-ups Only
// @namespace https://viayoo.com/
// @version 0.3
// @description Block pop-ups while preserving normal button functionality
// @author You
// @run-at document-start
// @match https://*/*
// @grant none
// @license MI
// ==/UserScript==
(function() {
// Block alert, confirm, and prompt pop-ups
window.alert = function() {
console.log("Alert pop-up blocked: ", arguments);
};
window.confirm = function() {
console.log("Confirm pop-up blocked: ", arguments);
return true; // Default to "OK"
};
window.prompt = function() {
console.log("Prompt pop-up blocked: ", arguments);
return null; // Default to "Cancel"
};
// Block window.open pop-ups
const originalWindowOpen = window.open;
window.open = function() {
console.log("Window.open pop-up blocked: ", arguments);
return null;
};
// Block unwanted behaviors without breaking buttons
document.addEventListener('DOMContentLoaded', function() {
// Prevent <a target="_blank"> from opening new windows
document.addEventListener('click', function(event) {
const target = event.target;
if (target.tagName === 'A' && target.target === '_blank') {
event.preventDefault();
console.log("Blocked new window from opening: ", target.href);
// Optionally, open the link in the same tab
window.location.href = target.href;
}
}, true);
// Prevent form submissions that trigger pop-ups
document.addEventListener('submit', function(event) {
const form = event.target;
console.log("Form submission intercepted: ", form);
// Allow the form to submit if it doesn't trigger a pop-up
if (!form.target || form.target !== '_blank') {
return; // Let the form submit normally
}
event.preventDefault();
console.log("Blocked form submission that would open a pop-up: ", form);
}, true);
});
console.log("Pop-up blocker script is active");
})();