Disable spell check in SWT Browser widget - java

I'm working on an Eclipse based application in which we use a Browser widget.
When typing text in this widget, a spell check is executed. We would like to deactivate it.
Is it possible ?

If you have control over the page that is displayed, you can simply disable spell checking by setting the appropriate attribute(s) (see
Disable spell-checking on HTML textfields). But I think you wouldn't have asked if that was the case...
If however the page is loaded from elsewhere you need to let the browser widget execute Javascript code that disables spell checking after the page is fully loaded.
The snippet below uses a ProgressListener to that is called when the page has been loaded completely. The Javascript that then gets executed is off the top of my head and may need refinement.
Browser browser = new Browser( parent, SWT.NONE );
browser.addProgressListener( new ProgressAdapter() {
public void completed( ProgressEvent event ) {
browser.execute( "document.getElementsByTagName( 'html' )[ 0 ].spellcheck = false;" );
}
} );
browser.setUrl( ... );

Related

Add keyListener to simulate a mouse click on a link, page is made with Javax jspx servlets. getElementById returns null

I´m trying to add hotkeys to the web application we use at work. The solution was to apply a Greasemonkey script, but the web system uses Liferay Portal, which is made with JavaX and jspx server applets.
I DON´T KNOW HOW TO APPLY "WAITFORKEYELEMENTS" answer, my knowledge is not that advanced.
What is the problem? I need to search the label for a link, i.e. “case file history” , add a keylistener event and simulate a mouse click. Here´s an example link:
<a id="form1:commandLink1211" name="form1:commandLink1211"
onclick="submitForm('form1',1,{source:'form1:commandLink1211'});
return false;"
class="linkMenu" href="#">
Look case file history
</a>
I need it to simulate a mouse click into this link, but the getElementById returns null. The page loads “empty” and then loads the jspx module. I know greasemonkey puts the script first. I tried to use
window.onload=funcion
document.ready() //didn´t know how to add this to my code
Other solutions include using a timer, but I don´t know how to apply it.
Current greasemonkey script:
(function(){
document.addEventListener('keydown', function(e) {
// pressed alt+g
if (e.keyCode == 71 && !e.shiftKey && !e.ctrlKey && e.altKey && !e.metaKey)
{
console.log ("key pressed");
document.getElementById('form1:commandLink1211').click();
}
}, false);
})();
The default behaviour of GreaseMonkey and similar alternatives is to run userscripts after the DOM (document-object-model) has been loaded, i.e. as soon the HTML has been parsed but before included resources have finished to load.
Some Scripts need to do stuff before the DOM has loaded. To achieve this they use the meta block entry #run-atdocument-start. This gives you the chance to intercept anything the site is doing while loading. However, with this entry your scripts do not know anything about the document when they start running. (Note the limited support in GM 4.0)
If you do not need to do stuff before the site has been loaded, use the meta entry #run-at document-end or omit this entry at all.
The more accurate approach is to implement an event listener. Modern browsers support the DOMContentLoaded event, formerly known as "domready" event in some JS-frameworks.
document.addEventListener('DOMContentLoaded', evt =>
{
// Do DOM-based stuff here...
document.addEventListener('keydown', evt =>
{
document.getElementById('form1:commandLink1211').click();
});
});
Using JQuery the shorthand $(evt => {/* DOM ready stuff */} ); will also work.
The element was not loaded yet when the Javascript ran, so the element with the given id was not found. Try to wrap a function around your code:
function loaded(){
document.addEventListener('keydown', function(e) {
// pressed alt+g
if (e.keyCode == 71 && !e.shiftKey && !e.ctrlKey && e.altKey && !e.metaKey)
{
console.log ("key pressed");
document.getElementById('form1:commandLink1211').click();
}
}, false);
}
and make sure you have
onload="loaded()"
in the body tag.
Instead of document.getElementById('form1:commandLink1211').click(); can yo instead do submitForm('form1',1,{source:'form1:commandLink1211'});. It should do the same thing, right?

GWT Hyperlink strange behavior - redirecting to http://localhost:8080/#

Okay, this is very strange.
All I am having is a Hyperlink in my menu:
Hyperlink eventCalendar= new Hyperlink("Eventkalender", "eventCalendar=" + store.getId());
and I am listening to the ValueChangeEvent in the MainViewPresenter. Please notice that I am not doing anything. Right before the creation of the listener I am setting a SimplePanel to be the display for the ActivityManager:
App.activityManager.setDisplay(this.mainView.getMainContentContainer());
History.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
GWT.log("onValueChange() historyToken " + historyToken);
}
});
But if I click the link what happens is this:
First, for the blink of an eye I can see the browser URL change to
http://localhost:8080/#eventCalendar=1
but it changes immediately back to
http://localhost:8080/#
which causes my landing page to get loaded inside the SimplePanel which I declared as display (see above).
Does anybody have an idea what could cause this behavior because this does not make sense to me? Why does the URL get changed again? Am I using History or Hyperlink wrong?
Most probably your PlaceHistoryMapper returns null for the eventCalendar=1 token, so it's replaced with the default place you gave to the PlaceHistoryHandler. If you're using GWT.create() based on PlaceTokenizers, with a factory and/or #WithTokenizers, that means either you don't have a PlaceTokenizer for the empty-string prefix (#Prefix("")), or that one tokenizer returns null.
That being said, you probably should rather try to use places directly rather than going through the history. That means using a ClickHandler on some widget and calling PlaceController#goTo with the appropriate place. Ideally, that widget would be an Anchor whose href is computed from the result of getToken from your PlaceHistoryMapper with the given place (how the href actually looks depends on your Historian; if you stick to the default behavior, then just prepend a # to the returned token).

Hard Refresh in GWT

Using Google Web Toolkit, I'd like to code the equivalent of a hard refresh (control + F5).
I don't believe (or know) if GWT's Window.Location will work.
import com.google.gwt.user.client.Window.Location;
Window.Location = currentPage; // I don't think it'll be hard refresh
For reloading the current page you need to call Window.Location.reload() method.
Reloads the current browser window. All GWT state will be lost.
Or you can even specify your own JSNI (below how todo), because by default force reload is false :
public static native void forceReload() /*-{
$wnd.location.reload(true);
}-*/;
According to https://developer.mozilla.org/en-US/docs/DOM/window.location#Methods you would need to call window.location.reload(true) to force the reload of the current page.
Unfortunately GWT wraps only the window.location.reload() via Window.Location.reload(), and it is up to the browser to retrieve the page from the cache or from another get. This is done to achieve the most cross-browser solution.
Never tried but you should be able to use the following.
public static native void reload(boolean force) /*-{
$wnd.location.reload(force);
}-*/;
For reload gwt page, you have two options:
1) Window.Location.reload();
Reloads the current browser window. All GWT state will be lost.
2) Window.Location.replace("newurl");
Replaces the current URL with a new one. All GWT state will be lost. In the browser's history, the current URL will be replaced by the new URL.

How can I detect a close in the browser tab?

Hello I am looking for information on the close tab (not browser) event if there is one in java for a applet. I am wondering if there is an event for that or a way to check a way to check for that. I would like to just capture the event and make a little popup box , stating Your session will expire or something along those lines. Is that possible at all or to a point with java or Javascript?
UPDATE: okay with the information you guys pointed me into i was able to get information on a simple enough javascript. Now it is working fine in IE , Chrome and Firefox but for some reason Safari 5.1.7 isn't liking the code. Not sure why. Here is the code if it helps.
jQuery(function() {
var hiddenBtn = document.getElementById("javaform:browserCloseSubmit");
try{
opera.setOverrideHistoryNavigationMode('compatible');
history.navigationMode = 'compatible';
}catch(e){}
//Sends the information to the javaBean.java file.
function ReturnMessage()
{
return hiddenBtn.click();
}
//UnBind Function
function UnBindWindow()
{
jQuery(window).unbind('beforeunload', ReturnMessage);
}
//Bind Exit Message Dialogue
jQuery(window).bind('beforeunload', ReturnMessage);
});
You have the onBeforeUnload event you can catch in JavaScript. See here.
Use window.onbeforeunload
window.onbeforeunload = function () {
return "Are you sure you want to exit?";
};
Note that it will also end in Are you sure you want to leave this page (or are you sure you want to reload this page if you are reloading)

selecting pulldown in htmlunit

I am using htmlunit in jython and am having trouble selecting a pull down link. The page I am going to has a table with other ajax links, and I can click on them and move around and it seems okay but I can't seem to figure out how to click on a pulldown menu that allows for more links on the page(this pulldown affects the ajax table so its not redirecting me or anything).
Here's my code:
selectField1 = page.getElementById("pageNumSelection")
options2 = selectField1.getOptions()
theOption3 = options2[4]
This gets the option I want, I verify its right. so I select it:
MoreOnPage = selectField1.setSelectedAttribute(theOption3, True)
and I am stuck here(not sure if selecting it works or not because I don't get any message, but I'm not sure what to do next. How do I refresh the page to see the larger list? When clicking on links all you have to do is find the link and then select linkNameVariable.click() into a variable and it works. but I'm not sure how to refresh a pulldown. when I try to use the webclient to create an xml page based on the the select variable, I still get the old page.
to make it a bit easier, I used htmlunit scripter and got some code that should work but its java and I'm not sure how to port it to jython. Here it is:
try
{
page = webClient.getPage( url );
HtmlSelect selectField1 = (HtmlSelect) page.getElementById("pageNumSelection");
List<HtmlOption> options2 = selectField1.getOptions();
HtmlOption theOption3 = null;
for(HtmlOption option: options2)
{
if(option.getText().equals("100") )
{
theOption3 = option;
break;
}
}
selectField1.setSelectedAttribute(theOption3, true );
Have a look at HtmlForm getSelectedByName
HtmlSelect htmlSelect = form.getSelectByName("stuff[1].type");
HtmlOption htmlOption = htmlSelect.getOption(3);
htmlOption.setSelected(true);
Be sure that WebClient.setJavaScriptEnabled is called. The documentation seems to indicate that it is on by default, but I think this is wrong.
Alternatively, you can use WebDriver, which is a framework that supports both HtmlUnit and Selenium. I personally find the syntax easier to deal with than HtmlUnit.
If I understand correctly, the selection of an option in the select box triggers an AJAX calls which, once finished, modifies some part of the page.
The problem here is that since AJAX is, by definition, asynchronous, you can't really know when the call is finished and when you may inspect the page again to find the new content.
HtmlUnit has a class named NicelyResynchronizingAjaxController, which you can pass an instance of to the WebClient's setAjaxController method. As indicated in the javadoc, using this ajax controller will automatically make the asynchronous calls coming from a direct user interaction synchronous instead of asynchronous. Once the setSelectedAttribute method is called, you'll thus be able to see the changed made to the original page.
The other option is to use WebClient's waitForBackgrounfJavascript method after the selection is done, and inspect he page once the background JavaScript has ended, or the timeout has been reached.
This isn't really an answer to the question because I've not used HtmlUnit much before, but you might want to look at Selenium, and in particular Selenium RC. With Selenium RC you are able to control the interactions with a page displayed in a native browser (Firefox for example). It has developer API's for Java and Python amongst others.
I understand that HtmlUnit uses its own javascript and web browser rendering engine and I'm wondering whether that may be a problem.

Categories