-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
126 lines (115 loc) · 3.45 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* eslint no-console: 0 */
'use strict';
const {app, BrowserWindow, Menu} = require('electron');
const noble = require('noble');
const metadata = require('./metadata.js');
const urldecode = require('./urldecode.js');
let mainWindow = null,
counter = 0;
// Quit when all windows are closed.
app.on('window-all-closed', () => {
app.quit();
});
noble.on('scanStart', () => {
console.log('Scan started...');
mainWindow.webContents.send('status', 'Scanning for Physical Web beacons. Press <span class="key" aria-label="command">⌘</span> + <span class="key">T</span> to stop.', true);
counter = 0;
app.dock.setBadge('');
});
noble.on('scanStop', () => {
console.log('Scan stopped...');
mainWindow.webContents.send('status', 'Scanning stopped. Press <span class="key" aria-label="command">⌘</span> + <span class="key">S</span> to restart scan.');
});
noble.on('discover', peripheral => {
let serviceData = peripheral.advertisement.serviceData;
if (serviceData && serviceData.length) {
let objects = [];
for (let i in serviceData) {
// check if Eddystone-URL
if (serviceData[i].data.toString('hex').substr(0, 2) === '10') {
let url = urldecode(serviceData[i].data.toString('hex'));
objects.push({'url': url});
}
}
if (objects.length) {
metadata(objects, Menu.getApplicationMenu().items[1].submenu.items[0].checked, message => {
mainWindow.webContents.send('url', message);
if (!mainWindow.isFocused()) {
counter += 1;
app.dock.setBadge('' + counter + '');
}
});
}
}
});
app.on('ready', () => {
const menuTemplate = [
{
'label': 'Physical Web Scan',
'submenu': [
{
'label': 'Scan for beacons',
'accelerator': 'Command+S',
'click': () => {
if (noble.state === 'poweredOn') {
noble.startScanning(['feaa']);
} else {
mainWindow.webContents.send('status', 'Bluetooth not ready or turned on. Press <span class="key" aria-label="command">⌘</span> + <span class="key">S</span> to retry.');
}
}
},
{
'label': 'Stop scanning',
'accelerator': 'Command+T',
'click': () => {
noble.stopScanning();
}
},
{
'label': 'Quit',
'accelerator': 'Command+Q',
'click': () => { app.quit(); }
}
]
},
{
'label': 'Developer settings',
'submenu': [
{
'label': 'Bypass proxy service',
'type': 'checkbox',
'checked': false,
'click': item => {
if (item.checked && noble.state === 'poweredOn') {
noble.startScanning(['feaa']);
}
}
}
]
}
];
let menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
mainWindow = new BrowserWindow({'width': 480, 'height': 600});
mainWindow.loadURL('file://' + __dirname + '/index.html');
mainWindow.on('closed', () => {
mainWindow = null;
});
mainWindow.webContents.on('did-finish-load', () => {
console.log(noble.state);
let startScan = () => {
setTimeout(() => {
if (noble.state === 'poweredOn') {
noble.startScanning(['feaa']);
} else {
startScan();
}
}, 300);
};
startScan();
});
});
app.on('browser-window-focus', () => {
counter = 0;
app.dock.setBadge('');
});