+
Skip to content

[messaging] How to communicate with an iFrame? #90

@christianlundberg

Description

@christianlundberg

The documentation says that we're supposed to use defineWindowMessaging in order to communicate with an iFrame, but since it's missing an example, I can't figure out what's missing, but I never seem to receive the message.

// content script
import { windowMessaging } from '@/windowMessaging';

export default defineContentScript({
  matches: ['<all_urls>'],

  main(ctx) {
    // Define the UI
    const ui = createIframeUi(ctx, {
      page: '/app.html',
      position: 'inline',
      anchor: 'body',
    });

    // Show UI to user
    ui.mount();

    setTimeout(() => {
      console.log('content: sending test');
      windowMessaging.sendMessage('test', undefined);
    }, 5000);
  },
});
// iframe
import { windowMessaging } from '@/windowMessaging';

windowMessaging.onMessage('test', () => {
  console.log('iframe: received test');
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载