Implement counter which closes #33, thanks to @jfouchard

This commit is contained in:
Jia Hao 2016-01-22 11:35:05 +08:00
parent 4a8ab13622
commit 59e9f6e104
5 changed files with 39 additions and 7 deletions

View File

@ -136,6 +136,14 @@ There is no known way to intercept and set an event listener for a desktop notif
However, this would cause issues when the command line argument `target` is set to a external page which is not a single page app, because clicking on hyperlinks and switching pages would naturally change the `document.title`. Hence, `--badge` is an optional command argument that can be set by the user if the side effect of this workaround is understood.
#### [counter[
```
--counter
```
Use a counter that persists even with window focus for the application badge for sites that use an "(X)" format counter in the page title (i.e. GMail). Same limitations as the badge option (above).
#### [width]
```

View File

@ -71,11 +71,26 @@ app.on('ready', function () {
mainWindow.webContents.send('params', JSON.stringify(appArgs));
});
mainWindow.on('page-title-updated', function () {
if (isOSX() && !mainWindow.isFocused()) {
if (appArgs.badge || appArgs.counter) {
mainWindow.on('page-title-updated', function () {
if (!isOSX() || mainWindow.isFocused()) {
return;
}
if (appArgs.counter) {
var itemCountRegex = /[\(](\d*?)[\)]/;
var match = itemCountRegex.exec(mainWindow.getTitle());
console.log(mainWindow.getTitle(), match);
if (match) {
app.dock.setBadge(match[1]);
}
return;
}
app.dock.setBadge('●');
}
});
});
}
mainWindow.webContents.on('new-window', function (event, urlToGo) {
if (linkIsInternal(appArgs.targetUrl, urlToGo)) {
@ -88,7 +103,10 @@ app.on('ready', function () {
mainWindow.loadURL(appArgs.targetUrl);
// if the window is focused, clear the badge
mainWindow.on('focus', function () {
if (isOSX()) {
if (!isOSX()) {
return;
}
if (appArgs.badge || appArgs.counter) {
app.dock.setBadge('');
}
});

View File

@ -31,7 +31,7 @@ function buildApp(options, callback) {
async.waterfall([
callback => {
copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.badge, options.width, options.height, options.userAgent, callback);
copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.badge, options.counter, options.width, options.height, options.userAgent, callback);
},
(tempDir, callback) => {
@ -61,12 +61,13 @@ function buildApp(options, callback) {
* @param {string} name
* @param {string} targetURL
* @param {boolean} badge
* @param {boolean} counter
* @param {number} width
* @param {number} height
* @param {string} userAgent
* @param {tempDirCallback} callback
*/
function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, width, height, userAgent, callback) {
function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, counter, width, height, userAgent, callback) {
const loadedPackageJson = packageJson;
copy(srcAppDir, tempDir, function(error) {
if (error) {
@ -79,6 +80,7 @@ function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, width, h
name: name,
targetUrl: targetURL,
badge: badge,
counter: counter,
width: width,
height: height,
userAgent: userAgent,

View File

@ -24,6 +24,7 @@ function main(program) {
program.conceal,
program.icon,
program.badge,
program.counter,
program.width,
program.height,
program.userAgent,
@ -59,6 +60,7 @@ if (require.main === module) {
.option('-o, --overwrite', 'if output directory for a platform already exists, replaces it rather than skipping it, defaults to false')
.option('-c, --conceal', 'packages the source code within your app into an archive, defaults to false, see http://electron.atom.io/docs/v0.36.0/tutorial/application-packaging/')
.option('-b, --badge', 'if the target app should show badges in the dock on receipt of desktop notifications (OSX only), defaults to false')
.option('--counter', 'if the target app should use a persistant counter badge in the dock (OSX only), defaults to false')
.option('-i, --icon <value>', 'the icon file to use as the icon for the app (should be a .icns file on OSX)')
.option('-w, --width <value>', 'set window width, defaults to 1280px', parseInt)
.option('-h, --height <value>', 'set window height, defaults to 800px', parseInt)

View File

@ -20,6 +20,7 @@ function optionsFactory(name,
conceal = false,
icon,
badge = false,
counter = false,
width = 1280,
height = 800,
userAgent,
@ -59,6 +60,7 @@ function optionsFactory(name,
// app configuration
badge: badge,
counter: counter,
width: width,
height: height,
userAgent: userAgent