Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: app.onIpcMessage is not a function #14

Closed
etornam45 opened this issue Dec 11, 2024 · 3 comments
Closed

TypeError: app.onIpcMessage is not a function #14

etornam45 opened this issue Dec 11, 2024 · 3 comments

Comments

@etornam45
Copy link

C:\Users\Etornam\Documents\codes\konja\try.js:5
app.onIpcMessage((data) => {
    ^

TypeError: app.onIpcMessage is not a function
    at Object.<anonymous> (C:\Users\Etornam\Documents\codes\konja\try.js:5:5)
    at Module._compile (node:internal/modules/cjs/loader:1565:14)
    at Object..js (node:internal/modules/cjs/loader:1708:10)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49

Node.js v22.12.0
const { Application } = require('@webviewjs/webview');

const app = new Application();

app.onIpcMessage((data) => {
  const reply = `You sent ${data.body.toString('utf-8')}`;
  window.evaluateScript(`onIpcMessage("${reply}")`);
});

const window = app.createBrowserWindow();
const webview = window.createWebview({
  html: `<!DOCTYPE html>
    <html>
        <head>
            <title>Webview</title>
        </head>
        <body>
            <h1 id="output">Hello world!</h1>
            <button id="btn">Click me!</button>
            <script>
                btn.onclick = function send() {
                    window.ipc.postMessage('Hello from webview');
                }
            </script>
        </body>
    </html>
    `,
  preload: `window.onIpcMessage = function(data) {
        const output = document.getElementById('output');
        output.innerText = \`Server Sent A Message: \${data}\`;
    }`,
});

window.setTitle('WebviewJS + Node');

app.run();```
@etornam45
Copy link
Author

I dont know where to locate the onIpcMessage function in the source code but I found

#[napi]
  /// Sets the IPC handler callback.
  pub fn on_ipc_message(&mut self, handler: Option<FunctionRef<IpcMessage, ()>>) {
    *self.ipc_state.borrow_mut() = handler;
  }```
  
  is it the same function

@twlite
Copy link
Member

twlite commented Dec 13, 2024

hi, this has been changed to

const app = new Application();
const window = app.createBrowserWindow();

const webview = window.createWebview({
    html: `<!DOCTYPE html>
    <html>
        <head>
            <title>Webview</title>
        </head>
        <body>
            <h1 id="output">Hello world!</h1>
            <button id="btn">Click me!</button>
            <script>
                btn.onclick = function send() {
                    window.ipc.postMessage('Hello from webview');
                }
            </script>
        </body>
    </html>
    `,
    preload: `window.onIpcMessage = function(data) {
        const output = document.getElementById('output');
        output.innerText = \`Server Sent A Message: \${data}\`;
    }`
});

if (!webview.isDevtoolsOpen()) webview.openDevtools();

webview.onIpcMessage((data) => {
    const reply = `You sent ${data.body.toString('utf-8')}`;
    window.evaluateScript(`onIpcMessage("${reply}")`)
})

app.run();

@etornam45
Copy link
Author

Thanks,

Now I get this issue #13

@twlite twlite closed this as completed Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants