crre un onglet suivant presedent pour un navigateur delectron js
- User
crre un onglet suivant presedent pour un navigateur delectron js
- Assistant
crre un onglet suivant presedent pour un navigateur delectron js
Pour créer un nouvel onglet dans un navigateur Electron, vous devez d'abord configurer votre projet Electron. Ensuite, vous pouvez utiliser la bibliothèque BrowserWindow d'Electron pour ouvrir de nouvelles fenêtres ou onglets.
Voici un exemple simple de code qui montre comment créer un nouvel onglet dans une application Electron :
Installer Electron : Si ce n'est pas déjà fait, assurez-vous d'avoir Node.js installé, puis créez un nouveau projet et installez Electron :
mkdir mon-app-electron
cd mon-app-electron
npm init -y
npm install electron --save-dev
Structure de fichiers : Créez les fichiers suivants :
main.js : Le fichier principal pour Electron.index.html : Le fichier HTML pour l'interface utilisateur.Contenu de main.js :
const { app, BrowserWindow, ipcMain } = require('electron');
let mainWindow;
const windows = [];
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
mainWindow.loadFile('index.html');
mainWindow.on('closed', function () {
mainWindow = null;
});
}
app.on('ready', createWindow);
ipcMain.on('open-new-tab', (event, url) => {
const newWindow = new BrowserWindow({
width: 800,
height: 600,
parent: mainWindow, // Définir une fenêtre parente pour un onglet
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
newWindow.loadURL(url);
windows.push(newWindow);
newWindow.on('closed', () => {
const index = windows.indexOf(newWindow);
if (index !== -1) {
windows.splice(index, 1);
}
});
});
app.on('window-all-closed', () => {
app.quit();
});
Contenu de index.html :
<!DOCTYPE html>
<html>
<head>
<title>Mon App Electron</title>
</head>
<body>
<h1>Bienvenue dans mon app Electron</h1>
<button id="new-tab">Ouvrir un nouvel onglet</button>
<script>
const { ipcRenderer } = require('electron');
document.getElementById('new-tab').addEventListener('click', () => {
const url = 'https://www.example.com'; // Spécifiez l'URL que vous voulez ouvrir
ipcRenderer.send('open-new-tab', url);
});
</script>
</body>
</html>
Exécutez votre application : Ajoutez ce script à votre package.json pour démarrer votre application Electron.
"scripts": {
"start": "electron ."
}
Ensuite, exécutez :
npm start
BrowserWindow : Utilisé pour créer une nouvelle fenêtre (similaire à un nouvel onglet dans un navigateur).Lorsque vous cliquez sur le bouton "Ouvrir un nouvel onglet", une nouvelle fenêtre s'ouvrira avec l'URL spécifiée. Vous pouvez personnaliser ce comportement selon vos besoins.
Not yet included in a published release.