-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
57 lines (47 loc) · 1.59 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
var loaderUtils = require('loader-utils');
module.exports = function(content) {
this.cacheable && this.cacheable();
var query = loaderUtils.parseQuery(this.query);
var scriptString = content.toString().replace(/\n$/, '');
var scriptEscaped = escape(scriptString);
var exportedFunction =
`module.exports = function() {
var args = Array.prototype.slice.call(arguments);
var callback;
if (typeof args[args.length - 1] === 'function') {
callback = args[args.length - 1];
}
if (args.length > 1) args.splice(-1,1); // remove callback
var argsStringified = JSON.stringify(args);
var cs;
if (window.CSInterface) {
cs = new window.CSInterface();
} else if (window.parent.CSInterface) {
cs = new window.parent.CSInterface()
} else {
throw Error("[extend-script-loader]: CSInterface not found. Are you sure it's included?")
}
cs.evalScript(\`(function() {
try {
var result = eval(unescape("${scriptEscaped}"));
if (typeof result === 'function') result = result.apply(this, \${argsStringified});
result = JSON.stringify({result: result});
return result;
} catch (err) {
return JSON.stringify({error: err});
};
})()\`, function(result) {
result = JSON.parse(result);
if (result.error) {
var error = result.error;
if (callback) callback(error);
var errorMessage = error.name + " " + error.fileName + ":" + error.line + " " + error.message;
console.error(errorMessage);
return;
}
if (callback) callback(undefined, result.result);
});
}`;
return exportedFunction;
}
module.exports.raw = true;