Skip to content

Commit 4c4aba0

Browse files
committed
feat: add function to open external link to the default browser & add about
1 parent 5e15b7c commit 4c4aba0

File tree

5 files changed

+116
-38
lines changed

5 files changed

+116
-38
lines changed

main.js

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
1-
const { app, BrowserWindow, nativeImage, shell } = require('electron');
2-
const path = require('path');
1+
const { app, ipcMain, shell } = require('electron');
2+
const createMenu = require('./menu');
3+
const createWindow = require('./window');
34

4-
let mainWindow;
5+
app.on('ready', () => {
6+
const mainWindow = createWindow();
7+
createMenu(mainWindow);
58

6-
function createWindow() {
7-
const iconPath = path.join(__dirname, 'assets/icon.png');
8-
9-
mainWindow = new BrowserWindow({
10-
width: 800,
11-
height: 600,
12-
autoHideMenuBar: true, // Hide the toolbar
13-
icon: nativeImage.createFromPath(iconPath),
14-
});
15-
16-
mainWindow.loadURL('https://misskey.id');
17-
18-
mainWindow.webContents.on('new-window', (event, url, frameName, disposition, options) => {
19-
event.preventDefault(); // Prevent opening the link in a new window
20-
21-
// Check if the clicked link has a class ending with "_link"
22-
const regex = /(\S+_link)$/i;
23-
const className = frameName.match(regex)?.[0];
24-
25-
if (className) {
26-
// Open links with the specified class in the default browser
27-
shell.openExternal(url);
28-
} else {
29-
// Handle other types of links within the Electron app
30-
mainWindow.loadURL(url);
31-
}
9+
// Register an IPC listener to receive messages from the renderer process
10+
ipcMain.on('open-external-link', (event, url) => {
11+
shell.openExternal(url); // Open external links in the default browser
3212
});
33-
34-
mainWindow.on('closed', () => {
35-
mainWindow = null;
36-
});
37-
}
38-
39-
app.on('ready', createWindow);
13+
});
4014

4115
app.on('window-all-closed', () => {
4216
if (process.platform !== 'darwin') {
@@ -46,6 +20,7 @@ app.on('window-all-closed', () => {
4620

4721
app.on('activate', () => {
4822
if (mainWindow === null) {
49-
createWindow();
23+
const mainWindow = createWindow();
24+
createMenu(mainWindow);
5025
}
5126
});

menu.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { app, Menu, dialog } = require('electron');
2+
3+
function createMenu(mainWindow) {
4+
const isMac = process.platform === 'darwin';
5+
6+
const template = [
7+
// File menu
8+
{
9+
label: 'File',
10+
submenu: [
11+
{
12+
label: 'Exit',
13+
accelerator: isMac ? 'CmdOrCtrl+Q' : 'Alt+F4',
14+
click() {
15+
app.quit();
16+
},
17+
},
18+
],
19+
},
20+
// View menu
21+
{
22+
label: 'View',
23+
submenu: [
24+
{ role: 'reload' },
25+
{ role: 'forcereload' },
26+
{ role: 'togglefullscreen' },
27+
],
28+
},
29+
// Help menu
30+
{
31+
label: 'About',
32+
click() {
33+
dialog.showMessageBox(mainWindow, {
34+
title: 'About',
35+
message: 'Unofficial Desktop for Misskey.ID\n\nMade with love by https://github.com/troke12',
36+
buttons: ['OK'],
37+
});
38+
},
39+
},
40+
];
41+
42+
const menu = Menu.buildFromTemplate(template);
43+
Menu.setApplicationMenu(menu);
44+
}
45+
46+
module.exports = createMenu;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "misskey-desktop",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Misskey Desktop unofficial desktop client",
55
"main": "main.js",
66
"scripts": {

preload.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { ipcRenderer } = require('electron');
2+
3+
document.addEventListener('click', (event) => {
4+
let target = event.target;
5+
6+
while (target && target.tagName !== 'A') {
7+
target = target.parentNode;
8+
}
9+
10+
if (target && target.getAttribute('target') === '_blank') {
11+
event.preventDefault();
12+
const url = target.href;
13+
ipcRenderer.send('open-external-link', url);
14+
}
15+
});

window.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { BrowserWindow, nativeImage } = require('electron');
2+
const path = require('path');
3+
4+
let mainWindow; // Change to a regular variable instead of a constant
5+
6+
function createWindow() {
7+
const iconPath = path.join(__dirname, 'assets/icon.png');
8+
9+
mainWindow = new BrowserWindow({
10+
width: 800,
11+
height: 600,
12+
autoHideMenuBar: true, // Hide the default menu bar
13+
icon: nativeImage.createFromPath(iconPath),
14+
webPreferences: {
15+
preload: path.join(__dirname, 'preload.js'), // Load the preload script
16+
},
17+
});
18+
19+
mainWindow.loadURL('https://misskey.id');
20+
21+
mainWindow.webContents.on('new-window', (event, url, frameName, disposition, options) => {
22+
event.preventDefault(); // Prevent opening the link in a new window
23+
24+
const isExternalLink = !url.startsWith('file://') && !url.startsWith('http://') && !url.startsWith('https://');
25+
26+
if (isExternalLink) {
27+
// Send a message to the renderer process indicating an external link is clicked
28+
mainWindow.webContents.send('external-link-clicked', url);
29+
} else {
30+
//console.log('Navigating to internal link:', url);
31+
mainWindow.loadURL(url); // Handle other types of links within the Electron app
32+
}
33+
});
34+
35+
mainWindow.on('closed', () => {
36+
mainWindow = null;
37+
});
38+
39+
return mainWindow;
40+
}
41+
42+
module.exports = createWindow;

0 commit comments

Comments
 (0)