-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (77 loc) · 2 KB
/
index.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
87
88
89
90
91
92
93
94
'use strict';
var winston = require.main.require('winston');
var db = module.parent.require('./database');
var plugin = {};
var engineID = '';
function constructSearch(req, res, next) {
db.getObject('nodebb-plugin-external-google-cse', function(err, data) {
if (err) {
return next(err);
}
if (!data) {
data = {
engineID: ''
};
}
var url = 'https://cse.google.com/cse/publicurl?cx=' + data.engineID + '&q=';
res.send(url);
});
}
var engineID = '';
plugin.init = function(params, callback) {
params.router.get('/admin/plugins/externalCSE',
params.middleware.applyCSRF,
params.middleware.admin.buildHeader,
renderAdmin);
params.router.get('/api/admin/plugins/externalCSE',
params.middleware.applyCSRF,
renderAdmin);
params.router.post('/api/admin/plugins/externalCSE/save',
params.middleware.applyCSRF,
save);
params.router.get('/CSE', constructSearch)
callback();
db.getObject('nodebb-plugin-external-google-cse', function(err, data) {
if (err) {
return winston.error(err.error);
}
if (data) {
engineID = data.engineID ? data.engineID : '';
}
});
};
function renderAdmin(req, res, next) {
db.getObject('nodebb-plugin-external-google-cse', function(err, data) {
if (err) {
return next(err);
}
if (!data) {
data = {
engineID: engineID
};
}
data.csrf = req.csrfToken();
res.render('admin/plugins/externalCSE', data);
});
}
function save(req, res, next) {
var data = {
engineID: req.body.engineID
};
db.setObject('nodebb-plugin-external-google-cse', data, function(err) {
if (err) {
return res.json(500, 'error-saving');
}
engineID = data.engineID;
res.json('Settings saved!');
});
}
plugin.adminMenu = function(customHeader, callback) {
customHeader.plugins.push({
route: '/plugins/externalCSE',
icon: 'fa-search',
name: 'Google CSE'
});
callback(null, customHeader);
};
module.exports = plugin;