electron/spec-main/fixtures/apps/set-path/main.js
Cheng Zhao 9483e714c4
feat: allow customizing browser data location (#33554)
* feat: redirect Electron/Chromium cache location

* fix: network services should also use browserData

* test: browserData

* chore: no need to explicitly create dir

* feat: browserData => sessionData

* test: check existings of specific items

* docs: add background on userData and sessionData

Co-authored-by: emmanuel.kimmerlin@thomsonreuters.com <emmanuel.kimmerlin@thomsonreuters.com>
2022-05-09 10:26:57 -04:00

43 lines
1.2 KiB
JavaScript

const http = require('http');
const { app, ipcMain, BrowserWindow } = require('electron');
if (process.argv.length > 3) {
app.setPath(process.argv[2], process.argv[3]);
}
const html = `
<script>
async function main() {
localStorage.setItem('myCat', 'Tom')
const db = indexedDB.open('db-name', 1)
await new Promise(resolve => db.onsuccess = resolve)
await navigator.serviceWorker.register('sw.js', {scope: './'})
}
main().then(() => {
require('electron').ipcRenderer.send('success')
})
</script>
`;
const js = 'console.log("From service worker")';
app.once('ready', () => {
ipcMain.on('success', () => {
app.quit();
});
const server = http.createServer((request, response) => {
if (request.url === '/') {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(html);
} else if (request.url === '/sw.js') {
response.writeHead(200, { 'Content-Type': 'text/javascript' });
response.end(js);
}
}).listen(0, '127.0.0.1', () => {
const serverUrl = 'http://127.0.0.1:' + server.address().port;
const mainWindow = new BrowserWindow({ show: false, webPreferences: { webSecurity: true, nodeIntegration: true, contextIsolation: false } });
mainWindow.loadURL(serverUrl);
});
});