optimizations
optimizations

file:a/README.md -> file:b/README.md
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 # node-red-contrib-web-watch
 
-A <a href="http://nodered.org" target="_new">Node-RED</a> node that watches for changes on webpages.
+A <a href="http://nodered.org" target="_blank">Node-RED</a> node that watches for changes on webpages.
 
 ## Install
 

--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
     "name": "node-red-contrib-web-watch",
-    "version": "0.0.2",
+    "version": "0.0.3",
     "description": "A Node-RED node that watches for changes on webpages",
     "repository": {
         "type": "git",

--- a/web-watch/web-watch.html
+++ b/web-watch/web-watch.html
@@ -4,7 +4,7 @@
         <input type="text" id="node-input-name" placeholder="Name">
     </div>
     <div class="form-row">
-        <label for="node-input-url"><i class="fa fa-dot-circle-o"></i> Url</label>
+        <label for="node-input-url"><i class="fa fa-link"></i> Url</label>
         <input type="text" id="node-input-url" placeholder="http://www.google.com">
     </div>
     <div class="form-row">
@@ -18,7 +18,7 @@
         <input type="text" id="node-input-name" placeholder="Name">
     </div>
     <div class="form-row">
-        <label for="node-input-url"><i class="fa fa-dot-circle-o"></i> Url</label>
+        <label for="node-input-url"><i class="fa fa-link"></i> Url</label>
         <input type="text" id="node-input-url" placeholder="http://www.google.com">
     </div>
     <div class="form-row">
@@ -41,7 +41,7 @@
         color: "#3c8dbc",
         defaults: {
             name: {value: ""},
-            url: {value: ""},
+            url: {value: "", required: true},
             interval: {value: "5", required: true, validate: RED.validators.number()}
         },
         inputs: 0,
@@ -60,7 +60,7 @@
         color: "#3c8dbc",
         defaults: {
             name: {value: ""},
-            url: {value: ""},
+            url: {value: "", required: true},
             interval: {value: "5", required: true, validate: RED.validators.number()}
         },
         inputs: 1,

--- a/web-watch/web-watch.js
+++ b/web-watch/web-watch.js
@@ -2,67 +2,116 @@
     "use strict";
     var request = require('request');
 
-    function WebpageWatch(n) {
-        RED.nodes.createNode(this,n);
-        this.url = n.url;
-        this.html = n.html;
-        this.interval = parseInt(n.interval);
-        var node = this,
-            intervalId,
-            html;
+    function WebpageWatch(config) {
+        RED.nodes.createNode(this, config);
+        var node = this;
 
-        node.log("Web watch URL " + this.url);
-
-        var msg = {};
-        var url = msg.url || node.url;
-
-        if(msg.hasOwnProperty('payload')) {
-            msg._payload = msg.payload;
-        }
-        if(msg.hasOwnProperty('topic')) {
-            msg._topic = msg.topic;
-        }
-        msg.payload = false;
-        msg.topic = url;
-
-        if (!url) {
-            node.warn('No url is specified. Either specify in node configuration.');
+        if (!config.url) {
+            node.warn('Web watch URL: No URL is specified. Please specify in node configuration.');
             return;
         }
 
+        config.interval = parseInt(config.interval);
+        var intervalId = null;
+        var cacheHtml = null;
+        var msg = {
+            payload : {
+                headers: null,
+                url: config.url,
+                body: null
+            }
+        };
+
+        node.log("Web watch URL (" + config.interval + " seconds): " + config.url);
+
         intervalId = setInterval(function() {
-            request(node.url, function (error, response, body) {
+            request(config.url, function (error, response, body) {
                 if (!error && response.statusCode == 200) {
-                    if (html == undefined) {
-                        html = body;
-                    } else if (html != "" && html != body) {
-                        html = body;
-                        msg.payload = body;
+                    if (cacheHtml == undefined) {
+                        cacheHtml = body;
+                    } else if (cacheHtml != "" && cacheHtml != body) {
+                        cacheHtml = body;
+                        msg.payload.headers = response.headers;
+                        msg.payload.body = body;
                         node.send(msg);
                     }
                 }
             });
-        }, node.interval * 1000);
+        }, config.interval * 1000);
 
-        this.on('close', function() {
-            clearInterval(intervalId);
+        node.on('close', function() {
+            if (intervalId != null) {
+                clearInterval(intervalId);
+            }
         });
     }
     RED.nodes.registerType("Web watch", WebpageWatch);
 
-    function WebpageWatchIn(n) {
-        RED.nodes.createNode(this,n);
-        this.url = n.url;
-        this.html = n.html;
-        this.interval = parseInt(n.interval);
-        var node = this,
-            intervalId,
-            html;
+    function WebpageWatchIn(config) {
+        RED.nodes.createNode(this, config);
+        var node = this;
 
-        node.log("Web watch in URL " + this.url);
+        config.interval = parseInt(config.interval);
+        var intervalId = null;
+        var cacheHtml = null;
+        var msg = {
+            headers: null,
+            url: config.url,
+            payload: null
+        };
 
-        this.on("input", function (msg) {
-            var url = msg.url || node.url;
+        function runInterval(msg) {
+            if (intervalId != null) {
+                clearInterval(intervalId);
+            }
+
+            intervalId = setInterval(function() {
+                request(config.url, function (error, response, body) {
+                    if (!error && response.statusCode == 200) {
+                        if (cacheHtml == undefined) {
+                            cacheHtml = body;
+                        } else if (cacheHtml != "" && cacheHtml != body) {
+                            cacheHtml = body;
+                            msg.headers = response.headers;
+                            msg.payload = body;
+                            node.send(msg);
+                        }
+                    }
+                });
+            }, config.interval * 1000);
+        }
+
+        if (config.url) {
+            node.log("Web watch in URL (" + config.interval + " seconds): " + config.url);
+            runInterval(msg);
+        }
+
+        node.on("input", function (msg) {
+            // try json parse on msg.payload first
+            try {
+                var _msg = JSON.parse(msg.payload);
+                if (typeof _msg === 'object') {
+                    if(_msg.hasOwnProperty('url')) {
+                        msg.url = _msg.url;
+                    }
+                    if(_msg.hasOwnProperty('interval')) {
+                        msg.interval = _msg.interval;
+                    }
+                }
+            }
+            catch (ex){
+                console.log (ex);
+            }
+
+            if (!msg.url) {
+                node.warn('Web watch in: No URL is specified. Either specify in node configuration or by passing in msg.url');
+                return;
+            }
+
+            config.url = msg.url;
+            config.interval = msg.interval || config.interval;
+
+            node.log("Web watch in URL (" + config.interval + " seconds): " + config.url);
 
             if(msg.hasOwnProperty('payload')) {
                 msg._payload = msg.payload;
@@ -70,31 +119,14 @@
             if(msg.hasOwnProperty('topic')) {
                 msg._topic = msg.topic;
             }
-            msg.payload = false;
-            msg.topic = url;
 
-            if (!url) {
-                node.warn('No url is specified. Either specify in node configuration or by passing in msg.url');
-                return;
-            }
-
-            intervalId = setInterval(function() {
-                request(node.url, function (error, response, body) {
-                    if (!error && response.statusCode == 200) {
-                        if (html == undefined) {
-                            html = body;
-                        } else if (html != "" && html != body) {
-                            html = body;
-                            msg.payload = body;
-                            node.send(msg);
-                        }
-                    }
-                });
-            }, node.interval * 1000);
+            runInterval(msg);
         });
 
-        this.on('close', function() {
-            clearInterval(intervalId);
+        node.on('close', function() {
+            if (intervalId != null) {
+                clearInterval(intervalId);
+            }
         });
     }
     RED.nodes.registerType("Web watch in", WebpageWatchIn);

comments