Home    |    GitHub Page    |    API    |    FAQ

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, https://www.npmjs.com/package/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('https://www.npmjs.com/package/open');

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

    // 3: npm open - does not work
    // https://github.com/pwnall/node-open
    // https://www.npmjs.com/package/open
    // first: npm install open
    // const open = require('open');
    // await open('https://github.com');
    // await open(note.source_url);

    // 4: built in open command - does not work
    // open('https://www.google.com', '_blank');
    open(selectedNote.source_url, '_blank');
  },
});