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

Fix passing WebContents to the window option #141

Merged
merged 6 commits into from Jun 17, 2021

Conversation

kondoumh
Copy link
Contributor

@kondoumh kondoumh commented Jun 15, 2021

This is a fix that allows you to specify WebContents for the window option of the contextMenu.
I want to receive a WebView dom-ready event from the renderer process and set WebContents in the main process to create a context menu. Like this.

ipcMain.on("webview-ready", (e, url) => {
  const content = webContents.getAllWebContents().find(c => c.getURL() === url);
  contextMenu({
    window: content,
    prepend: (defaultActions, parameters, mainWindow) => [
      {
        label: 'Rainbow',
        visible: parameters.mediaType === 'image'
      },
      {
        label: 'Search Google for “{selection}”',
        visible: parameters.selectionText.trim().length > 0,
        click: () => {
          shell.openExternal(`https://google.com/search?q=${encodeURIComponent(parameters.selectionText)}`);
        }
      }
    ]
  });
});

Currently, when I specify WebContents for window, I get the following error:

Uncaught Exception:
TypeError: Cannot read property '_events' of undefined
at _addListener (events.js:350:19)
at addListener (events.js:406:10)
at module.exports (/xxxx/node_modules/electron-context-menu/index.js:362:4)

@sindresorhus sindresorhus changed the title Allow WebContents to be set in the window option Fix passing WebContents to the window option Jun 17, 2021
@sindresorhus sindresorhus merged commit 798b616 into sindresorhus:main Jun 17, 2021
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

Successfully merging this pull request may close these issues.

None yet

2 participants