Copying online-offline-events.md file
This commit is contained in:
parent
4392f1d77c
commit
b6cfa6f967
1 changed files with 82 additions and 0 deletions
82
docs-translations/pt-BR/tutorial/online-offline-events.md
Normal file
82
docs-translations/pt-BR/tutorial/online-offline-events.md
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
# Online/Offline Event Detection
|
||||||
|
|
||||||
|
Online and offline event detection can be implemented in the renderer process
|
||||||
|
using standard HTML5 APIs, as shown in the following example.
|
||||||
|
|
||||||
|
_main.js_
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var app = require('app');
|
||||||
|
var BrowserWindow = require('browser-window');
|
||||||
|
var onlineStatusWindow;
|
||||||
|
|
||||||
|
app.on('ready', function() {
|
||||||
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false });
|
||||||
|
onlineStatusWindow.loadUrl('file://' + __dirname + '/online-status.html');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
_online-status.html_
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
var alertOnlineStatus = function() {
|
||||||
|
window.alert(navigator.onLine ? 'online' : 'offline');
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('online', alertOnlineStatus);
|
||||||
|
window.addEventListener('offline', alertOnlineStatus);
|
||||||
|
|
||||||
|
alertOnlineStatus();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
There may be instances where you want to respond to these events in the
|
||||||
|
main process as well. The main process however does not have a
|
||||||
|
`navigator` object and thus cannot detect these events directly. Using
|
||||||
|
Electron's inter-process communication utilities, the events can be forwarded
|
||||||
|
to the main process and handled as needed, as shown in the following example.
|
||||||
|
|
||||||
|
_main.js_
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var app = require('app');
|
||||||
|
var ipc = require('ipc');
|
||||||
|
var BrowserWindow = require('browser-window');
|
||||||
|
var onlineStatusWindow;
|
||||||
|
|
||||||
|
app.on('ready', function() {
|
||||||
|
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false });
|
||||||
|
onlineStatusWindow.loadUrl('file://' + __dirname + '/online-status.html');
|
||||||
|
});
|
||||||
|
|
||||||
|
ipc.on('online-status-changed', function(event, status) {
|
||||||
|
console.log(status);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
_online-status.html_
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
var ipc = require('ipc');
|
||||||
|
var updateOnlineStatus = function() {
|
||||||
|
ipc.send('online-status-changed', navigator.onLine ? 'online' : 'offline');
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('online', updateOnlineStatus);
|
||||||
|
window.addEventListener('offline', updateOnlineStatus);
|
||||||
|
|
||||||
|
updateOnlineStatus();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
Loading…
Add table
Add a link
Reference in a new issue