I need to rewrite a existing function which is created by the website when the page is loaded. The function is something like this:
function CheckStatus() {
var vcode = $.trim($("#insertCode").val()).toUpperCase();
var vreturn = encodeURIComponent(document.getElementById('text_return').value);
... (lots of other stuff)
}
And I would like to rewite this function to be like this:
function CheckStatus() {
return true;
}
If I paste this function in the chrome console, it rewrites with no problem. But when I try to do it with selenium It does not rewrite... I think it creates another function with the same name. My code in python using selenium webdriver is this:
driver.execute_script("function CheckStatus() { return true;}")
It does not return errors. Nothing happens actually.
Any clues how to solve this with selenium?
Thanks!
Solve it with something similar:
driver.execute_script("CheckStatus = function CheckStatus() { return true;}")
Thanks guys!
You're sending a function declaration. Nothing is executed here because you didn't ask to execute anything, you just defined a function. What you want looks more like this:
driver.execute_script("() => ({ return true;})()")
Don't forget to actually call you function at the end.
i'm not sure, but try to set the function to window object, maybe python's selenium has another name space
so:
driver.execute_script("window.CheckStatus = () => true;")
Related
I want to be able to tell if a function is noop. I was looking for a built in method such as angular.isNoop() but couldn't find anything. Is there anything that differentiates a noop?
A noop is simply a function that contains no operations. You can test for a specific noop function by using ===
For example;
console.log(x === angular.noop);
Will print true if x was assign the noop from Angular, but this will not work if x is using the noop from jQuery.
To check if a variable looks like a noop. You just need to see if the function string ends with {}. You can try something like this.
console.log(angular.isFunction(x) && /\{\}$/.test(x.toString()));
The above should work even in the code is minified.
A noop function has a name just like any other function. That name is noop. So you can check for it just by calling:
var theFunction = angular.noop;
theFunction.name === 'noop'
It is not a typical task, it is unlikely that the framework will have one.
function isNoop(fn) {
var trimRegex = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
try {
return !fn.toString().match(/{([\s\S]*)}$/)[1].replace(trimRegex, '');
} catch (e) { };
}
It does not check for noop statements within function, of course. The credits for trimRegex go to jQuery.
So, I want to run this script when button A is clicked. The script is stored in an Object as a string. When button A is clicked, I use eval, like: eval(Object[script]). This works fine, unless I have functions within the script, when I do, it breaks because they are not getting defined. Is there a way to get around this? I tried putting the function definition in a var and putting it at the top of the script. Now, if I simply copy my script to the console, it executes perfectly. Is there a way to execute a script as if it were typed into the console?
FYI: This is a simplification of my problem, I realize there are better ways to do what I describe here.
The best fix is to stop storing code as strings. Use functions instead.
buttonA.script = function() {
do whatever you were doing in your eval
};
// then, instead of `eval(buttonA['script'])`, say...
buttonA.script();
// or, if the name is in a variable...
var foo = 'script'; // for example
buttonA[foo]();
About the only time eval makes sense is when you have code that by its very nature has to be dynamically generated or interpreted. For the vast majority of cases, that is not true. I can only think of a case where it would be true, in fact: the textarea script testing thing mentioned in the comments.
For every other case...
obj = {
first: function() {
function test() { alert('hi'); }
test();
}
};
obj['first']();
// or simply
obj.first();
// and what's more...`test` doesn't escape and trample on stuff.
try { test(); }
catch (ex) { alert("" + ex); } says `test` is not defined
This works:
var oFunc = function (value) {
alert(value)
}
var obj = { code: "oFunc('hello')" }
eval(obj["code"]);
Or am I missing something?
Update
This also works
var obj = { code: "var oFunc = function (value) {alert(value)}; oFunc('hello')" }
eval(obj["code"]);
In your code alert(hi) should be alert("hi")
obj = {
first: 'function test() { alert("hi") } test();'
}
eval(obj["first"]);
DEMO.
I have a function that looks like this:
function outer() {
function inner_1() {
alert('inner_1');
}
function inner_2() {
alert('inner_2');
}
function inner_3() {
alert('inner_3');
}
inner_1();
inner_2();
inner_3();
}
I need to call outer(), but I want to replace inner_1() with another function.
I have tried this:
new_outer = outer;
new_outer.inner_1 = function() {
alert('my new inner function');
};
If I try to call the newly redefined inner_1 like this:
new_outer.inner_1();
it works as expected ('my new inner function' is alerted).
But if I try to call the outer function:
new_outer();
the old version of inner_1 is called.
I want to redefine inner_1 and the call outer. How can I achieve this?
This seems like a really bad idea so I am not going to post any code. However, if you are pursuing the answer for "educational purposes only", I will just hint that although you cannot easily redefine a function from outside its scope (as per your example), there is nothing stopping you from redefining a function attached to a function object.
I do think, however, there is a better solution to whatever the problem you are trying to solve is.
Wondering if there is an elegant way to listen for a function in JavaScript and/or jQuery.
Rather than listening for a $('#mything').click(function(){ //blah }) I'd like to listen for when a specific function is fired off. I don't want to edit the function as it's within a library that I don't want to hack directly.
I did find this: http://plugins.jquery.com/project/jqConnect which connects functions.
But wondering about a better technique.
The only way to do this is to override the function (ie, hack the library):
(function() {
var oldVersion = someLibrary.someFunction;
someLibrary.someFunction = function() {
// do some stuff
var result = oldVersion.apply(this, arguments);
// do some more stuff
return result;
};
})();
Edit: To run your code after the library function has run, just call the library function first, storing the result in a variable. Then, run your code, and finally return the previously stored result. I've updated my example above to accomodate running code either before or after the library function.
I am trying to implement following:
How can I start a flash video from javascript?
However I am unable to call method from Javascript. The trace message I wrote within AS file is not able to see while calling file within browser.
How can I test whether my JS function is calling AS method or not?
The FlashBug addon for Firefox lets you see Flash trace outputs in your browser.
here is the code which I am using:
import flash.external.*;
var flashFunction:String =
"jsstopMainVideo"; var
realFunction:Function = stopMainVideo;
function stopMainVideo(){
trace("called from javascript");
//flvPlayer.stop(); }
//stopMainVideo();
var wasSuccessful:Boolean =
ExternalInterface.addCallback(flashFunction,
null, realFunction);
In JS I am doing:
var me = null; function
getID( swfID ){
if(navigator.appName.indexOf("Microsoft")
!= -1){
me = window[swfID];
}else{
me = document[swfID];
} } getID("signupVideoVideo");
me.jsstopMainVideo();
I am getting JS error that function me.jsstopMainVideo() is not a function
The simplest method is to create a javascript function that only has an alert function inside.
try calling it, you either get the pop-up or you don't.
edit:
Alert is a javascript command, but you can call it directly from flash using external interface call.
as:
var call_java:uint;
call_java = ExternalInterface.call('alert','!!!');
or... call the alert from a function
AS:
var call_java:uint;
call_java = ExternalInterface.call('myFunction','!!!');
javascript:
funciton myFunction(val)
{
alert(val);
}
Just check this example http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html , maybe you forgot something