Created
July 11, 2025 15:07
-
-
Save panther7/feb2f90be78eab0c433c2d9eddac3a80 to your computer and use it in GitHub Desktop.
useragent sometimes different
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --> | |
| <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'"> | |
| <title>Hello World!</title> | |
| </head> | |
| <body> | |
| <h1>Hello World!</h1> | |
| We are using Node.js <span id="node-version"></span>, | |
| Chromium <span id="chrome-version"></span>, | |
| and Electron <span id="electron-version"></span>. | |
| <br> | |
| UserAgent: <div id="ua"></div> | |
| <!-- You can also require other files to run in this process --> | |
| <script src="./renderer.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { | |
| app, | |
| session, | |
| BrowserWindow, | |
| WebContentsView, | |
| } = require('electron') | |
| const path = require('node:path'); | |
| const testPartition = 'persist:test'; | |
| const testUA = 'MyCustomUA'; | |
| function createWindow () { | |
| const mainWindow = new BrowserWindow({ | |
| width: 800, | |
| height: 600, | |
| webPreferences: { | |
| preload: path.join(__dirname, 'preload.js') | |
| } | |
| }) | |
| mainWindow.loadFile('index.html') | |
| return mainWindow; | |
| } | |
| function createTab() { | |
| const tab = new WebContentsView({ | |
| webPreferences: { | |
| partition: testPartition, | |
| }, | |
| }) | |
| tab.webContents.loadURL('https://drive.google.com/file/d/1eQ7ennUEBfdGFdPMfOKClMR_ia0gQru_/view?usp=sharing'); | |
| console.log('UserAgent from webwebContents', tab.webContents.getUserAgent()); | |
| tab.webContents.executeJavaScript('navigator.userAgent').then((value) => { | |
| console.log('UserAgent from navigator:', value) | |
| }) | |
| return tab; | |
| } | |
| app.whenReady().then(() => { | |
| const ses = session.fromPartition(testPartition) | |
| ses.setUserAgent(testUA) | |
| console.log('UserAgent sets to:', testUA); | |
| ses.webRequest.onBeforeSendHeaders({ urls: ['<all_urls>'] }, (details, callback) => { | |
| if (testUA !== details.requestHeaders['User-Agent']) { | |
| console.log('UserAgent is different!!!', details.requestHeaders['User-Agent']) | |
| } | |
| callback({ cancel: false }); | |
| }); | |
| createWindow() | |
| createTab() | |
| app.on('activate', function () { | |
| if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
| }) | |
| }) | |
| app.on('window-all-closed', function () { | |
| if (process.platform !== 'darwin') app.quit() | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "fluttering-pump-interlay-2138p", | |
| "productName": "fluttering-pump-interlay-2138p", | |
| "description": "My Electron application description", | |
| "keywords": [], | |
| "main": "./main.js", | |
| "version": "1.0.0", | |
| "author": "filip.mosner", | |
| "scripts": { | |
| "start": "electron ." | |
| }, | |
| "dependencies": {}, | |
| "devDependencies": { | |
| "electron": "37.2.1" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * The preload script runs before `index.html` is loaded | |
| * in the renderer. It has access to web APIs as well as | |
| * Electron's renderer process modules and some polyfilled | |
| * Node.js functions. | |
| * | |
| * https://www.electronjs.org/docs/latest/tutorial/sandbox | |
| */ | |
| window.addEventListener('DOMContentLoaded', () => { | |
| const replaceText = (selector, text) => { | |
| const element = document.getElementById(selector) | |
| if (element) element.innerText = text | |
| } | |
| for (const type of ['chrome', 'node', 'electron']) { | |
| replaceText(`${type}-version`, process.versions[type]) | |
| } | |
| document.getElementById('ua').textContent = navigator.userAgent | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This file is loaded via the <script> tag in the index.html file and will | |
| * be executed in the renderer process for that window. No Node.js APIs are | |
| * available in this process because `nodeIntegration` is turned off and | |
| * `contextIsolation` is turned on. Use the contextBridge API in `preload.js` | |
| * to expose Node.js functionality from the main process. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment