Skip to content

Instantly share code, notes, and snippets.

@bayleedev
Created April 21, 2016 21:06
Show Gist options
  • Save bayleedev/1836954796536ffe684e151b39b38517 to your computer and use it in GitHub Desktop.
Save bayleedev/1836954796536ffe684e151b39b38517 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Electron Boilerplate</title>
<script src="helpers/context_menu.js"></script>
<script src="helpers/external_links.js"></script>
<style>
body { margin: 0 }
</style>
</head>
<body>
<div id="container">
</div>
<script>
const { remote } = require('electron')
const electronScreen = require('electron').screen
const maxHeight = 500
// Have the content grow
var container = document.getElementById('container')
setInterval(function() {
var newNode = document.createElement('div');
newNode.innerHTML = new Date()
container.appendChild(newNode);
// updateHeight() // push change events, if possible
}, 100);
// pull change events
setInterval(function() {
updateHeight()
}, 100);
// Update the height of the browser window
function updateHeight() {
var currentWindow = remote.getCurrentWindow()
const { height, width, y } = currentWindow.getBounds()
var screenHeight = electronScreen.getPrimaryDisplay().workAreaSize.height
var newHeight = Math.min(
maxHeight,
container.offsetHeight,
screenHeight - y
)
console.log({newHeight, height})
if (newHeight !== height) {
currentWindow.setSize(width, newHeight)
}
}
</script>
</body>
</html>
@bayleedev
Copy link
Author

  mainWindow.webContents.on('did-finish-load', function() {
    setInterval(() => {
      mainWindow.webContents.executeJavaScript('document.body.children[0].offsetHeight', (mainContentHeight) => {
        const { height, width, y } = mainWindow.getBounds()
        const maxHeight = 500
        const screenHeight = electronScreen.getPrimaryDisplay().workAreaSize.height
        const newHeight = Math.min(
            maxHeight,
            mainContentHeight,
            screenHeight - y
        )
        console.log({newHeight, height})
        if (newHeight !== height) {
            mainWindow.setSize(width, newHeight)
        }
      });
    }, 100);
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment