Override existing Javascript function in a webpage - java

I display a webpage (not mine) in a webview. There is a Textarea, in which I need to put some text.
<TEXTAREA NAME="app1" ROWS="3" style="width:95%" TABINDEX="1"
onKeyDown="javascript:return gerTouch(event);"
onKeyUp="javascript:verif(app1,300,true);"
onBlur="javascript:effacer();">TEXT TO CHANGE</TEXTAREA>
Is it possible, with addJavascriptInterface, to rewrite the existing onKeyDown (or another one) function, in order to put my text in the desired place ?
Something like :
webview1.addJavascriptInterface(new Object()
{
public void verif(String strl,int i,boolean b)
{
//Some code ?
}
}, "ok");
The textarea's name changes at each loading, and I cannot access the html textarea with another way.
Any other suggestion would be welcome :-) (my goal: put a value in a webview's textarea : Put text inside a textarea in a webview)

You can change the verif function by running something like after the webview has loaded (call it inside the onPageFinished() of your WebViewClient) :
webview.loadUrl("javascript:window.verif = function(strl,i,b){/*some code*/}");

Related

Check button is selected using selenium

I have HTML like this:
<input data-errorqtip="" aria-invalid="false" id="checkbox-1371-inputEl" class="x-form-field x-form-checkbox x-form-cb" autocomplete="off" hidefocus="true" type="button">
In UI it is showing like checkbox. How to check if it is selected?
I am using bellow code but is not working.
if (!field.isSelected()) {
field.click();
}
if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
driver.findElement(By.id("idOfTheElement")).click();
}
Looks to me like you are using EXTjs. And while it renders as a checkbox, EXT does not set the input element value to selected so selenium won't know about it.
You will need to use selenium's ExecuteScript method to run javascript against EXT to determine if the item is selected or not.
I'd dig up the code, but I use EXT, Selenium, and C# and I have a feeling the syntax is going to be a bit different between the two, but the above is the general steps you will need to go through to get the result you are looking for.

With the click of an HTML button, how to INVOKE a java method AND REDIRECT to another HTML page?

I have a button through which i INVOKE a java method :
<h:commandButton value="My Schedule" action="#{empDutySchedBean.viewMyTask}" rendered="#{welcomeBean.workSchedule}" class="mainLinks"/>
now through this method viewMyTask() i get a List of values returned from the database.
public List viewMyTask()
{
setEmpID((String) session.getAttribute("userName"));
setEmpDuty(ts.getMyTask(getEmpID()));
return empDuty;
}
From here i want tht with the click of a button, the user is redirected to another page and the List data shows up.
I dont understand how to REDIRECT to another HTML page and display the LIST data.
NOTE: --> this button is basically a menu on my webpage. For the other menus i have used an anchor tag. However as you cannot invoke a method using anchor tag, i have used button tag. But because of this, I am not being able to redirect
Some Ways that i think it can be solved are:
use anchor tag instead on button to redirect to the other page AND then use onLoad method of javascript to INVOKE java method
invoke another method after viewMyTask() to perform the redirection.
I dont kw how these methods would work though.
First off, I assume you already know how to return the data Java side, in which case this is really just a javascript question. To do simple post-gets, I usually use Ajax, like so:
new Ajax.Request(myUrlToCall, {
method:'POST',
onComplete: function(transport) {
window.location.replace(theRedirectUrl);
}
});
This will call whatever url you wanted to call Java side, then on the completion of this call, will redirect you to whatever url you want to go to. If you need to send some parameters to the java side, all you need to add is:
parameters:myParamVar,
after the method:'POST' line, and if you want your java side to return the url, then all you'd need to do is send it as json or similar in the response, then do:
window.location.replace(transport.responseJSON["theRedirectUrl"])
Hope that helped!

GWT History - Why does it change the focus of my screen?

I have a simple application structure that will contain three composites at any one time - my header, footer and content composites. The composites are laid out in the follow manner.
<body>
<div id="header">
<div id="content">
<div id="footer">
</body>
The composites are assigned to the three <div>'s in my EntryPoint. The EntryPoint also contains a History Handler as follows.
History.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
if(historyToken.isEmpty()) {
parseTokens();
ContentContainer.INSTANCE.setContent(new IndexScreen());
}
else if(historyToken.equalsIgnoreCase("registration")) {
ContentContainer.INSTANCE.setContent(new RegisterScreen());
}
else if(historyToken.equalsIgnoreCase("login")) {
ContentContainer.INSTANCE.setContent(new LoginScreen());
}
}
});
History.fireCurrentHistoryState();
In my Composites, I have several ClickEvent handlers that look similar to the following.
#UiHandler("registerLink")
public void handleClick(ClickEvent event) {
ContentContainer.INSTANCE.setContent(new RegisterScreen());
History.newItem("registration", true);
}
This registers the history very nicely. It also brings me to the appropriate pages when I click the back button. However, it has the very odd effect of focusing the browser on the content composite, with the effect of the browser being scrolled to the bottom. It's not a deal breaker but it kind of breaks the user experience a little. (Do let me know if the explanation isn't clear, I'll add images)
What exactly is causing this and how can I fix it?
GWT history tokens work with the hash # token. In html this was originally intended to use the browser to focus on a specific part of the page, if an element has a name or id with the string after the # token. Could it be you have an id matching your history token?
Also in you handleClick you call setContent. But if I'm correct the call after it triggers a change event and will end up in the onValueChange, which also calls the setContent. So it looks like you are calling setContent twice here.

Modal Window or confirm box in Form.onSubmit

In Form.onSubmit can I add a SimpleAttributeModifier to that form? Actually I have a situation where the onSubmit method of the form look like this:
#Override
public void onSubmit() {
//some code
if (some_condition) {
//here I want to show Javascript confirm box or wicket modal window
//but I can not get any AjaxRequestTarget here to show that modal
add(new SimpleAttributeModifier("onSubmit", "return confirm('confirm msg')"));
}
//some code
}
It is not working. Is there any way to achieve this? Thank you.
What you're trying will definitely not work, as the onSubmit() method is not called in time. It is a server-side method called when the form is submitted. In order to show a confirm dialogue, you want some javascript executed on the client before the form is sent back from the client to the server.
You need the attachment of javascript to happen when the html is rendered, so that it's there to be called when the user clicks the submit button.
It can of course be done. I don't have my own code for it handy, but I think this example should get you pointed in the right direction.

GWT adding Event Handler overrides the native one?

lets say i have the following html element on my host page:
<input type="text" onfocus="this.value = ''" id="textField"/>
In gwt i wrap this into an TextBox Widget like this:
final TextBox myTextBox = TextBox.wrap(DOM.getElementById("textField"));
If i now want to add a focus handler what will happen?
myTextBox.addFocusHandler(new FocusHandler() {
public void onFocus(final FocusEvent event) {
// do something
}
});
What i want to achieve is that first the javascript which was defined on the host page should be executed and after that my focus handler should be executed. But what actually is happening is that the javascript from the hostpage is not being executed and only the code within onFocus is executed. Is this behavoir normal? I would expect that add means actually add and not override. Is there a way to work arround this? Or do i maybe have any flaws which induce this behavoir. Any help is appreceated.
kuku

Categories