tryphotino / photino.NET

https://tryphotino.io
Apache License 2.0
915 stars 74 forks source link

What does it mean to Photino that window.external is deprecated by browsers? #124

Open raddevus opened 1 year ago

raddevus commented 1 year ago

Just discovered that window.external is being deprecated by browsers. You can see the deprecation warning on the MDN at: https://developer.mozilla.org/en-US/docs/Web/API/Window/external

What will this mean for Photino.NET moving forward?

vvollers commented 1 year ago

I doubt it will mean much since the window.external object is being initialized/created by Photino itself. In the end it is a wrapper around window.chrome.webview.postMessage (Windows/Webview2), window.webkit.messageHandlers.Photinointerop (a custom handler, Linux/WebkitGTK) and window.webkit.messageHandlers.photinointerop (Mac/Webkit).

see for example (windows) https://github.com/tryphotino/photino.Native/blob/24be1c50d3dcb4c92c33a0081160ecc10959553c/Photino.Native/Photino.Windows.cpp#L751

window.external = {
    sendMessage: function(message) {
        window.chrome.webview.postMessage(message);
    },
    receiveMessage: function(callback) {
        window.chrome.webview.addEventListener('message', function(e) {
            callback(e.data);
        });
    }
};

The initialization is a raw declaration of a new object, regardless if 'window.external' already existed, which javascript allows (try overwriting console.log with a new function sometime). So the deprecation of a mechanism which wasn't used doesn't affect the project I think.

raddevus commented 1 year ago

That is excellent info and great news. thanks for pointing out that this is actually a wrapper around those other (supported) methods.

raddevus commented 1 year ago

Closed since window.external is a Photino wrapper around the proper code.

MikeYeager commented 11 months ago

Going forward, we may need to implement this on a platform-by-platform basis.

WebView2 (Windows): https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addhostobjecttoscript?view=webview2-dotnet-1.0.2151.40#Microsoft_Web_WebView2_Core_CoreWebView2_AddHostObjectToScript_System_String_System_Object

WebKitGTK (Linux): maybe this? https://webkitgtk.org/reference/webkit2gtk/stable/method.WebView.run_javascript.html

WebKit (Mac): maybe this? https://stackoverflow.com/questions/62035494/how-to-call-postmessage-in-wkwebview-to-js

For now, to get around TypeScript compile errors you can do a work-around like (myObject as any).myMethod() or myObject.myMethod?.()

stevesobol commented 11 months ago

That would work.

I played around a bit and discovered // @ts-ignore works too. The code runs, it just won't transpile because it isn't valid TypeScript.

We should look into the options you talked about, but I think there will still be an issue with TypeScript. This may not be a problem that requires us to make changes. I'm not sure.