In my Python program, I need to run the hcaptchaCallback function (screenshot), but the function name has a different numbers each time. Can I somehow run this function by specifying only the first part of the name (hcaptchaCallback)?
driver.execute_script('hcaptchaCallback...()')
There is nothing that lets you really do that. You might be able to pull off looping over the globals and looking for a variable that starts with your string.
window[Object.keys(window).find(k=>k.startsWith("hcaptchaCallback"))]();
that will call it the way you showed, but looking at the function signature, looks like you are going to have to use call()
Related
I want to debug a website that is built from millions of lines of js code, and I want to find the function that will receive a specific value as one of the arguments. Is it possible to break the execution when any function receive a specific value as one of the arguments?
You could add a conditional breakpoint in
Chrome's debugger
Firefox' debugger
Alternatively, if the browser of your choice does not have similar functionality, you could invoke the debugger statement conditionally (if you can modify the code):
if(value === "wanted") debugger;
however that only works if you know the functions that might receive a specific parameter
Is it possible to break the execution when any function receive a specific value as one of the arguments?
No, that's close to impossible. Adding a breakpoint to "any function" will probably slow down execution a lot, if it's even possible. Doing so programmatically might work somehow, though it seems like a lot of work for little benefit. If you know the value, you probably also know the source and can debug from there.
Edit: apparently it's not clear, guess I'll make it more concise.
Node application is built, uses a native addon. I need to pass in a Javascript function from this project through node-addon-api into my C++ addon. Then, I need to be able to call that function from C++ multiple times. The issue arose when I found out I am unable to save the reference to the Javascript function due to the napi_env (required for calling the function) being protected from caching.
Could not find any clear answers or examples on the internet regarding how to do this, looking for tips.
Original:
as the title describes, I need to figure out a way to call a JS function multiple times in my addon. Generic use case is that my addon does some long running commands and needs to periodically push a status update back to the javascript.
I thought the best approach would be to have the user pass in a function (which just appends to a text block) for my addon to call (so it can write the updates), this way the javascript side can decide where it gets displayed.
I have experimented to get this working. Found out that my original way of saving the function in a persistent napi_value doesn't work since you cannot save napi_env as well.
I found this thread, which I think is the closest to what I need, but I can't manage to translate the Nan to napi_ so it would work with what I'm using. Callback NodeJS Javascript function from multithreaded C++ addon
Also attempted passing in an EventEmitter, but similar problem as above.
Can anyone give some pointers on if I am heading in the right direction? Perhaps help me dig up a few examples on how to accomplish this?
Your question is not clear. Assuming you are using Javascript in Node, have a look at FFI which allows one to loading and calling dynamic libraries using Javascript.
Alternatively one can just execute a function as follows from the command line:
/usr/bin/node yourjsfunctionfilehere.js
You can also pass command line parameters to the called JS function.
I made a nodeJS script that has a parameter.
At this state, i can pass arguments to npm start and it will fire up my script with it, and all works perfectly.
Now I have to pass multiple arguments to npm start and it will fire up as many scripts needed. ( one per argument )
But I really have no idea how to do that except to make the other script that accepts all arguments and launch etc... but I don't want this solution. So if some of you have the answer it will be nice!
Thanks.
Given that you're fine with the arguments being handled in a single process, the answer is simple. Whatever way your current program runs on a single argument, make that into a function and pass into it your multiple arguments, separately.
According to this answer, you can access the arguments array via process.argv.
So if your program used to do console.log(process.argv[0]), what you would do now is make your previous program into a function, function run(arg) { console.log(arg) }, and then run that function on each argument, process.argv.forEach(run)
To me, the following code seems reasonable enough:
$("#oneButton").click(
alert("hello");
);
It seems to say that when OneButton is clicked, please pop up an alert saying "hello".
However, in reality, the alert pops up regardless of whether the button is clicked or not.
One has to wrap alert("hello"); in an anonymous function, and THEN (and only then), the alert popping up will depend on clicking the button. To me this seems unnecessarily convoluted.
There must be a good reason why the designers of jQuery thought it acceptable for the alert in the code above to pop up even when the button hasn't been clicked. What is this reason?
Fair question I guess, although I'm not a fan of the arrogance that came with it :)
Lets break this down a bit:
object.method(function() {
alert('hi');
});
Your question is, why can't I skip the anonymous function?
What we're really doing here, is telling method to execute something at a later point. What's being executed is being supplied as a function.
We could simply give it a reference to a function instead!
object.method(alert);
Here's the problem, we've sent it a function, but now we can't send it any arguments. If we want to bring arument along to method, we must use ( and ).
As soon as those characters are included, the alert will instead get executed and the result of alert will get sent to method.
Now why can't this also be sent by reference? A very simple reason, you need some way to also pass the result of a function as an argument to another function, and the javascript engine cannot distinguish if your intent is to:
Send the result of a function as an argument to this other function, OR
Send a reference to the function with certain arguments to the other function.
Using ( and ) on a function means calling it immediately in almost every programming language, and javascript is no different.
There is a workaround:
object.method(alert.bind(this, "hi"));
Because .click() is a function and it may need a/some parameter/s to be used properly.
.click() alone will only trigger the event.
But .click(parameter) will do what's in the parameter after the event has been triggered. In this case parameter is a callback, i.e a function called after the main function finished.
But for the callback to be called, you will have to create a function.
Either by naming one:
function alertThis(){
alert('hello');
}
$("#oneButton").click(alertThis);
Or:
$("#oneButton").click(function(){
alert('hello');
});
The language could only provide a less verbose syntax for functions (look at coffeescript), but it's ok for library functions to execute immediately, so you can wrap them and pass code around when needed.
What do you suggest instead? I can only think of one alternative approach where primitive API return themselves a function, but that would lead to uglier code when you need to combine multiple primitives (even with direct syntax support by the language):
$(btn).onClick(alert().andThen(blink()).andThen(log()));
And also you would be forced to call
alert()()
When you need to display the dialog immediately.
The problem is that Javascript has no macros (with the meaning of Lisp) and the only way to provide "code" to a function is by passing a function/closure object.
click is just a regular method and accepts a parameter that is "code" to execute when the button is clicked. As for any parameter however the expression passed to click is evaluated when making the call and not later when user clicks the button.
To have click to work as you like the syntax should handle click differently than other function calls and this is what is allowed for example in Lisp by using macros instead of functions. Javascript has no macros and the syntax is fixed in the language: it doesn't have a click special form and you cannot create one.
The situation is not that terrible because Javascript syntax allows for inline anonymous functions so basically you just need to wrap your "code" parameters with function(){...} at the call site to get it working.
In Python for example things are a bit more annoying because only extremely simple functions can be specified inline as the lambda form can be used to specify anonymous inline functions but has serious limitations and doesn't allow any statement but just a single expression.
I'm currently experimenting on self replicating code. Out of love for the language I'd like to write it in javascript.
I'm working on a program that writes a function's code which in turn writes its function own code and so on. Basically, the desired process is this:
I manually create A function which returns code (which includes some randomness) and a numeric value (proposed solution to a problem).
I call this function a number of times, evaluate the results of each of those returned functions, and continue the process until I have code that is sufficiently good for what I'm trying to do.
Now, I have always been told how eval is evil, how never to use it and so on. However for my specific use case it seems like the Function constructor or eval are exactly what I'm looking for.
So, in short the question is:
Are eval/Function constructor indeed the best tools to use in my case? If so, I figured I'd use the Function constructor to scope the code executed, but is there a way to truly limit it from accessing the global scope? Also, what are some good practices for eval usage in my case?
I think I just figured out something I could use:
If I run my javascript code using node.js I can use the vm module which allows me to execute javascript code safely in a new context, and without letting the executed code have any access to the local or global scopes.
vm.runInNewContext compiles code, then runs it in sandbox and returns the result.
Running code does not have access to local scope. The object sandbox will be used as
the global object for code. sandbox and filename are optional, filename is only used in
stack traces.
You can see a full example here: vm.runInNewContext
This will allow me to eval code safely, and seems to be the safest way (I found) currently available. I think this is a much better solution than eval or calling the Function constructor.
Thank you everyone who helped.
Unfortunately I believe there is no way to prevent it from accessing the global scope. Suppose for example that in a web browser i evaled some code like this :
(function(window) {
eval(script);
)(null));
Any time the script tries to access window - it will get an error, since window is null. However someone who knew what they were doing could always do this :
var global = (function() {
return this;
}());
Since when you invoke a function in what Crockford calls the "function invocation style" then the this is always bound to the global variable.