Skip to content

Instantly share code, notes, and snippets.

@boljen
Last active May 24, 2019 14:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boljen/ce879f0a8260b67bf42b65554673115b to your computer and use it in GitHub Desktop.
Save boljen/ce879f0a8260b67bf42b65554673115b to your computer and use it in GitHub Desktop.
Webview
See https://github.com/electron/electron/issues/7375
Copyright (c) 2017 Christophe Bol
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
import { remote } from "electron";
import * as React from "react";
const win = remote.getCurrentWindow();
export class Webview extends React.Component<{ url: string }, {}> {
private _webview: Electron.WebviewTag;
private _mount: HTMLDivElement;
public _onRef = (mount: HTMLDivElement) => {
this._mount = mount;
this._webview = document.createElement("webview");
this._webview.src = this.props.url;
mount.appendChild(this._webview);
this._webview.addEventListener('dom-ready', this.resize);
this._webview.addEventListener('did-start-loading', this.resize);
this._webview.addEventListener('did-stop-loading', this.resize);
win.on('resize', this.resize);
}
public componentWillUnmount() {
win.removeListener("resize", this.resize);
}
public render() {
return <div className="webview-wrapper" ref={this._onRef} />;
}
private resize = () => {
win.webContents.getZoomFactor((factor) => {
this._webview.style.minWidth = (this._mount.clientWidth * factor) + "px";
this._webview.style.minHeight = (this._mount.clientHeight * factor) + "px";
});
}
}
.webview-wrapper {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
position: relative;
overflow: hidden;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment