-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
86 lines (79 loc) · 2.43 KB
/
app.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
'use strict';
var fs = require('fs.promised');
var os = require('os');
var rp = require('request-promise');
var Promise = require('bluebird');
var rimraf = Promise.promisify(require('rimraf'));
var sqlite3 = Promise.promisifyAll(require('sqlite3').verbose());
var wallpaper = require('wallpaper');
var schedule = require('node-schedule');
// Get the correct Momentum image based on the os platform
var platform = os.platform();
var localStorageFile = os.homedir();
if (platform === 'darwin') {
localStorageFile += '/Library/Application Support/Google/Chrome/Default/Local Storage/';
} else if (platform === 'win32') {
localStorageFile += '\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Local Storage\\';
} else if (platform === 'linux') {
localStorageFile += '/.config/google-chrome/Default/Local Storage/';
}
localStorageFile += 'chrome-extension_laookkfknpbbblfpciffpaejjkokdgca_0.localstorage';
/**
* Main function that changes the desktop background
*/
function start() {
var localStorage = new sqlite3.Database(localStorageFile);
var table = 'ItemTable';
var key = `momentum-background-${getTodayDate()}`;
var value = 'value';
var selectQuery = `SELECT ${value} FROM ${table} where key="${key}"`;
var imageName = `image-${getRandomInt(0, 9999999)}.jpeg`;
return localStorage.allAsync(selectQuery)
.then(res => {
var backgroundInfo = res[0].value.toString('utf16le');
var background = JSON.parse(backgroundInfo);
return background.filename;
})
.then(filename => {
return rp({
uri: filename,
encoding: 'binary'
});
})
.then(image => {
return fs.writeFile(imageName, image, 'binary');
})
.then(() => {
return wallpaper.set(imageName);
})
.then(() => {
console.log('Desktop background changed!');
return rimraf(imageName);
});
}
/**
* Generate random integer to use as part of the background image name
*/
function getRandomInt(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
/**
* Get today's date in the format of YYYY-MM-DD
* @returns String
*/
function getTodayDate() {
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
month = month < 10 ? `0${month}` : month;
var date = d.getDate();
return `${year}-${month}-${date}`;
}
if (process.env.NODE_ENV === 'test') {
start();
} else {
start();
schedule.scheduleJob({hour: 0, minute: 0}, () => {
start();
});
}