How to Open the Latest Web Viewer (v1.8.3) Programmatically?

Hi everyone!

I’m working on a custom Obsidian plugin and would like to programmatically open the latest version of the Web Viewer core plugin (v1.8.3). My goal is to create a new Web Viewer tab and load a specified URL through code.

Is there any API like this?

this.app.workspace.openUrl("https://example.com");

Thanks

The regular JS API window.open("https://example.com") will automatically open the page in the web viewer, if it’s disabled it will load in the default browser instead.

1 Like

Cool, thanks a lot.

This method will pop up a new window, is there any way to open it in a new tab?
@joethei

1 Like

You can just use the standard getLeaf & setViewState API. This method is mentioned in this page of the docs: Views - Developer Documentation

// set paneType to be "tab" if you want to open in a new tab
const leaf = this.app.workspace.getLeaf(paneType);
await leaf.setViewState({
    type: 'webviewer',
    state: {
        url: 'https://obsidian.md',
        navigate: true,
    },
    active: true,
});
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.