JavaScript Injection to Disable the Alert Function - javascript

Salutations,
I am working on a project that requires that I control a JavaScript-heavy website from an application created in Visual Basic.
I have been fairly successful with using webBrowser.navigate("javascript:blahblahblah") to login and change values on the website's controls. This would be fine, however the site uses an alert window after I do the injection to log in. Users will not want to see that. I can change the opacity and visibility for the vb frame, but the javascript alert window cannot be hidden using the properties of the frame and the webBrowser control.
So, I'm looking for a way to override the alert function via JavaScript injection (or some other way if possible). I've tried:
webBrowser.Navigate("javascript:function alert() {};")
webBrowser.Navigate("javascript:window.alert = function() { };")
webBrowser.Navigate("javascript:alert = function{};")
And the alert window still appears.
Any ideas on how to stop the alert windows from appearing?
Thanks.

Instead of webBrowswer.Navigatetry webBrowser.Document.InvokeScript. Reference

Related

How to prevent document from creating new popup windows onclick event of user using javascript

In my wordpress site, some hacker has embedded a script into it somewhere.
The problem I'm having now is, whenever the site is opened, the first time a user clicks anywhere on it, a spam popup appears, due to the hacker's script.
Is there any way I can prevent the popup window from being created with Javascript?
If I can't prevent it, is there any way that I can close the popup using JS? Unfortunately, since the other script is creating it, I don't have a reference to the opened window. I've seen other scripts calling .close() on an opened window, but I don't have a reference to the new window, so I don't know how to close it.
Here's my site.
The popup on your site is opened by something calling window.open. You can prevent it by assigning something else to window.open and running your script before the other script does:
window.open = () => undefined;
(This does work, if I run this code on your site before your site's Javascript runs, no windows get opened)
It's not uncommon for a client to want to implement this, but if you're the server, it'd be better to fix the server to remove the malicious code, rather than try to patch around it.

HTML in a window

I want to make a window with HTML that works similar to ones opened by windows. I know the method with actual browsers, but it isn't good enough as I have link and navigation buttons.
This would make my job easier in making softwares with lots of animations
The best you can get is calling Window.open with third argument set as: 'menubar=no,location=no,resizable=no,scrollbars=no,status=no'.
This will open a new browser window with only the address bar shown.
There is no way to open a native window out of the browser's scope from JavaScript code other than this. It is a security limitation.
However, the other alternatives include are Window.alert or a Window.prompt.
If you want to open a popup box you can use alert("This is a dialog box!");, confirm("Is it ok or not?"); or prompt("Enter a value and confirm it or not");.
https://www.w3schools.com/js/js_popup.asp
If you want to open a native window, that is not possible as it has already been said.

GeckoFX: Script Taking Too Long dialog

I have an application set up to perform some automation using GeckoFX web browser. My application is nicely setup, everything is working fine.
The problem that I'm running into is that when Gecko loads a webpage into itself, it sometimes doesn't fire the DocumentComplete event for a long time, instead gives an error dialog box that The script is taking too long, with two buttons Continue or Stop the script (although this happens rarely, but does happen).
I can't figure out how to suppress this error/warning dialog. In case the web browser is having a script error, I want it to stop processing and fire the DocumentComplete event anyway, I have coded my application in a manner to handle partially loaded webpages. But I can't reach to this point since the application gets stuck up at the error dialog.
Any suggestions on how to suppress scripting errors like this in GeckoFX? The IE Web Browser control had a property called SuppressErrors (or something).
Thanks in advance!
There exists an about:config setting dom.max_script_run_time which controls how long a script can run before this error appears. Setting this to 0 will disable the dialog entirely.
This can be done programatically in GeckoFX:
Gecko.GeckoPreferences.User("dom.max_script_run_time") = 0;
Via: https://support.mozilla.org/en-US/kb/warning-unresponsive-script

how to call or mimic a javascript function inside a webview

I am making a hybrid app. I am using xamarin for android, pretty much the same as android. I have already figured out how to hook the phone's back button press. When pressed I want my app code to either mimic or call a javascript function that is part of the webpage that the webview is displaying. It is my web page, so I know the code that the webview is rendering. To be specific, I want to call a jquery slideToggle function on a page element when the phone's back button is pressed. Can that be done, and if so what would be the best approach? I'm hoping that someone here has had to do something just like this in the past. Thanks.
I don't think webview supports JQuery, unless maybe you reference it.
The Xamarin android way of doing it is:
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");
Source : Android.Webkit.WebView.AddJavascriptInterface Method
Also make sure you have enabled Javascript,
web_view.Settings.JavaScriptEnabled = true;
You can show or hide the element by finiding it by id (document.FindElementById). Then you can set its display to block or none. Thus you can do it purely in Javascript.
Add on :
There are couple of problems with your method. To name a few -
on pressing of back button user expects a particular behavior and it should not be altered. This would not give a rich user experience.
if you are override ing the back button press then what about the navigation bar menu click.
JavaScript way of doing can cause security issues.

Popup a message upon exiting a webpage

We'd like to have a message popup when a visitor to specific webpages leave those webpages. So we could tie some Javascript to the links on those webpages, but then we can't control if the user exited the webpage by typing in a URL, using a bookmark or just closing the window...
I assume we have limited options if the user tries closing the browser window... but I do know it's possible because Google Docs' Documents offers the chance to cancel closing the window if you have unsaved work while closing the browser.
What are my options? Can I have Javascript called upon going to another webpage? Can I control the text in the popup when trying to close the window?
I'm using jQuery, so if there are good solutions implemented with jQuery that's perfectly fine.
Yes.
https://developer.mozilla.org/en/DOM/window.onbeforeunload
jQuery UI Dialog OnBeforeUnload
There is onunload event you can bind to, first example:
http://www.codetoad.com/javascript/miscellaneous/onunload_event.asp

Categories