Open Preview or Browser Programmatically


#1

Is there a way to open a preview of a running application or browser window with code from within c9. so i usually code javascript nodejs and use a webpack dev server. there are a couple places to go and click a button to get either the preview within c9 or a full browser tab. just looking to add some code or a script somewhere just to save myself a click


#2

To open preview you can use preview command

services.commands.exec("preview", null, {
    server: true,
    newTab: true
});

similar to https://github.com/c9/core/blob/cc0ecb420bf8a8eb5e32b0fb60d7924f88c970be/plugins/c9.ide.preview/preview.js#L89

You can add code to your init script (which can be opened from cloud9 menu), what other actions do you want that code to perform?


#3

cool. but this init code in the init script opens a preview window when i open my workspace. its cool that this works, but for example i want to do the following. i usually start my dev server from the command line with npm start, or whatever it happens to be. usually that command points to something like node index.js . i want to have the window open command executed during or directly after this.

how do i get commands or functions out of the init script and into the context of the workspace so i can call them whenever i want.
thanks.


#4

how do i make it so my postgresql database starts from the init script. right now i enter this in the command line to start it sudo service postgresql start


#5

What workflow you want to have,
You either can create a bash script that runs your project and open a preview window with c9 command, or a button/shortcut/menu item in the ide that writes something in the terminal and opens the preview