Skip to content

Instantly share code, notes, and snippets.

@bendemboski
Created January 8, 2020 06:19
Show Gist options
  • Save bendemboski/0c4149121f21cd6d914b9f94b9118ba2 to your computer and use it in GitHub Desktop.
Save bendemboski/0c4149121f21cd6d914b9f94b9118ba2 to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<script src="test.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
const {app, protocol, BrowserWindow} = require('electron')
// canned content
const testJs = `
function testSourceMapUrls() {
var arr2 = [6, 4, -2, 8, 7];
sortAndDisplay2(arr2);
}
function sortAndDisplay2(arr) {
arr.sort();
console.log(arr);
}
//# sourceMappingURL=test.js.map
`;
const testJsMap = `{"version":3,"file":"test.js","sourceRoot":"","sources":["test.ts"],"names":[],"mappings":"AAAA;IAEI,IAAI,IAAI,GAAa,CAAC,CAAC,EAAC,CAAC,EAAC,CAAC,CAAC,EAAC,CAAC,EAAC,CAAC,CAAC,CAAC;IAClC,eAAe,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED,yBAA0B,GAAa;IAEnC,GAAG,CAAC,IAAI,EAAE,CAAC;IACX,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACrB,CAAC"}`;
const testTs = `
function testSourceMapUrls()
{
let arr2: number[] = [6,4,-2,8,7];
sortAndDisplay2(arr2);
}
function sortAndDisplay2 (arr: number[])
{
arr.sort();
console.log(arr);
}`;
// register custom scheme
protocol.registerSchemesAsPrivileged([
{ scheme: 'cust', privileges: { standard: true, secure: true } }
]);
// cheesy string handler to demonstrate map file loading (or failure to)
function handler({ url }, callback) {
if (url === 'cust://index/') {
return callback({
mimeType: 'text/html',
data: require('fs').readFileSync('index.html').toString()
});
} else if (url == 'cust://index/test.js') {
callback({
mimeType: 'application/javascript',
data: testJs
});
} else if (url == 'cust://index/test.js.map') {
console.log('source map requested!');
callback({
mimeType: 'application/json',
data: testJsMap
});
} else if (url == 'cust://index/test.ts') {
callback({
mimeType: 'text/plain',
data: testTs
});
}
}
// register protocol on custom scheme
app.on('ready', () => {
protocol.registerStringProtocol('cust', handler, error => {
if (error) {
console.error('Failed to register protocol');
}
});
});
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadURL('cust://index')
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment