added HTTP authentication, custom headers, return content as html/json master
added HTTP authentication, custom headers, return content as html/json

--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
     "name": "node-red-contrib-web-watch",
-    "version": "0.0.4",
+    "version": "0.0.5",
     "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
@@ -11,6 +11,30 @@
         <label for="node-input-interval"><i class="fa fa-clock-o"></i> Interval (S)</label>
         <input type="text" id="node-input-interval" placeholder="Interval">
     </div>
+    <p>HTTP Authentication</p>
+    <div class="form-row">
+        <label for="node-input-username"><i class="fa fa-user"></i> Username</label>
+        <input type="text" id="node-input-username" placeholder="Username">
+    </div>
+    <div class="form-row">
+        <label for="node-input-password"><i class="fa fa-key"></i> Password</label>
+        <input type="text" id="node-input-password" placeholder="Password">
+    </div>
+    <p>Return msg.payload as</p>
+    <div class="form-row">
+        <label for="node-input-body"><i class="fa fa-code"></i> Body</label>
+        <select id="node-input-body" style="width:200px !important">
+            <option value="html">HTML</option>
+            <option value="json">JSON</option>
+        </select>
+    </div>
+    <div class="form-row">
+        <i class="fa fa-code"></i> Custom headers (JSON)
+        <input type="hidden" id="node-input-headers">
+    </div>
+    <div class="form-row node-text-editor-row">
+        <div style="height:250px;" class="node-text-editor" id="node-input-headers-editor" ></div>
+    </div>
 </script>
 <script type="text/x-red" data-template-name="Web watch in">
     <div class="form-row">
@@ -24,6 +48,34 @@
     <div class="form-row">
         <label for="node-input-interval"><i class="fa fa-clock-o"></i> Interval (S)</label>
         <input type="text" id="node-input-interval" placeholder="Interval">
+    </div>
+    <div class="form-row">
+        <label for="node-input-headers"><i class="fa fa-user"></i> Custom headers</label>
+        <input type="text" id="node-input-headers" placeholder="Custom headers">
+    </div>
+    <p>HTTP Authentication</p>
+    <div class="form-row">
+        <label for="node-input-username"><i class="fa fa-user"></i> Username</label>
+        <input type="text" id="node-input-username" placeholder="Username">
+    </div>
+    <div class="form-row">
+        <label for="node-input-password"><i class="fa fa-key"></i> Password</label>
+        <input type="text" id="node-input-password" placeholder="Password">
+    </div>
+    <p>Return msg.payload as</p>
+    <div class="form-row">
+        <label for="node-input-body"><i class="fa fa-code"></i> Body</label>
+        <select id="node-input-body" style="width:200px !important">
+            <option value="html">HTML</option>
+            <option value="json">JSON</option>
+        </select>
+    </div>
+    <div class="form-row">
+        <i class="fa fa-code"></i> Custom headers (JSON)
+        <input type="hidden" id="node-input-headers">
+    </div>
+    <div class="form-row node-text-editor-row">
+        <div style="height:250px;" class="node-text-editor" id="node-input-headers-editor" ></div>
     </div>
 </script>
 
@@ -43,7 +95,23 @@
             name: {value: ""},
             url: {value: "", required: true},
             interval: {value: "60", required: true, validate: RED.validators.number()},
-            active: {value:true}
+            headers: {value: '{\n\t"User-Agent": "request"\n}', validate: function(v) {
+                var valid = true;
+                if (v) {
+                    try {
+                        JSON.parse(v);
+                    }
+                    catch (ex) {
+                        valid = false;
+                    }
+                    $("#node-input-headers-editor").toggleClass("input-error", !valid);
+                }
+                return valid;
+            }},
+            username: {value: ""},
+            password: {value: ""},
+            body: {value: "html", required: true},
+            active: {value: true}
         },
         inputs: 0,
         outputs: 1,
@@ -82,6 +150,21 @@
                     }
                 });
             }
+        },
+        oneditprepare: function() {
+            if (this.body === undefined) {
+                this.body = "html";
+                $("#node-input-body").val(this.body);
+            }
+            this.editor = RED.editor.createEditor({
+                id: 'node-input-headers-editor',
+                mode: 'ace/mode/javascript',
+                value: $("#node-input-headers").val()
+            });
+        },
+        oneditsave: function() {
+            $("#node-input-headers").val(this.editor.getValue());
+            delete this.editor;
         }
     });
     RED.nodes.registerType("Web watch in", {
@@ -91,6 +174,22 @@
             name: {value: ""},
             url: {value: "", required: true},
             interval: {value: "60", required: true, validate: RED.validators.number()},
+            headers: {value: '{\n\t"User-Agent": "request"\n}', validate: function(v) {
+                var valid = true;
+                if (v) {
+                    try {
+                        JSON.parse(v);
+                    }
+                    catch (ex) {
+                        valid = false;
+                    }
+                    $("#node-input-headers-editor").toggleClass("input-error", !valid);
+                }
+                return valid;
+            }},
+            username: {value: ""},
+            password: {value: ""},
+            body: {value: "html", required: true},
             active: {value:true}
         },
         inputs: 1,
@@ -130,6 +229,21 @@
                     }
                 });
             }
+        },
+        oneditprepare: function() {
+            if (this.body === undefined) {
+                this.body = "html";
+                $("#node-input-body").val(this.body);
+            }
+            this.editor = RED.editor.createEditor({
+                id: 'node-input-headers-editor',
+                mode: 'ace/mode/javascript',
+                value: $("#node-input-headers").val()
+            });
+        },
+        oneditsave: function() {
+            $("#node-input-headers").val(this.editor.getValue());
+            delete this.editor;
         }
     });
 </script>

--- a/web-watch/web-watch.js
+++ b/web-watch/web-watch.js
@@ -61,7 +61,7 @@
                     }
                 }
             }
-            catch (ex){
+            catch (ex) {
                 console.log (ex);
             }
 
@@ -118,13 +118,30 @@
 
         node.intervalId = setInterval(function() {
             if (node.active == false) return;
-            request(config.url, function (error, response, body) {
+            var options = {};
+            if (config.username && config.password) {
+                options.auth = {
+                    'user': config.username,
+                    'pass': config.password
+                };
+            }
+            if (config.headers) {
+                 options.headers = JSON.parse(config.headers);
+            }
+            request(config.url, options, function (error, response, body) {
                 if (!error && response.statusCode == 200) {
                     if (node.cacheHtml == null) {
                         node.cacheHtml = body;
                     } else if (node.cacheHtml != "" && node.cacheHtml != body) {
                         node.cacheHtml = body;
                         msg.headers = response.headers;
+                        if (config.body == 'json') {
+                            try {
+                                body = JSON.parse(body);
+                            } catch(ex) {
+                                body = ex;
+                            }
+                        }
                         msg.payload = body;
                         node.send(msg);
                     }

comments