feat: electron building

This commit is contained in:
SethBurkart123
2024-12-05 18:09:06 +11:00
parent f1512ba6e1
commit bb388ab000
6 changed files with 354 additions and 405 deletions
+88
View File
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html>
<head>
<title>BetterSEQTA Settings</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
padding: 20px;
max-width: 600px;
margin: 0 auto;
background: #f5f5f5;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
color: #333;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
}
input[type="url"] {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
margin-bottom: 10px;
}
button {
background: #4F46E5;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background: #4338CA;
}
</style>
</head>
<body>
<div class="container">
<h1>BetterSEQTA Settings</h1>
<div class="form-group">
<label for="seqtaUrl">SEQTA Website URL</label>
<input type="url" id="seqtaUrl" placeholder="https://your-school.seqta.com.au" required>
<button onclick="saveSettings()">Save Settings</button>
</div>
</div>
<script>
// Access electron APIs through the contextBridge
const electron = window.require('electron');
const { ipcRenderer } = electron;
const Store = window.require('electron-store');
const store = new Store();
// Load saved URL on page load
window.addEventListener('DOMContentLoaded', () => {
console.log('Loading saved URL...');
const savedUrl = store.get('seqtaUrl') || '';
console.log('Saved URL:', savedUrl);
document.getElementById('seqtaUrl').value = savedUrl;
});
// Save settings
function saveSettings() {
const url = document.getElementById('seqtaUrl').value;
console.log('Saving URL:', url);
if (url) {
ipcRenderer.send('set-seqta-url', url);
}
}
</script>
</body>
</html>
+207
View File
@@ -0,0 +1,207 @@
import { app, BrowserWindow, ipcMain, session } from 'electron';
import path from 'path';
import { fileURLToPath } from 'url';
import Store from 'electron-store';
// Fix for __dirname in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const store = new Store();
let mainWindow = null;
let settingsWindow = null;
// CSS to inject
const customCSS = `
#alertBar {
display: none !important;
}
`;
// Get the correct path for the extension based on whether we're in development or production
function getExtensionPath() {
if (app.isPackaged) {
// In production, the extension is in the resources directory
return path.join(process.resourcesPath, 'chrome-extension');
} else {
// In development, the extension is in the dist directory
return path.join(__dirname, '..', 'dist', 'chrome');
}
}
// Load the Chrome extension
async function loadExtension() {
try {
const extensionPath = getExtensionPath();
console.log('Loading extension from:', extensionPath);
await session.defaultSession.loadExtension(extensionPath, {
allowFileAccess: true
});
console.log('Extension loaded successfully!');
} catch (err) {
console.error('Failed to load extension:', err);
}
}
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
// Performance optimizations
backgroundThrottling: false,
enableWebSQL: false,
webgl: false,
offscreen: false
},
// Performance optimizations
show: false, // Don't show until ready
backgroundColor: '#ffffff'
});
const seqtaUrl = store.get('seqtaUrl');
// Optimize page loading
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
// Open external links in browser instead of new electron window
if (url.startsWith('http')) {
require('electron').shell.openExternal(url);
return { action: 'deny' };
}
return { action: 'allow' };
});
// Inject CSS when the page loads
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.insertCSS(customCSS).catch(err => {
console.error('Failed to inject CSS:', err);
});
});
// Only show window when it's ready
mainWindow.once('ready-to-show', () => {
mainWindow.show();
mainWindow.focus();
});
if (seqtaUrl) {
mainWindow.loadURL(seqtaUrl, {
// Performance optimizations for page loading
httpReferrer: seqtaUrl,
userAgent: 'Chrome',
cache: 'force-cache'
}).then(() => {
// Re-inject CSS after URL change
mainWindow.webContents.insertCSS(customCSS).catch(err => {
console.error('Failed to inject CSS after URL change:', err);
});
});
} else {
createSettingsWindow();
}
// Optimize memory usage
mainWindow.on('minimize', () => {
if (process.platform === 'darwin') return; // Skip for macOS
mainWindow.webContents.setBackgroundThrottling(true);
});
mainWindow.on('restore', () => {
mainWindow.webContents.setBackgroundThrottling(false);
});
// Only enable DevTools in development
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools();
}
}
function createSettingsWindow() {
if (settingsWindow) {
settingsWindow.focus();
return;
}
settingsWindow = new BrowserWindow({
width: 600,
height: 400,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
// Performance optimizations
backgroundThrottling: false,
enableWebSQL: false,
webgl: false
},
show: false,
backgroundColor: '#ffffff'
});
const settingsPath = path.join(__dirname, 'index.html');
settingsWindow.loadFile(settingsPath);
settingsWindow.once('ready-to-show', () => {
settingsWindow.show();
settingsWindow.focus();
});
// Only enable DevTools in development
if (process.env.NODE_ENV === 'development') {
settingsWindow.webContents.openDevTools();
}
settingsWindow.on('closed', () => {
settingsWindow = null;
});
}
// Performance optimization: Disable hardware acceleration if running on low-end device
if (process.platform !== 'darwin') { // Skip for macOS
app.disableHardwareAcceleration();
}
// Performance optimization: Disable smooth scrolling
app.commandLine.appendSwitch('disable-smooth-scrolling');
// Wait for app to be ready before creating windows
app.whenReady().then(async () => {
await loadExtension();
createMainWindow();
});
// Performance optimization: Quit immediately instead of gracefully
app.on('window-all-closed', () => {
app.quit();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
}
});
// Handle setting the SEQTA URL
ipcMain.on('set-seqta-url', (event, url) => {
console.log('Setting SEQTA URL:', url);
store.set('seqtaUrl', url);
if (mainWindow) {
mainWindow.loadURL(url, {
httpReferrer: url,
userAgent: 'Chrome',
cache: 'force-cache'
}).then(() => {
// Re-inject CSS after URL change
mainWindow.webContents.insertCSS(customCSS).catch(err => {
console.error('Failed to inject CSS after URL change:', err);
});
});
} else {
createMainWindow();
}
if (settingsWindow) {
settingsWindow.close();
}
});