How to open URL in default browser from plugin?

I have one general questions regarding URLs.

I want to implement a command to open the source_url in the default browser. But I couldn't get it running. It seems the plugin has no access to electron packages (e.g. shell, bridge) and other npm package (e.g. open, also not work for me.

So, how can I open an URL in the default browser? Or rather, any other type of URL in the corresponding application/process?

This is how my command currently looks like:

await joplin.commands.register({
  name: 'openURLInBrowser',
  label: 'Open URL in browser',
  iconName: 'fas fa-globe',
  enabledCondition: "oneNoteSelected",
  execute: async () => {
    /* get the selected note and exit if none is currently selected */
    const selectedNote = await joplin.workspace.selectedNote();
    if (!selectedNote) return;
    /* exit if 'source_url' is not set for note */
    if (!selectedNote.source_url) return;

    /* open URL in default browser */
    // 1: from joplin code - does not work
    // const bridge = require('electron').remote.require('./bridge').default;
    // bridge().openExternal('');

    // 2: electron shell - does not work
    // const { shell } = require('electron');
    // shell.openExternal('');

    // 3: npm open - does not work
    // first: npm install open
    // const open = require('open');
    // await open('');
    // await open(note.source_url);

    // 4: built in open command - does not work
    // open('', '_blank');
    open(selectedNote.source_url, '_blank');
1 Like