Summary of my Goal
I want to load a third party website into Electron and manipulate its CSS. I want the CSS changes I made to persist. I want to bundle the code as an app and the CSS changes to persist when the user runs the app. The website I want to embed uses google for authentication.
- The First Approach
The first approach I tried was to change the CSS in the browser via the Google Chrome "Overrides" feature. This did not work as the changes do not persist to users when they open the app on their machines.
The second approach is documented in this question I posted: In Electron (Forge) when nodeIntegrationInWorker: true, embedding gmail shows a blank screen
- The Second Approach
Note - In my code below I've used gmail.com as the example website to load. It creates the same problem.
index.js
const { app, BrowserWindow } = require('electron');
const path = require('path');
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
// width: 1000,
// height: 800,
// resizable: false,
// frame:false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegrationInWorker: true,
contextIsolation: true
},
});
;
mainWindow.loadURL("https://gmail.com")
mainWindow.webContents.openDevTools();
};
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
preload.js
function changeCSSonLoad(){
let ele1 = document.querySelector('123-some large class list > div > div.flex.h-full.flex-1.flex-col.md\\:pl-\\)');
let ele2 = document.querySelector('456-some large class list > div > div.flex.h-full.flex-1.flex-col.md\\:pl-\\)');
let ele3 = document.querySelector('789-some large class list > div > div.flex.h-full.flex-1.flex-col.md\\:pl-\\)');
ele2.style.display = "none";
ele3.style.display = "none";
ele4.style.display = "none";
console.log("IT WERRRRKS")
}
setTimeout(function(){ changeCSSonLoad(); }, 1000);
The code above works fine after the user is logged in. If the user is not logged in and they are prompted to do so via Google, the screen turns blank white (the connection is severed). It is exactly what happens in this Github post: https://github.com/electron/electron/issues/8950
The solution in the previous link is to remove nodeIntegrationInWorker: true
from my code. If I do that the CSS changes no longer work.
I've been online reading about preload.js and from the looks of it I am not using it correctly. This post talks about the "Proper" way to use preload.js
In summary,
- I want to load a third party website into Electron and manipulate its CSS. The website I want to load uses Google for authentication. I have created a version that works except when users are prompted to authenticate.
- I am not sure if I am using preload.js the right way and by proxy I am not sure if I am going about solving this entire problem the right way in Electron.