-
Notifications
You must be signed in to change notification settings - Fork 10
/
node_helper.js
37 lines (30 loc) · 903 Bytes
/
node_helper.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
const fetch = require("node-fetch");
var NodeHelper = require("node_helper");
var cheerio = require("cheerio");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper: " + this.name);
},
socketNotificationReceived: function(notification, payload) {
var self = this;
console.log("Dilbert -> Notification: " + notification + " Payload: " + payload);
if(notification === "GET_COMIC") {
var url = "http://dilbert.com";
console.log('-> node request -> ' + url);
fetch(url)
.then(response => response.text())
.then(body => {
var $ = cheerio.load(body);
var src = $(".img-comic").attr('src');
console.log('Dilbert img -> ' + src);
self.sendSocketNotification("COMIC", {
img : src
});
})
.catch((error) => {
console.log('Dilbert Fetch Error -> ' + error);
});
return;
}
},
});