Wednesday, December 4, 2024

Re: GWT history with push state

Thank you Thomas and Colin.  Setting onpopstate with a lambda is perfect!

On Thursday, 5 December 2024 at 6:51:23 am UTC+11 Thomas Broyer wrote:
On Wednesday, December 4, 2024 at 5:10:32 PM UTC+1 Colin Alworth wrote:
Note that "@FunctionalInterface" is not required to express a Java interface with only one abstract method as a lambda. You can write:

DomGlobal.window.onpopstate = event -> {
    // Do something
    return null;
};

to use it as a lambda. This compiles for me with GWT 2.12.1 and Java 11.

Does it show that I haven't written that kind of code for quite some time? 😂
(and/or rely too much on my IDE)
 
The key is the "return null", missing in your example (and in Thomas's as well as a return type for the anon inner class). 

The return type is declared as Object, which may be an oversight in the original closure externs file, I don't see a way that an event handler can change anything by returning a different value. In JS, a missing return would implicitly return undefined, but Java requires an explicit return for non-void methods.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/f4c976e2-7b1c-458a-b680-388e55b92541n%40googlegroups.com.

Re: GWT history with push state



On Wednesday, December 4, 2024 at 5:10:32 PM UTC+1 Colin Alworth wrote:
Note that "@FunctionalInterface" is not required to express a Java interface with only one abstract method as a lambda. You can write:

DomGlobal.window.onpopstate = event -> {
    // Do something
    return null;
};

to use it as a lambda. This compiles for me with GWT 2.12.1 and Java 11.

Does it show that I haven't written that kind of code for quite some time? 😂
(and/or rely too much on my IDE)
 
The key is the "return null", missing in your example (and in Thomas's as well as a return type for the anon inner class). 

The return type is declared as Object, which may be an oversight in the original closure externs file, I don't see a way that an event handler can change anything by returning a different value. In JS, a missing return would implicitly return undefined, but Java requires an explicit return for non-void methods.

An event handler's return type is used to cancel the event (https://blog.ltgt.net/html-event-handlers/#return-value / https://html.spec.whatwg.org/multipage/webappapis.html#the-event-handler-processing-algorithm), but for that specific event it doesn't matter as it's not cancelable anyway (https://html.spec.whatwg.org/multipage/browsing-the-web.html#updating-the-document:popstateevent)

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/dfc672c9-0f91-4f15-9de2-408b3f2cecf5n%40googlegroups.com.

Re: GWT history with push state

Note that "@FunctionalInterface" is not required to express a Java interface with only one abstract method as a lambda. You can write:

DomGlobal.window.onpopstate = event -> {
    // Do something
    return null;
};

to use it as a lambda. This compiles for me with GWT 2.12.1 and Java 11.

The key is the "return null", missing in your example (and in Thomas's as well as a return type for the anon inner class). 

The return type is declared as Object, which may be an oversight in the original closure externs file, I don't see a way that an event handler can change anything by returning a different value. In JS, a missing return would implicitly return undefined, but Java requires an explicit return for non-void methods.
On Wednesday, December 4, 2024 at 3:54:27 AM UTC-6 Thomas Broyer wrote:
addEventListener indeed is an option (and I'd say possibly a better option), but AFAICT you could also have done:

DomGlobal.window.onpopstate = new OnpopstateFn() {
    @Override
    public onInvoke(Event event) {
        // do something
    }
});

On Wednesday, December 4, 2024 at 5:23:00 AM UTC+1 ma...@craig-mitchell.com wrote:
I'm finally getting around to replacing these calls with Elemental2.

To replace the JSNI method:

public static native void initialise() /*-{
$wnd.onpopstate = $entry(function(e) {
@com.blah.YourClass::yourMethodThatListensForUrlChange()();
});
}-*/;

I would have thought it would be:

public static void initialise() {
DomGlobal.window.onpopstate.onInvoke(event -> {
// Do something
});
}

However, that doesn't compile, as onInvoke takes a @JsFunction, not a @FunctionalInterface.  So, I have to do something like this:

public static void initialise() {
DomGlobal.window.addEventListener("popstate", event -> {
// Do something
});
}

Is this correct?

On Friday, 16 October 2020 at 3:40:39 pm UTC+11 Craig Mitchell wrote:
As Tomas said, not directly with History.  It's simple JSNI though:

public static native void updateURL(String newUrl) /*-{
    $wnd.history.pushState(newUrl, "", newUrl);
}-*/;

public static native void initialise() /*-{
$wnd.onpopstate = $entry(function(e) {
@com.blah.YourClass::yourMethodThatListensForUrlChange()();
});
}-*/;

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/e13d42ad-95ac-422e-8694-db9dc997723cn%40googlegroups.com.

Re: Chrome SecurityError: Failed to execute 'pushState' on 'History'

I tried it as well ( elemental2-dom solution) however the behaviour was quite different to before so I stopped investigating.
The fix for us was to add a null / blank check to the historyRewrite Parameter which resulted in 'URL 'about:blank'' which has a different origin than the actual URL and is thus blocked...

Still thank you for your help everyone!

Craig Mitchell schrieb am Mittwoch, 4. Dezember 2024 um 04:52:13 UTC+1:
Apologies, just saw this:  https://github.com/google/elemental2?tab=readme-ov-file#gwt

Added:  <inherits name="elemental2.dom.Dom"/>

And it's working great!
On Wednesday, 4 December 2024 at 2:45:36 pm UTC+11 Craig Mitchell wrote:
A little off topic, however, I just tried to use elemental2-dom, without any success.

I add the lib to my client module:

<dependency>
  <groupId>com.google.elemental2</groupId>
  <artifactId>elemental2-dom</artifactId>
  <version>1.2.3</version>
</dependency>

And then try to use it:

DomGlobal.history.pushState(myUrl, "", myUrl);

And got the error:

[ERROR] Line 190: No source code is available for type elemental2.dom.DomGlobal; did you forget to inherit a required module?

So, I added:

<inherits name="elemental2.dom.DomGlobal"/>

to my gwt.xml, and got the error:

[ERROR] Unable to find 'elemental2/dom/DomGlobal.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?

Any idea what I'm doing wrong?

Thanks.

On Wednesday, 4 December 2024 at 5:05:41 am UTC+11 Colin Alworth wrote:
First... you should update this to use jsinterop - consider just adding elemental2-dom to your project and calling DomGlobal.history.pushState(new Object(), "", historyRewrite); in plain Java?

To answer the question more directly, you probably need to change "window" to "$wnd" to access the actual outer window. Your current code, as written, is technically attempting to change the URL of the iframe that the app is loaded in.
On Tuesday, December 3, 2024 at 11:43:00 AM UTC-6 Lonzak wrote:
I successfully updated GWT from 2.10.1 to 2.12.1.
In Firefox everything works perfectly however not in chrome based browsers. When I try to
execute the following code:

public static native int setHistory(String historyRewrite) /*-{

window.history.pushState({}, '', historyRewrite);

}-*/;


Error:
SEVERE: (SecurityError) : Failed to execute 'pushState' on 'History': A history state object with URL 'https://company.url/home' cannot be created in a document
with origin 'https://company.url' and URL 'about:blank'.
com.google.gwt.core.client.JavaScriptException: (SecurityError) : Failed to execute 'pushState' on 'History': A history state object with URL
'https://company.url/home' cannot be created in a document with origin 'https://company.url' and URL 'about:blank'.

If I revert to GWT it works again.
Any idea what changed (and how to fix it)?
Is the script maybe loaded before the document is fully loaded?

Thanks!

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/d6528117-7a6b-4d95-ab2f-5f3d1e2c640cn%40googlegroups.com.

Re: GWT history with push state

addEventListener indeed is an option (and I'd say possibly a better option), but AFAICT you could also have done:

DomGlobal.window.onpopstate = new OnpopstateFn() {
    @Override
    public onInvoke(Event event) {
        // do something
    }
});

On Wednesday, December 4, 2024 at 5:23:00 AM UTC+1 ma...@craig-mitchell.com wrote:
I'm finally getting around to replacing these calls with Elemental2.

To replace the JSNI method:

public static native void initialise() /*-{
$wnd.onpopstate = $entry(function(e) {
@com.blah.YourClass::yourMethodThatListensForUrlChange()();
});
}-*/;

I would have thought it would be:

public static void initialise() {
DomGlobal.window.onpopstate.onInvoke(event -> {
// Do something
});
}

However, that doesn't compile, as onInvoke takes a @JsFunction, not a @FunctionalInterface.  So, I have to do something like this:

public static void initialise() {
DomGlobal.window.addEventListener("popstate", event -> {
// Do something
});
}

Is this correct?

On Friday, 16 October 2020 at 3:40:39 pm UTC+11 Craig Mitchell wrote:
As Tomas said, not directly with History.  It's simple JSNI though:

public static native void updateURL(String newUrl) /*-{
    $wnd.history.pushState(newUrl, "", newUrl);
}-*/;

public static native void initialise() /*-{
$wnd.onpopstate = $entry(function(e) {
@com.blah.YourClass::yourMethodThatListensForUrlChange()();
});
}-*/;

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/60e98ca7-e63b-4f64-b7f2-b6b875e8b092n%40googlegroups.com.

Tuesday, December 3, 2024

Re: GWT history with push state

I'm finally getting around to replacing these calls with Elemental2.

To replace the JSNI method:

public static native void initialise() /*-{
$wnd.onpopstate = $entry(function(e) {
@com.blah.YourClass::yourMethodThatListensForUrlChange()();
});
}-*/;

I would have thought it would be:

public static void initialise() {
DomGlobal.window.onpopstate.onInvoke(event -> {
// Do something
});
}

However, that doesn't compile, as onInvoke takes a @JsFunction, not a @FunctionalInterface.  So, I have to do something like this:

public static void initialise() {
DomGlobal.window.addEventListener("popstate", event -> {
// Do something
});
}

Is this correct?

On Friday, 16 October 2020 at 3:40:39 pm UTC+11 Craig Mitchell wrote:
As Tomas said, not directly with History.  It's simple JSNI though:

public static native void updateURL(String newUrl) /*-{
    $wnd.history.pushState(newUrl, "", newUrl);
}-*/;

public static native void initialise() /*-{
$wnd.onpopstate = $entry(function(e) {
@com.blah.YourClass::yourMethodThatListensForUrlChange()();
});
}-*/;

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/59a9c005-54fe-445f-9d35-d554d5b9039fn%40googlegroups.com.

Re: Chrome SecurityError: Failed to execute 'pushState' on 'History'

Apologies, just saw this:  https://github.com/google/elemental2?tab=readme-ov-file#gwt

Added:  <inherits name="elemental2.dom.Dom"/>

And it's working great!
On Wednesday, 4 December 2024 at 2:45:36 pm UTC+11 Craig Mitchell wrote:
A little off topic, however, I just tried to use elemental2-dom, without any success.

I add the lib to my client module:

<dependency>
  <groupId>com.google.elemental2</groupId>
  <artifactId>elemental2-dom</artifactId>
  <version>1.2.3</version>
</dependency>

And then try to use it:

DomGlobal.history.pushState(myUrl, "", myUrl);

And got the error:

[ERROR] Line 190: No source code is available for type elemental2.dom.DomGlobal; did you forget to inherit a required module?

So, I added:

<inherits name="elemental2.dom.DomGlobal"/>

to my gwt.xml, and got the error:

[ERROR] Unable to find 'elemental2/dom/DomGlobal.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?

Any idea what I'm doing wrong?

Thanks.

On Wednesday, 4 December 2024 at 5:05:41 am UTC+11 Colin Alworth wrote:
First... you should update this to use jsinterop - consider just adding elemental2-dom to your project and calling DomGlobal.history.pushState(new Object(), "", historyRewrite); in plain Java?

To answer the question more directly, you probably need to change "window" to "$wnd" to access the actual outer window. Your current code, as written, is technically attempting to change the URL of the iframe that the app is loaded in.
On Tuesday, December 3, 2024 at 11:43:00 AM UTC-6 Lonzak wrote:
I successfully updated GWT from 2.10.1 to 2.12.1.
In Firefox everything works perfectly however not in chrome based browsers. When I try to
execute the following code:

public static native int setHistory(String historyRewrite) /*-{

window.history.pushState({}, '', historyRewrite);

}-*/;


Error:
SEVERE: (SecurityError) : Failed to execute 'pushState' on 'History': A history state object with URL 'https://company.url/home' cannot be created in a document
with origin 'https://company.url' and URL 'about:blank'.
com.google.gwt.core.client.JavaScriptException: (SecurityError) : Failed to execute 'pushState' on 'History': A history state object with URL
'https://company.url/home' cannot be created in a document with origin 'https://company.url' and URL 'about:blank'.

If I revert to GWT it works again.
Any idea what changed (and how to fix it)?
Is the script maybe loaded before the document is fully loaded?

Thanks!

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/6fa5c58f-2e1b-43dc-8ae9-0ea90e945d83n%40googlegroups.com.