I'm making an electron app and I want to make a button in my index.html
file to open up the default internet browser (ex. Chrome) to my GitHub repository website. I have seen other StackOverflow questions on this with successful answers, but they contain snippets of JavaScript and I don't know where to put them.

- 157
- 1
- 10
-
Does this answer your question? [How can I force external links from browser-window to open in a default browser from Electron?](https://stackoverflow.com/questions/32402327/how-can-i-force-external-links-from-browser-window-to-open-in-a-default-browser) – icc97 Aug 24 '23 at 13:21
2 Answers
Use the shell module:
Add the following code to your main.js:
At its beginning:
const {ipcMain} = require('electron');
const {shell} = require('electron');
After the app.on function:
ipcMain.on('loadGH', (event, arg) => {
shell.openExternal(arg);
});
Within the head of your index.html you then need to instantiate the ICP module:
<script>
const ipc = require('electron').ipcRenderer;
</script>
Then use the onclick event to actually perform the loading of the new window:
<a HREF="#" onclick="ipc.send('loadGH','http://github.com/yourGitHubName');">Link</a>

- 5,266
- 41
- 57
This answer references an earlier ambigious version of the question, then hinting at the need to open a new electron window.
When you say "external link" I assume you want to open another window containing your github repo.
This can be done by adding the following code to your main.js:
At its beginning:
const ipcMain = require('electron').ipcMain;
Inside the app.on function:
var externalWindow = new BrowserWindow ({
width: 800,
height: 600
})
After the app.on function:
ipcMain.on('loadGH', (event, arg) => {
externalWindow.loadURL(arg);
});
Within the head of your index.html you then need to instantiate the ICP module:
<script>
const ipc = require('electron').ipcRenderer;
</script>
Then use the onclick event in an href to actually perform the loading of the new window:
<a HREF="#" onclick="ipc.send('loadGH','http://github.com/yourGitHubName');">Link</a>

- 5,266
- 41
- 57
-
Which `app.on` function should the first and second snippets go in? I have two `app.on` functions and the first one checks if the application if the `process.platform` is not equal to `darwin`. In the comments made by the GitHub, it says it has something to do with OS X. I'm not sure if you have these extra functions or not--our `main.js` files may be different. The one I just mentioned is also declared like so... `app.on('window-all-closed', function() {` – andersamer Jun 27 '16 at 23:04
-
That's fine, you'll have to look for app.on('ready', function {... }), this is the one that is called upon app start. – Jens Habegger Jun 27 '16 at 23:11
-
whoops... it appears I didn't ask my question right. I actually wanted to open up the default internet browser containing my GitHub repo. I'll fix that above. – andersamer Jun 27 '16 at 23:16