How to invoke a javascript function in web page by injected javascript

studio

#1

Anybody can help on this? I injected a segment of javascript and try to invoke a javascript function in web page, and failed. To simplify the issue, I made a sample html page as below:

<HTML><BODY>
<script type="text/javascript">
var myFunc = function(){
	alert("Function has been invoked");
}
</script>
<a href="#" id="myTestHref" onclick="myFunc();">Click Here!</a>
</BODY></HTML>

When I injected below js, it works as my expectation.
function(e){
document.getElementById(‘myTestHref’).click();
}

However, when I tried to directly invoke the function: myFunc() with below js, it failed and error message is: “Message: ReferenceError: myFunc is not defined”.
function(e){
myFunc();
}

I packaged sample and uploaded here:
Demo.zip (2.1 KB)


#2

This is awful way to write code, because of dirty global scope, but anyway, try to call window.myFunc()


#3

This smart and i think this is very nice and useful one.


#4

Thanks for your reply. Unfortunately, it does not work. Error message: “TypeError: window.myFunc is not a function”.
Anyway, I got a solution. In my actual practice, click a button on web page will invoke the js function. My difficulty is: the button’s element ID is dynamatically generated and uiPath can not capture it. As an alternative solution, with injectJS, I iterate all buttons and find out the correct one, then, invoke click() method.The injected JS likes:

function(e){
	var btnCtrls=document.getElementsByTagName('A');
	for(i=0;i<btnCtrls.length;i++){
		if(btnCtrls[i].text=='QUERY'){
			btnCtrls[i].click();
			break;
		}
	}
}

#5

Calling stopRefreshSite() from javascript console stops the timed refresh from occurring.

I’d like to disable this recurring refresh with a chrome extension. I thought that a content script which calls stopRefreshSite() whenever the page loads makes sense, though because content scripts execute in an isolated world this doesn’t seem to be possible.

Is there any way to call the webpage function? Or would trying to prevent the original javascript from running in the first place be a better strategy?