Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
larsbs committed Sep 11, 2018
0 parents commit 6374b31
Show file tree
Hide file tree
Showing 10 changed files with 10,656 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Webview Hangs

Sample repo to test a problem with electron webview

## Installation

Clone and install dependencies with npm:

```bash
$ git clone https://github.com/Drawbotics/webview-hangs.git
$ cd webview-hangs
$ npm i
```

## Run

Run it with:

```bash
$ npm run dev
```
50 changes: 50 additions & 0 deletions app/main/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { app, BrowserWindow } = require('electron');
const url = require('url');
const path = require('path');

const interceptRequests = require('./intercept-requests');


const IS_DEV = process.env.NODE_ENV === 'development';


let mainWindow;

function createWindow() {
mainWindow = new BrowserWindow({
width: IS_DEV ? 1600 : 1100,
height: 800,
webPreferences: {
webSecurity: false,
},
});

mainWindow.loadFile(path.resolve(__dirname, '../render/index.html'));

mainWindow.on('closed', () => {
mainWindow = null;
});

if (IS_DEV) {
mainWindow.toggleDevTools();
}

interceptRequests();
}


app.on('ready', createWindow);


app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});


app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
17 changes: 17 additions & 0 deletions app/main/intercept-requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { session } = require('electron');
const url = require('url');
const path = require('path');


module.exports = function interceptRequests() {
session.defaultSession.webRequest.onBeforeRequest((details, next) => {
const redirectURL = details.url === 'https://www.google.com/'
? `file://${url.format(path.resolve(__dirname, '../render/redirected.html'))}`
: null;
if (redirectURL == null) {
return next({ cancel: false });
}
console.log('Redirecting to:', redirectURL);
return next({ cancel: false, redirectURL })
});
}
54 changes: 54 additions & 0 deletions app/render/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webview Hangs</title>
<style type="text/css">
html, body {
height: 100%;
font-size: 14px;
}
#root {
min-height: 100%;
display: flex;
flex-direction: column;
}
.webview {
width: 100%;
height: 100%;
flex: 1;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="root">
<button id="interact">Interact</button>
<webview
id="webview"
class="webview"
src="https://www.google.com" />
</div>
<script type="text/javascript">
const path = require('path');
const url = require('url');

const localUrl = `file://${url.format(path.resolve(__dirname, 'redirected.html'))}`;
const webview = document.getElementById('webview');
const interact = document.getElementById('interact');

webview.addEventListener('dom-ready', () => {
if (webview.getURL() === localUrl) {
return;
}
webview.openDevTools();
webview.loadURL(localUrl);
console.log(webview.getURL());
});

interact.addEventListener('click', () => {
console.log(webview.getURL());
});
</script>
</body>
</html>
9 changes: 9 additions & 0 deletions app/render/redirected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Redirected</h1>
</body>
</html>
3 changes: 3 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"watch": [ "app/main" ]
}
Loading

0 comments on commit 6374b31

Please sign in to comment.