C# GeckoFX - Calling a JS function from the navigated Page - javascript

I am trying to call my function "initialize" in the navigated page using GeckFX(version 33),
I have tried the following actions:
_wb.Navigate("javascript:void(initialize());");
and
using (Gecko.AutoJSContext context = new AutoJSContext(_wb.Window.JSContext))
{
var result = context.EvaluateScript("initialize();", _wb.Window.DomWindow);
}
both didn't work.
the first one didn't even return an error, the second one returned the following error message:
Error HRESULT E_FAIL has been returned from a call to a COM component.
I am performing those actions in the "DocumentCompleted" event handler.
Is there something I am missing?
my guess is that it didn't finish loading the page as when I stop in debug mode I don't see the web page from within this even handler(only when i continue it appears)
any ideas how to make it work?
thanks.

I have found the issue,
I should have used:
Application.DoEvents();
and then call
_wb.Navigate("javascript:void(initialize());");

Related

Unable to use 'console.log' after redirecting it to 'alert'

I have implemented a JS module which I use from several Node.js scripts.
I would like to be able to use this module also from a browser, and I would like all messages to be displayed in an HTML alert box instead of in the terminal.
Ideally, I would like to achieve this without changing my JS module (for example, by replacing all occurrences of console.log with alert, or by passing the logging function when I initialize the module).
So I've figured I could simply set console.log = alert right before I use this module from the client-side code that I have.
For all it matters, I am actually doing it with Electron (cross-platform desktop applications using JavaScript), but I have also tested it on a browser, and the result is the same.
With console.log = alert, I successfully change console.log from function log() {[native code]} into into function alert() {[native code]}.
However, when I then attempt to call console.log("test"), I get an exception TypeError: Illegal invocation.
Here is a simple client-side code which reproduces this problem:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
try{
alert(`before:\n- alert = ${alert}\n- console.log = ${console.log}`);
console.log = alert;
alert(`after:\n- alert = ${alert}\n- console.log = ${console.log}`);
console.log("test");
}
catch (error) {
alert(error);
}
</script>
</body>
</html>
Why am I getting this problem, and how I may resolve it (ideally without changing my JS module)?
Is it possible that alert eventually calls console.log, which yields some sort of infinite recursion?
The value of this matters.
alert expects this to be window not console.
Thus:
console.log = window.alert.bind(window);

Meteor Accounts.onLogin infinite loop

In the Meteor forums I read that it is suggested to put Meteor.logoutOtherClients inside Accounts.onLogin(). Although this works, there is a problem to it, and that is the Accounts.onLogin() gets called multiple times when there are multiple TABS (not browsers) opened. Is this the expected output?
Here is my code below:
Accounts.onLogin(() => {
console.log('onLogin called')
Meteor.logoutOtherClients((error) => {
if (error) {
console.log(`error: ${error.error}`)
}
})
// Some Meteor Method calls here
alert('Welcome User!')
})
Another problem is that I got method calls in the same Accounts.onLogin() block and it gets called every time.
meteor#1.4.2.6
accounts-base#1.2.17
Question
How should I prevent this infinite calls from happening?
If I can't prevent this, where should I dispatch method calls when user logs in? Because obviously if I put it inside this code block it causes the dispatches to get called infinitely and that alert gets fired infinitely.
You can also see the details reported here: https://github.com/meteor/meteor/issues/8669
This is a confirmed bug #8669. So my workaround is I created a manual token for the user instead of using the default from accounts-base. I also handled the checking manually so basically getting rid of "magic" Meteor offers.

Javascript structured error handling

I am developing my first Javascript app and I am trying to go object oriented.
There is a basic closure that returns my primary object and every function I invoke rests in that object. Some pseudo code would look like this:
primary = (function(){
var object = {
doSomething = function(){};
},
return {intance:function(return object)}
});
//invocation
primary.instance().doSomething();
What I am trying to achieve is to attach an error handler function to my object, so that whenever there is an internal error, it is cought, and I don't have to wrap every function call in a try catch block.
I tried object.onerrorbut the error went on to window object. Maybe I am getting the concept wrong. I tried searching on Github for some simpler framework that includes structured error handling, but no luck. I am pretty familiar with this in PHP, but I haven't done this so far in Javascript. Can somebody show me an example how it is done right?
EDIT: I know that structured error handling goes further, I am just trying to get a root handler, so that no errors / exceptions can pass on to the window object
Dealing with the error event without a try catch block will halt the execution of your script (except for any asynchronous functions that have already been called).
You can suppress (non-ajax, non-syntax) errors by capturing them on document.body or a more specific object, and stop them being thrown to the user (or reaching the window object) by using e.preventDefault() or return false, and send them to a global/object handler (to inspect or log) by passing the event object as an argument - but any of those options will stop your script execution beyond the point of error. That's the main benefit of a try catch block, and as far as I know there is no way around that.

JavaScript Invalid Argument

I have a complex function that loops through the elements during the onSuccess: of my js and I'm getting the following error that I haven't seen before.
exception encountered : {"message": "Invalid argument.", "description": "Invalid argument.", "number": -2147024809, "name": "Error"}
The js function looks like this:
if(Object.isArray(list)){
list.each(function(listItem, index){
if(!Object.isUndefined(listItem.disabled)){
listItem.disabled = disableFlag;
}
});
}
that is called from the onSuccess: portion of an Update. My html is a button that is calling the noted function from an onclick. When I run it the onException: always happens and I'm getting the error by:
Object.toJSON(exception)
Has anyone seen this before? I have tried playing around with the functionality and it seems that when I use the button to do what it's supposed to do after a specific sequence of events is the only time this happens. So, I placed an arbitrary link on the page and wanted to see if I clicked that, what would happen and it updated the JSON object on the page and allowed for me to use the button for it's set action without the error. Any help would be appreciated.
Most of the time this kind of error happens in IE when you set an attribute of a DOM element.
If listItem is DOM element then maybe it's not yet added to the document or disableFlag is an invalid value. Or the error happens outside the provided code.

Error calling method on NPObject! in JavaScript

I've got some Visual C++ code (FireBreath) that wants to open a stream using PortAudio. After having done all the initial operations, I have this code:
err = Pa_OpenStream( &stream, &parametriIngresso, &parametriUscita, SAMPLE_RATE, FRAMES_PER_BUFFER, 0, My_Callback, &myData);
err = Pa_StartStream(stream);
while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
{
Pa_Sleep(1000);
}
err = Pa_CloseStream(stream);
This function is called from JavaScript, and after a certain number of seconds the plugin crashes giving me a Error calling method on NPObject! error on the line where it's called from in the JavaScript.
Error calling method on NPObject! is the error you get on most current browsers whenever anything goes wrong. You used to be able to send exception text from a NPAPI plugin (like a firebreath plugin) but all of the browsers have stopped passing this correctly recently.
Anyway, the upshot of this is that all that error message tells you for sure is that something went wrong in your plugin; if the plugin is actually crashing, the error message "Error calling method on NPObject!" has nothing to do with the actual crash, but rather just means "we were trying to call something on a plugin that crashed". Attach a debugger and find out what caused the crash and you will be closer to finding out what is actually happening.
This is a problem of thread.
Thread principal is busy for more time from plugin and since Javascript is single-threading, it crash.
The solution is create the new thread.

Categories

Resources