问题
I am trying to remove an Alert box on an external site with Grease Monkey and jQuery.
HTML:
<!DOCTYPE html>
<html>
<body>
<script>alert("Remove this Alert box with GreaseMonkey.");</script>
<p>Hello world</p>
</body>
</html>
GreaseMonkey script (currently without the jQuery part):
// ==UserScript==
// @name Remove Alert box
// @include http://www.example.com/alert_remove/
// ==/UserScript==
var node = document.getElementsByTagName('alert');
node.parentNode.removeChild(node);
if(window.alert) {
alert("ALERT DETECTED"); // No response.
}
I don't think this can be solved using jQuery since the jQuery code only triggers when the page has loaded and the alert is visible ($(document).ready).
Can I remove the alert-element from the DOM?
Can I send a keypress (13=Enter) to handle the alert if it shows up?
Thanks.
回答1:
Since Greasemonkey is loaded after the page and the script isn't triggered by any event, I think it's impossible to achieve what you are looking for.
回答2:
Greasemonkey doesn't run until the page is loaded, so this is not possible with Greasemonkey. If you are really determined, you could set up a proxy (such as squid) with a method similar to what is used in this Upside-Down-Ternet trick.
If you can get your own JavaScript to run before the other code calls alert
, then you would want to do something like this:
window.backupAlert = window.alert;
window.alert = function () {return true};
(tested working)
You can then use the backupAlert
function in place of alert
in your own scripts, but other scripts that assume alert
still exists will not show a popup.
The reasoning for this is that alert is a function inside the window object; it is not a tag or DOM object in the document.
And no, you cannot send a keypress with JavaScript. This is globally true; you can trigger the functions that handle key press events, but you can't send OS click or keypress events.
回答3:
var node = document.getElementsByTagName('alert');
node.parentNode.removeChild(node);
- it won't work at all, cause "alert" is not a node, but "script" is. If you managed to execute your GreaseMonkey script before alert was shown, you have to do this instead:
var node = document.getElementsByTagName('script')[0];
var str = node.firstChild.textContent;
var result = str.replace(/alert("[\w\s.]*");/, "");
node.firstChild.textContent = result;
- tested in FireFox. What is going on there - you look for first script tag (can be easy done to look all of them), then you take its content (code), then you replace alert with empty string and put modifide code back. In your case result page will still heve script tag, but without any code;
hope it helps
回答4:
I'm not sure of doing it with javascript, since js runs on a rendered page already. My first thoughts would be to catch it before the page renders. Like with PHP. Read the contents of the external site into a string and the strip out the ALERT lines of code, then display the page.
来源:https://stackoverflow.com/questions/2374797/remove-alert-box-before-it-pops-up