-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
40 lines (33 loc) · 971 Bytes
/
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
/**
* Generates the userscript metadata block.
* @param {object} userscript The object containing the userscript data.
* @param {object} context The data used to parse values with mustache. Use {{{yourKey}}} in the data to parse.
* @return {string} The finalized metadata block.
*/
var hogan = require('hogan.js');
function generateUserscriptHeader(userscript, context) {
var header = [];
var key = null;
var data = null;
header.push('// ==UserScript==');
for (key in userscript) {
if (!userscript.hasOwnProperty(key)) {
continue;
}
data = userscript[key];
if (Array.isArray(data)) {
data.forEach(function (value) {
pushLine(key, value);
});
}
else {
pushLine(key, data);
}
}
header.push('// ==/UserScript==');
function pushLine(key, value) {
header.push('// @' + key + ' ' + hogan.compile(value).render(context));
}
return header.join('\n') + '\n\n';
};
module.exports = generateUserscriptHeader;