diff --git a/v2/internal/frontend/runtime/desktop/ipc.js b/v2/internal/frontend/runtime/desktop/ipc.js index 022ba156..0f16eab3 100644 --- a/v2/internal/frontend/runtime/desktop/ipc.js +++ b/v2/internal/frontend/runtime/desktop/ipc.js @@ -10,25 +10,22 @@ The electron alternative for Go /* jshint esversion: 6 */ /** - * SendMessage sends the given message to the backend + * WailsInvoke sends the given message to the backend * * @param {string} message */ -// const windows = 0; -// const macos = 1; -// const linux = 2; +(function () { + // Credit: https://stackoverflow.com/a/2631521 + let _deeptest = function (s) { + var obj = window[s.shift()]; + while (obj && s.length) obj = obj[s.shift()]; + return obj; + }; + window.WailsInvoke = _deeptest(["chrome", "webview", "postMessage"]) || + _deeptest(["webkit", "messageHandlers", "external", "postMessage"]); -window.WailsInvoke = function (message) { - - // Call Platform specific invoke method - if (PLATFORM === 0) { - window.chrome.webview.postMessage(message); - } else if (PLATFORM === 1) { - window.webkit.messageHandlers.external.postMessage(message); - } else if (PLATFORM === 2) { - console.error("Unsupported Platform"); - } else { + if (!window.WailsInvoke) { console.error("Unsupported Platform"); } -}; +})(); \ No newline at end of file diff --git a/v2/internal/frontend/runtime/ipc_darwin.go b/v2/internal/frontend/runtime/ipc.go similarity index 55% rename from v2/internal/frontend/runtime/ipc_darwin.go rename to v2/internal/frontend/runtime/ipc.go index 524d10c9..1f887379 100644 --- a/v2/internal/frontend/runtime/ipc_darwin.go +++ b/v2/internal/frontend/runtime/ipc.go @@ -1,8 +1,8 @@ -//go:build darwin +//go:build darwin || windows package runtime import _ "embed" -//go:embed ipc_darwin.js +//go:embed ipc.js var DesktopIPC []byte diff --git a/v2/internal/frontend/runtime/ipc_darwin.js b/v2/internal/frontend/runtime/ipc_darwin.js deleted file mode 100644 index d770ecf0..00000000 --- a/v2/internal/frontend/runtime/ipc_darwin.js +++ /dev/null @@ -1 +0,0 @@ -(()=>{window.WailsInvoke=function(e){window.webkit.messageHandlers.external.postMessage(e)};})(); diff --git a/v2/internal/frontend/runtime/ipc_windows.go b/v2/internal/frontend/runtime/ipc_windows.go deleted file mode 100644 index 0099925c..00000000 --- a/v2/internal/frontend/runtime/ipc_windows.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build windows - -package runtime - -import _ "embed" - -//go:embed ipc_windows.js -var DesktopIPC []byte diff --git a/v2/internal/frontend/runtime/ipc_windows.js b/v2/internal/frontend/runtime/ipc_windows.js deleted file mode 100644 index 9adc4a01..00000000 --- a/v2/internal/frontend/runtime/ipc_windows.js +++ /dev/null @@ -1 +0,0 @@ -(()=>{window.WailsInvoke=function(e){window.chrome.webview.postMessage(e)};})(); diff --git a/v2/internal/frontend/runtime/package.json b/v2/internal/frontend/runtime/package.json index d462ce6e..28e9ddfb 100644 --- a/v2/internal/frontend/runtime/package.json +++ b/v2/internal/frontend/runtime/package.json @@ -5,8 +5,7 @@ "main": "index.js", "scripts": { "build": "run-p build:*", - "build:ipc-windows": "esbuild desktop/ipc.js --bundle --minify --outfile=ipc_windows.js --define:PLATFORM=0", - "build:ipc-darwin": "esbuild desktop/ipc.js --bundle --minify --outfile=ipc_darwin.js --define:PLATFORM=1", + "build:ipc-desktop": "esbuild desktop/ipc.js --bundle --minify --outfile=ipc.js", "build:ipc-dev": "cd dev && npm run build", "build:runtime-desktop-prod": "esbuild desktop/main.js --bundle --minify --outfile=runtime_prod_desktop.js --define:ENV=1", "build:runtime-desktop-dev": "esbuild desktop/main.js --bundle --sourcemap=inline --outfile=runtime_dev_desktop.js --define:ENV=0",