headers for request

This commit is contained in:
Martin Donnelly 2016-03-23 10:29:00 +00:00
parent e5334cc5e7
commit d85d7fddd7
5 changed files with 335 additions and 308 deletions

32
.editorconfig Normal file
View File

@ -0,0 +1,32 @@
; http://editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
[*.txt]
insert_final_newline = false
trim_trailing_whitespace = false
[*.py]
indent_size = 4
[*.m]
indent_size = 4
[Makefile]
indent_style = tab
indent_size = 8
[*.{js,json}]
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false

46
.jscsrc Normal file
View File

@ -0,0 +1,46 @@
{
"disallowKeywords": ["with"],
"disallowKeywordsOnNewLine": ["else"],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleVarDecl": "exceptUndefined",
"disallowNewlineBeforeBlockStatements": true,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpacesInFunction": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInsideParentheses": true,
"disallowTrailingWhitespace": true,
"maximumLineLength": 120,
"requireCamelCaseOrUpperCaseIdentifiers": false,
"requireCapitalizedComments": true,
"requireCapitalizedConstructors": true,
"requireCurlyBraces": true,
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"return",
"try",
"catch",
"typeof"
],
"requireSpaceAfterLineComment": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBeforeObjectValues": true,
"requireSpacesInFunction": {
"beforeOpeningCurlyBrace": true
},
"requireTrailingComma": false,
"requireEarlyReturn": false,
"validateIndentation": 2,
"validateLineBreaks": "LF",
"validateQuoteMarks": "'"
}

View File

@ -1,375 +1,313 @@
import * as EJS from 'ejs';
'use strict'; 'use strict';
/** /**
* Created by Martin on 24/02/2016. * Created by Martin on 24/02/2016.
*/ */
$.fn.pressEnter = function (fn) { $.fn.pressEnter = function(fn) {
return this.each(function () { return this.each(function() {
$(this).bind('enterPress', fn); $(this).bind('enterPress', fn);
$(this).keyup(function (e) { $(this).keyup(function(e) {
if (e.keyCode === 13) { if (e.keyCode === 13) {
$(this).trigger('enterPress'); $(this).trigger('enterPress');
} }
});
}); });
});
}; };
var Keeper = (function () { var Keeper = function() {
var self = this; var self = this;
console.log('GO!'); console.log('GO!');
var $list = $('#listContainer'); var $list = $('#listContainer');
var displayList = function (obj) { var displayList = function(obj) {
var html = new EJS({url: '/partials/list.ejs'}).render(obj); var html = new EJS({url: '/partials/list.ejs'}).render(obj);
// console.log(html); // Console.log(html);
$list.empty(); $list.empty();
$list.append(html); $list.append(html);
$('#listContainer').find('.entry').not('.emptyMessage').click(function () { $('#listContainer').find('.entry').not('.emptyMessage').click(function() {
getBookmark(this.id); getBookmark(this.id);
}); });
}, }, displayPage = function(obj) {
displayPage = function (obj) { var $bodyContents = $('#bodyContents');
var $bodyContents = $('#bodyContents');
if (obj.reduced.length > 0) {
var vdata = {
data: {
_id: obj._id,
_rev: obj._rev,
title: obj.title,
reduced: obj.reduced,
tags: obj.tags,
url: obj.url
}
};
var redoData = {
_id: obj._id,
_rev: obj._rev,
url: obj.url
};
var html = new EJS({url: 'partials/view.ejs'}).render(vdata);
$bodyContents.empty();
$bodyContents.scrollTop(0);
$bodyContents.append(html);
$('#redo').on('click', function () {
self.trigger('redo', redoData);
});
$('#tagSave').on('click', function () {
self.trigger('tagsave', redoData);
});
$('#tageditmode').on('click', function () {
self.trigger('startTags');
});
if (obj.reduced.length > 0) {
var vdata = {
data: {
_id: obj._id,
_rev: obj._rev,
title: obj.title,
reduced: obj.reduced,
tags: obj.tags,
url: obj.url
} }
}, getBookmark = function (id) { };
//console.log('getBookmark');
var redoData = {
_id: obj._id, _rev: obj._rev, url: obj.url
};
var html = new EJS({url: 'partials/view.ejs'}).render(vdata);
$bodyContents.empty();
$bodyContents.scrollTop(0);
$bodyContents.append(html);
$('#redo').on('click', function() {
self.trigger('redo', redoData);
});
$('#tagSave').on('click', function() {
self.trigger('tagsave', redoData);
});
$('#tageditmode').on('click', function() {
self.trigger('startTags');
});
}
}, getBookmark = function(id) {
// Console.log('getBookmark');
var url = '/entry/' + id; var url = '/entry/' + id;
var data = ''; var data = '';
$.ajax({ $.ajax({
type: 'GET', type: 'GET', url: url, data: data, dataType: 'json',
url: url,
data: data,
dataType: 'json',
timeout: 10000, timeout: 10000,
//contentType: ('application/json'), // ContentType: ('application/json'),
headers: { headers: {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type' 'Access-Control-Allow-Headers': 'Content-Type'
}, }, success: function(data) {
success: function (data) { // Console.log(data);
// console.log(data); displayPage(data);
displayPage(data); }, error: function(xhr, type) {
},
error: function (xhr, type) { }
console.log('ajax error');
console.log(xhr);
console.log(type);
}
}); });
}, }, updateMainTagList = function(obj) {
updateMainTagList = function (obj) { var $taglist = $('#tagList');
var $taglist = $('#tagList');
var html = new EJS({url: '/partials/taglist.ejs'}).render(obj); var html = new EJS({url: '/partials/taglist.ejs'}).render(obj);
$taglist.empty(); $taglist.empty();
$taglist.html(html); $taglist.html(html);
}, },
getMainTagList = function () { getMainTagList = function() {
var url = '/tags'; var url = '/tags';
$.ajax({ $.ajax({
type: 'GET', type: 'GET', url: url, data: '', dataType: 'json',
url: url,
data: '',
dataType: 'json',
timeout: 10000, timeout: 10000,
//contentType: ('application/json'), // ContentType: ('application/json'),
headers: { headers: {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type' 'Access-Control-Allow-Headers': 'Content-Type'
}, }, success: function(data) {
success: function (data) { // Console.log(data);
// console.log(data); updateMainTagList(data);
updateMainTagList(data); }, error: function(xhr, type) {
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
},
getList = function () {
var url = '/list'; }
});
}, getList = function() {
$.ajax({ var url = '/list';
type: 'GET',
url: url,
data: '',
dataType: 'json',
timeout: 10000, $.ajax({
type: 'GET', url: url, data: '', dataType: 'json',
//contentType: ('application/json'), timeout: 10000,
headers: {
'Access-Control-Allow-Origin': '*', // ContentType: ('application/json'),
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', headers: {
'Access-Control-Allow-Headers': 'Content-Type' 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}, success: function(data) {
// Console.log(data);
displayList(data);
}, error: function(xhr, type) {
},
success: function (data) {
// console.log(data);
displayList(data);
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
},
saveTags = function (d) {
function parse(input) {
return input
.trim()
.split(/\s*,\s*/)
.map(function (col) {
return col.trim();
})
.filter(function (col) {
return col && col.length;
});
} }
});
}, saveTags = function(d) {
function parse(input) {
return input
.trim()
.split(/\s*,\s*/)
.map(function(col) {
return col.trim();
})
.filter(function(col) {
return col && col.length;
});
}
var data = d; var data = d;
var url = '/tags'; var url = '/tags';
var tags = {}, tagItems = $('#edittags').val(); var tags = {};
tagItems = parse(tagItems); var tagItems = $('#edittags').val();
tags.list = tagItems; tagItems = parse(tagItems);
tags.solid = tagItems.join(', '); tags.list = tagItems;
tags.solid = tagItems.join(', ');
data.tags = tags; data.tags = tags;
$.ajax({ $.ajax({
type: 'POST', type: 'POST', url: url, data: d, dataType: 'json',
url: url,
data: d,
dataType: 'json',
timeout: 10000, timeout: 10000,
//contentType: ('application/json'), // ContentType: ('application/json'),
headers: { headers: {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type' 'Access-Control-Allow-Headers': 'Content-Type'
}, success: function() {
// Console.log(data);
// displayList(data);
$('#visualTabs').toggle();
$('#tagForm').toggle();
}, error: function(xhr, type) {
},
success: function () {
// console.log(data);
// displayList(data);
$('#visualTabs').toggle();
$('#tagForm').toggle();
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
} }
}); });
} }, addNew = function(newUrl) {
var url = '/add';
, addNew = function (newUrl) { var data = {url: JSON.stringify(newUrl)};
var url = '/add'; $.ajax({
type: 'POST', url: url, data: data, dataType: 'json',
var data = {url: JSON.stringify(newUrl)}; timeout: 10000,
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
timeout: 10000, //ContentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
//contentType: ('application/json'), }, success: function() {
headers: { // Console.log(data);
'Access-Control-Allow-Origin': '*', // displayList(data);
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', }, error: function(xhr, type) {
'Access-Control-Allow-Headers': 'Content-Type'
}, }
success: function () { });
// console.log(data); }, redo = function(d) {
// displayList(data); console.log('redooing');
}, var url = '/redo';
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
},
redo = function (d) {
console.log('redooing');
var url = '/redo';
$.ajax({ $.ajax({
type: 'POST', type: 'POST', url: url, data: d, dataType: 'json',
url: url,
data: d,
dataType: 'json',
timeout: 10000, timeout: 10000,
//contentType: ('application/json'), //ContentType: ('application/json'),
headers: { headers: {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type' 'Access-Control-Allow-Headers': 'Content-Type'
}, }, success: function() {
success: function () { // Console.log(data);
// console.log(data); // displayList(data);
// displayList(data); }, error: function(xhr, type) {
},
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
},
start = function () {
getList(); }
getMainTagList(); });
}, }, start = function() {
listFromTag = function (tag) {
console.log('get for ' + tag);
var url = '/tags/' + tag;
$.ajax({ getList();
type: 'GET', getMainTagList();
url: url, }, listFromTag = function(tag) {
data: '', console.log('get for ' + tag);
dataType: 'json', var url = '/tags/' + tag;
timeout: 10000, $.ajax({
type: 'GET', url: url, data: '', dataType: 'json',
//contentType: ('application/json'), timeout: 10000,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}, // ContentType: ('application/json'),
success: function (data) { headers: {
// console.log(data); 'Access-Control-Allow-Origin': '*',
displayList(data); 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
}, 'Access-Control-Allow-Headers': 'Content-Type'
error: function (xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
}; }, success: function(data) {
// Console.log(data);
displayList(data);
}, error: function(xhr, type) {
$('#newurl').pressEnter(function () { }
var url = $(this).val(); });
if (url !== null) {
console.log('Adding: ' + url);
addNew(url);
$('#addstatus').fadeIn(400).delay(1500).fadeOut(400);
$(this).val('');
/* setTimeout(function () {
getList();
}, 5000);*/
}
});
$('#fnRefresh').on('click', function () { };
getList();
});
$(window).on('hashchange', ()=> { $('#newurl').pressEnter(function() {
var url = $(this).val();
if (url !== null) {
console.log('Adding: ' + url);
addNew(url);
$('#addstatus').fadeIn(400).delay(1500).fadeOut(400);
$(this).val('');
/* SetTimeout(function () {
getList();
}, 5000);*/
}
});
self.trigger('hashchange', location.hash); $('#fnRefresh').on('click', function() {
}); getList();
});
this.bind('redo', function (data) { $(window).on('hashchange', ()=> {
redo(data);
});
this.bind('tagsave', function (data) { self.trigger('hashchange', location.hash);
saveTags(data); });
});
this.bind('startTags', function (data) {
$('#visualTabs').toggle();
$('#tagForm').toggle();
});
this.bind('hashchange', function (data) { this.bind('redo', function(data) {
redo(data);
});
var tagroute = data.replace('#?', ''); this.bind('tagsave', function(data) {
saveTags(data);
});
this.bind('startTags', function(data) {
$('#visualTabs').toggle();
$('#tagForm').toggle();
});
if (tagroute !== '') this.bind('hashchange', function(data) {
{
listFromTag(tagroute);
}
else
{
getList();
}
});
start(); var tagroute = data.replace('#?', '');
})
; if (tagroute !== '') {
listFromTag(tagroute);
} else {
getList();
}
});
start();
};
MicroEvent.mixin(Keeper); MicroEvent.mixin(Keeper);

View File

@ -5,7 +5,7 @@
"main": "recipe-server.js", "main": "recipe-server.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"update" :"git pull && npm install && gulp default" "update": "git pull && npm install && gulp default"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
@ -31,7 +31,7 @@
"gulp-cssnano": "^2.1.1", "gulp-cssnano": "^2.1.1",
"gulp-html-replace": "^1.5.5", "gulp-html-replace": "^1.5.5",
"gulp-htmlmin": "^1.3.0", "gulp-htmlmin": "^1.3.0",
"gulp-inject": "^3.0.0", "gulp-inject": "^4.0.0",
"gulp-jshint": "^2.0.0", "gulp-jshint": "^2.0.0",
"gulp-livereload": "^3.8.1", "gulp-livereload": "^3.8.1",
"gulp-notify": "^2.2.0", "gulp-notify": "^2.2.0",

View File

@ -284,7 +284,18 @@ function genericGrab(obj, res) {
logger.info(_id); logger.info(_id);
logger.info(_ver); logger.info(_ver);
request(url, function (err, resp, body) {
var options = {
url: url,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'
},
jar:true,
followRedirect:true,
followAllRedirects:true
};
request(options, function (err, resp, body) {
if (err) if (err)
throw err; throw err;