I am looking to implement a MouseOver event handler for any tag, specifically targeting anchor tags in a legacy HTML page.
Following a GWT guide, I successfully utilized their JSNI method to retrieve all anchor tags with some minor adjustments for errors.
Now, my goal is to bind all the elements collected in an ArrayList to an event handler. How can I achieve this?
Below is the code snippet that I have written:
private native void putElementLinkIDsInList(BodyElement elt, ArrayList list) /*-{
var links = elt.getElementsByTagName("a");
for (var i = 0; i < links.length; i++ ) {
var link = links.item(i);
link.id = ("uid-a-" + i);
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87ebeef4f3a9c7ede6f1e6a9f2f3eeeba9c6f5f5e6fecba0ebecf7">[email protected]</a>::add(Ljava/lang/Object;) (link.id);
}
}-*/;
/**
* Find all anchor tags and if any point outside the site, redirect them to a
* "blocked" page.
*/
private void rewriteLinksIterative() {
ArrayList links = new ArrayList();
putElementLinkIDsInList(Document.get().getBody(), links);
for (int i = 0; i < links.size(); i++) {
Element elt = DOM.getElementById((String) links.get(i));
rewriteLink(elt, "www.example.com");
}
}
/**
* Block all accesses out of the website that don't match 'sitename'
*
* @param element
* An anchor link element
* @param sitename
* name of the website to check. e.g. "www.example.com"
*/
private void rewriteLink(Element element, String sitename) {
String href = DOM.getElementProperty(element, "href");
if (null == href) {
return;
}
// We want to re-write absolute URLs that go outside of this site
if (href.startsWith("http://")
&& !href.startsWith("http://" + sitename + "/")) {
DOM.setElementProperty(element, "href", "http://" + sitename
+ "/Blocked.html");
}
}