-1

I'm using chromium embedded in my application. Using it, I would like to load the web-version of Visual Studio Code ( https://vscode.dev/ ), and then automatically open a specific directory on the device, without the user having to do it manually.

I'm sure this is possible using JavaScript (even if not officially supported), but I'm not sure how to find out which commands are responsible for opening a folder. When you press "Open Folder..." in VS Code, it opens a file dialog window where you can select the target directory. If I could break when a folder has actually been selected (and confirmed), I could probably narrow it down, but I couldn't find a listener even that would work for such a case.

Is there a simple way to do this without having to take a deep dive into the entire source code?

Silverlan
  • 2,783
  • 3
  • 31
  • 66
  • ... web VS Code can open local folders? – starball May 11 '23 at 20:06
  • Yes, but only in chromium-based browsers (and it has to be confirmed by the user). It's not possible in Firefox. – Silverlan May 12 '23 at 06:23
  • 1
    "_I'm sure this is possible using JavaScript (even if not officially supported)_": Why are you sure? – jsejcksn May 13 '23 at 14:58
  • could you provide some minimal setup code to make it easier to get started working an an attempt at an answer? perhaps a [stack snippet](https://meta.stackoverflow.com/q/358992/11107541)? – starball May 14 '23 at 10:01
  • 1
    VS Code probably uses `showDirectoryPicker` to open the directory, so you can [search for that term in the source](https://github.com/microsoft/vscode/search?q=showdirectorypicker). For the File System Access API, I don't think you can automatically specify a directory in browser without it having been picked previously. It does offer a [`startIn`](https://developer.mozilla.org/en-US/docs/Web/API/Window/showDirectoryPicker#parameters) parameter, but it only offers a starting point, and it's only for [well known directories](https://wicg.github.io/file-system-access/#enumdef-wellknowndirectory) – Steve May 16 '23 at 22:51
  • @Steve, sorry if my answer seems to be a plagia of your comment, believe me when I say that I really made all the technical search. If I had seen your comment, I wouldn't have post. – Philippe May 20 '23 at 08:30
  • @Philippe, no worries, I didn't bother posting an answer because I don't have a complete answer. Like others, I suspect regular browsers don't allow this for security reasons, in general. It might be possible with a custom build of vscode with custom browser extensions, though at that point, it seems not too different than the electron version of vscode – Steve May 23 '23 at 15:48

3 Answers3

0

Well, if you are talking about window.showOpenFilePicker(), it is well documented on mdn web docs and I can't see any reference to a starting folder. With the options, you can filter by file types, but that's all with fun.

Yes but! Maybe you could have a chance here with the startIn option. Or not, if you're not over https of if it is not yet implemented on every browsers...

The startIn and id options were first introduced in Chrome 91.

By the way, on Chrome over Windows (in https context), such a piece of code is ok :

async function getFile(knownDirectory) {
  options = {
    id: 'foo',
    startIn: knownDirectory,
  };
  // open file picker, destructure the one element returned array
  [fileHandle] = await window.showOpenFilePicker(options);

  // run code with our fileHandle
}

getFile("pictures");

But ONLY with specific system folders, aka "well known directories" (pictures,desktop, downloads for example). Technically speaking, you can add new ones locally but it's a bit hard to deploy...

[EDIT]

After a very interesting discussion below, it appears that there is no way to inject any script on https://vscode.dev/. So, the piece of code above is not useful in your case.

Philippe
  • 1,134
  • 12
  • 22
  • even then, how would you get the file picker to automatically get something picked? the question asks: "_I would like to load the web-version of Visual Studio Code ( https://vscode.dev/ ), and then automatically open a specific directory on the device, without the user having to do it manually._" – starball May 19 '23 at 23:17
  • You're right @user, even then... it may be usefull for someone to retrieve this piece of a modest attempt at an answer. I personnaly had fun to write this post, and learned many things (who cares in fact ? ^^) – Philippe May 19 '23 at 23:26
  • 1
    There's nothing wrong with the type of answer you've posted here (see [answer], which affirms this). I'm just commenting on the part where you said "_Maybe you could have a chance here_", to question how probable that "maybe" is. – starball May 19 '23 at 23:29
  • @user maybe a sort of monkey (grease or tamper) could intercede here ? – Philippe May 19 '23 at 23:36
  • I don't know much about userscript development (I've only done a little bit myself), but from a scan of https://www.tampermonkey.net/documentation.php, I didn't see anything that looked obviously useful here. – starball May 19 '23 at 23:48
  • @user, I have investigated further more, in fact Content Security Policy directives on vscode.dev don't allow inline scripts (legit ^^). So you're right, no userscript could achieve that. – Philippe May 20 '23 at 08:15
0

If the browser does its job of sandboxing sites from your machine properly, I expect the answer to be no- not with plain JS in the embedded browser (though I might be wrong now, or in the future- Ex. If a standard permission is added for webpages accessing the user's filesystem to access something it has been granted access to before without asking for permission again). At the theoretical point when you have to select what directory to open, the browser should present you with a file dialog that scripts in the browser cannot control for security reasons.

If you want anything more, you'd need ask a question about using a specific technology for driving system-level inputs.

starball
  • 20,030
  • 7
  • 43
  • 238
0

If you want to open a specific folder in your browser using vscode.dev, you can use the Dev Containers extension for VS Code. This extension allows you to create or access a complete development environment in a container and open any folder that is in or mounted to a container.

To use this extension, you need to install and configure Docker, then launch Dev Containers: Open Folder in Container... in VS Code. command in VS Code and select the folder you want to open. This folder can be local or remote, as long as you can access it through Docker.

If you want to use JavaScript to open a folder automatically, you may need to look at VS Code source code or use some unofficial method. I haven't found an easy way to do this, so I suggest you use the Dev Containers extension to solve your problem.

References:

(1) Developing inside a Container - Visual Studio Code

(2) Workspaces in Visual Studio Code

(3) Is there a way to open a folder in a container from the VSCode command line using the remote-containers extension?.

(4) How to open folder in new vs code instance by right clicking on the folder in the existing vs code instance?.