Can you use absolute paths in Electron?

The simple question here is that I cannot find a direct answer.

Is it possible to use absolute paths for dependencies (for example, <script src="/my-script.js"></script> ) with the electron and work?

Currently it just does mainWindow.loadURL('file://' + __dirname + '/index.html');

What loads index.html is just fine, but here's the thing, index.html loads

Which fails because it looks at the root of the entire hard drive

This will simplify my life, because otherwise I would have to reorganize a bunch of URLs for the templates, and some of them would forever break my application if I ever wanted to add pages to a subdirectory of a website (for example, http: //website.com/m/ ).

Any suggestions? Thanks!

+6
source share
2 answers

You can override the built-in URI to resolve the file path using protocol.interceptFileProcotol() , then your handler can map /my-script.js to whatever path you want.

+2
source

Relative paths work just fine. Try removing the first / as follows: <script src="my-script.js"></script>

If you really need access to the absolute path of your applications, you can get it using app.getAppPath() :

 var app = require('remote').require('app'); console.log(app.getAppPath()); 

And you can load scripts by inserting them into the DOM with a small amount of built-in JS.

+2
source

All Articles