Is it possible to execute a JavaScript function loaded in a WebBrowser control at any moment in AutoHotkey? - webbrowser-control

I thought InvokeScript may work but it didn't.
strHTML =
(
<!DOCTYPE html>
<html>
<head>
<script> function displayDate() { document.getElementById("demo").innerHTML=Date(); }</script>
</head>
<body>
<p id="demo" onclick="displayDate()">This is a paragraph. Click here.</p>
</body>
</html>
)
new WebBrowser(strHTML)
Class WebBrowser
{
__New(strHTML) {
static WB
Gui, New, Resize
Gui, Add, ActiveX, vWB w780 h580 , Shell.Explorer
Gui, show, w800 h600
WB.Navigate("about:blank")
Loop
Sleep 10
Until (WB.readyState=4 && WB.document.readyState="complete" && !WB.busy)
WB.Document.Write(strHtml)
; WB.Document.Close()
WB.document.InvokeScript("displayDate")
; WB.document.parentWindow.document.InvokeScript("displayDate") ; does not work
return this
GuiClose:
ExitApp
}
}

There are at least two ways that you can call the function:
WB.document.parentWindow.execScript("displayDate()")
WB.document.parentWindow.displayDate()
All global functions within the script are available as methods of the window object.
The WebBrowser control referred in the article you linked to is part of the .NET Framework, and is not the same as the WebBrowser control you are using. This is why attempting to call InvokeScript raises an "Unknown name" error message.

Related

Send text to Text Field

How to send Text in text area field using geb & Spock?
Using just Selenium & the sendKeys I am able to send the text.
But unable to implement using geb spock.
this[field] = value - Not working
element.sendKeys(""); - Working
You probably want to look into the use of Geb Modules, particularly the Text Area module.
Taken from the documentation:
Given the html…
<html>
<body>
<textarea name="language"/>
</body>
</html>
It can be used this way…
def textarea = $(name: "language").module(Textarea)
textarea.text = "Groovy"
assert textarea.text == "Groovy"
<html>
<body>
<textarea name="language" id = "id1"/>
</body>
</html>
=====================================================
//content definition
textarea = { $(#id1") }
//updating value
textarea.value("your value")
OR
textarea << "your value"

Click button in WebBrowser control vb.net

I am trying to click the button highlighted in picture below. The code is from a web page:
I'm not to sure, but I believe the button is within an iFrame.
I have tried:
Dim wrapClick As HtmlElement = Contact.WebBrowser1.Document.GetElementById("Btn_WrapUp")
wrapClick.InvokeMember("Click")
And:
Dim elPoint As New Point(704, 340)
Dim wrapClick As HtmlElement = Contact.WebBrowser1.Document.GetElementFromPoint(elPoint)
wrapClick.InvokeMember("onClick")
And:
Contact.WebBrowser1.Document.GetElementById("Btn_WrapUp").InvokeMember("Click")
In all of the above, I have tried 'onClick' and 'Click'.
WebBrowser1 is on a different form.
Thanks!
Click button in WebBrowser control - working example:
Main file with frame - x.html:
<html>
<body>
<iframe width="400" height="300" src="y.html" id="frame1"> </iframe>
</body>
</html>
File placed in frame - y.html - with submit button:
<html>
<body>
<form action="...some_action...">
<input type="submit" id="btn"> Submit!
</form>
</body>
</html>
Button on VB form with OnClick event:
Dim Frame1 As HtmlWindow = WebBrowser1.Document.Window.Frames("frame1")
Frame1.Document.GetElementById("btn").InvokeMember("click")
VB2010Ex & .NET Framework 4 Client Profile.
I have tried code with action that load other site. Site was loaded in iframe.
Finally success :-)

IE 11. Why have I click and dblclick events after reload page if I double click a submit button

I have a simple html page and event handlers on submit button and form. Event handler function print to console what is event appers.
<html>
<head>
<script type="text/javascript" src="../js/jquery-2.1.1.js"></script>
</head>
<body>
<form id="formId" method="post">
<input id="inpSbm" name="inpSbm" type="submit" value="SubmitTest" class="lockDoubleClick"/>
</form>
</body>
<script>
$(document).ready(function () {
console.log("on ready");
$(".lockDoubleClick").each(function () {
var btn = $(this);
btn.click(function () {
console.log("click")
});
btn.dblclick(function () {
console.log("double click")
});
btn.closest('form').submit(function () {
console.log("submit form");
});
});
});
</script>
</html>
In IE11 when I double click on submit button I see the following in console:
click
submit form
on ready
click
double click
Why have I click and double click event after page is ready?
Because double-click is one of the most stupid ideas in the history of computing.
There are users who can double-click very quickly. There are others who can barely get in under the time limit that is the only difference between a double click and two singles. This means that when the computer sees the first click it must wait for a very long time to see if the user is going to click again. Users hate this; it makes the interface seem very slow.
The only solution is to have a 'double-click' actually hit the application as two clicks. They are identical except that the second one has a little flag that says it came in under the deadline for a double-click.
If against all reason you must accept both a double-click and a single-click on the same object you MUST be able to reverse the effect of the first click when the second user gets around to actually pushing the button for their very belated double-click.

pass form data to a new window

I'm trying to figure out how to pass form data collected from sql database to a new window. The idea is when the user click 'Rediger' (edit), that a new small window will open up with the current data and an input field for the user to change the data, and then hit the save button for the data to be written to the database. Then the window must close and the original page being updated with the new data. Is this possible? Can anyone please help me with this? Thank you.
Check out the page here: http://kristoff.it/onlinecoaching/coach/
Here is my dummy code:
<!DOCTYPE html>
<html>
<head>
<script>
function getValue()
  {
var txtfield=document.getElementById(this.id);
//alert(txtfield.innerHTML);
newWindow.document.write(txtfield.innerHTML);
return newWindow;
//windowsize(640, 480)
  }
</script>
</head>
<body>
<form method="POST" id="submitform">
<label id="name">John Smith</label><input type="button" id="button" onclick="getValue()" value="Edit">
<br>
<label id="title">Director</label><input type="button" id="button" onclick="getValue()" value="Edit">
</form>
</body>
</html>
You'll need some AJAX for this.
First follow this discussion, to be able to detect when the windows was closed.
Then make an ajaxRequest like this:
$.ajax({
url: "someUrlWhereYouCanFindTheUpdatedRecords.html",
cache: false
}).done(function( html ) {
//Some code that updates your webpage
});
Note that this code is using JQuery
Good luck!

JavaScript .innerHTMLworking only when called manually

I've got a very simple function, of replacing the innerHTML of a element. I've been trying to debug this for hours but simply can't, and it's infuriating.
When called from a button press the JavaScript (as follows) works well, but when called from another function it doesn't work. I am totally lost as to why this might be, and its a fairly core part of my app
// This loaded function in my actual code is a document listener
// checking for when Cordova is loaded which then calls the loaded function
loaded();
function loaded() {
alert("loaded");
changeText();
}
function changeText() {
alert("started");
document.getElementById('boldStuff').innerHTML = 'Fred Flinstone';
}
Button press and HTML to replace
<div id="main">
<input type='button' onclick='changeText()' value='Change Text'/>
<p>Change this text >> <b id='boldStuff'> THIS TEXT</b> </p>
</div>
It is also here in full on JSFiddle
You are already changed the innerHTML by calling the function loaded(); on onLoad.
Put this in an empty file and same as .html and open with browser and try. I have commented the function loaded();. Now it will be changed in onclick.
<div id="main">
<input type='button' onclick='changeText();' value='Change Text'/>
<p>Change this text >> <b id='boldStuff'> THIS TEXT</b> </p>
</div>
<script>
//loaded();
function loaded() {
alert("loaded");
changeText();
}
function changeText() {
alert("started");
document.getElementById('boldStuff').innerHTML = 'Fred Flinstone';
}
</script>
The problem here is, that the element you're trying to manipulate is not yet existing when you are calling the changeText() function.
To ensure that the code is only executed after the page has finished loading (and all elements are in place) you can use the onload handler on the body element like this:
<body onload="loaded();">
Additionally you should know, that it's very bad practice to manipulate values by using the innerHTML property. The correct way is to use DOM Manipulations, maybe this can help you.
You script loads before the element (boldStuff) is loaded,
Test Link - 1 - Put the js in a seperate file
Test Link - 2 - put the js at the very end, before closing the <body>