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

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)

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

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

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;
		}
	}
}
1 Like

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?

@aceyang

Exactly the requirement is same like yours, I have to call a global function from injectjs activity, due to extension executing our js function in different context, it is not executing… did you find any solution?

check this thread How do i get js object from chrome browser?

Hi Guys,

I’m facing similar issue. We will need to filter grid in a table. That is performed by invoking java script function. It would be great if there is any solution for this.

Thanks!
Ashok

Hey what’s up,
You have managed to fix it, and if so, could you tell me how, that right now I am in something similar.
thanks