提问人:ElektroStudios 提问时间:7/9/2023 最后编辑:ElektroStudios 更新时间:7/9/2023 访问量:133
Microsoft ClearScript 中的“x 未定义”错误
'x is not defined' error in Microsoft ClearScript
问:
我想为 xkpasswd JavaScript 库创建一个基本的 .NET 包装器(请参阅此处的 generate.js 源代码,以及此处的现场演示)。为了实现这一点,我使用了 Microsoft ClearScript。
我有两个问题。我认为第一个已经解决或部分解决了......
我遇到的第一个问题是在尝试执行 generate.js 文件时,我收到一条错误消息:“”。我试图通过在执行脚本文档()时将文档类别设置为来解决错误,这是我在此答案中看到类似解决方案后发现的;并且还进行了更改以从 node.js 中删除“fs”和“path”模块,因此这是我现在正在执行的代码:require is not defined
ModuleCategory.CommonJS
engine.ExecuteDocument(".\generate.js", ModuleCategory.CommonJS)
//var fs = require('fs');
//var path = require('path');
var defaultWordList = require('./xkpasswd-words.json');
// define helpers
var h = {
random: function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
getRandomWord: function getRandomWord(wordList) {
return wordList[h.random(0, wordList.length - 1)];
},
resolveComplexity: function resolveComplexity(complexity) {
// Patterns can consist of any combination of the following: (w)ords, (d)igits, (s)eparators
var compl = complexity || 2;
var rtn = {};
rtn.separators = '#.-=+_';
if (compl < 1) compl = 1;
if (compl > 6) compl = 6;
if (compl === 1) rtn.pattern = 'wsw';
if (compl === 2) rtn.pattern = 'wswsw';
if (compl === 3) rtn.pattern = 'wswswsdd';
if (compl === 4) rtn.pattern = 'wswswswsdd';
if (compl === 5) {
rtn.pattern = 'wswswswswsdd';
rtn.separators = '#.-=+_!$*:~?';
}
if (compl === 6) {
rtn.pattern = 'ddswswswswswsdd';
rtn.transform = 'alternate';
rtn.separators = '#.-=+_!$*:~?%^&;';
}
return rtn;
},
processOpts: function processOpts(opts) {
var rtn = {};
var complexity = parseInt(opts.complexity, 10);
complexity = typeof complexity === 'number' ? complexity : 3;
var predefined = h.resolveComplexity(complexity);
var separators = typeof opts.separators === 'string' ? opts.separators : predefined.separators;
rtn.pattern = opts.pattern || predefined.pattern;
rtn.separator = separators.split('')[h.random(0, separators.length - 1)];
rtn.transform = opts.transform || predefined.transform || 'lowercase';
rtn.wordList = opts.wordList;
return rtn;
},
// this needs to support the following options:
// 1) ""words.json""
// 2) ""words.txt""
// 3) ""orange,banana, fizz, buzz"" (string of comma-separated words)
readCustomWordList: function readCustomWordList(input) {
var data;
var rtn = [];
if (Array.isArray(input)) {
data = input;
}
// parse string input
if (typeof input === 'string') {
var tmpWordList = input.split(',');
if (tmpWordList.length === 1) {
//var targetFile = path.resolve(tmpWordList[0]);
var fileName = tmpWordList[0];
var shell = new ActiveXObject('WScript.Shell');
var targetFile = shell.CurrentDirectory + '\\' + fileName;
if (targetFile.indexOf('.json') === targetFile.length - 5) {
// eslint-disable-next-line
data = require(targetFile);
}
if (targetFile.indexOf('.txt') === targetFile.length - 4) {
//var fileContents = fs.readFileSync(targetFile).toString();
var fileSystem = new ActiveXObject('Scripting.FileSystemObject');
var file = fileSystem.OpenTextFile(targetFile, 1);
var fileContents = file.ReadAll();
file.Close();
data = fileContents.split('\n');
}
}
if (!data) {
data = tmpWordList;
}
}
// if there's no data return false
if (!data) {
return false;
}
// remove empty
for (var i = 0; i < data.length; i++) {
var word = typeof data[i] === 'string' ? data[i].trim() : '';
if (word.length) {
rtn.push(word);
}
}
return rtn;
}
};
module.exports = function main(opts) {
var o = h.processOpts(opts || {});
var pattern = o.pattern.split('');
var uppercase = (typeof o.transform === 'string' && o.transform.toLowerCase() === 'uppercase');
var password = [];
var wordList = defaultWordList;
var customWordList = h.readCustomWordList(o.wordList);
if (Array.isArray(customWordList) && customWordList.length) {
wordList = customWordList;
}
pattern.forEach(function generatePasswordSegment(type) {
var value;
if (type === 'd') value = h.random(0, 9);
if (type === 's') value = o.separator;
if (type === 'w' || type === 'W') {
value = h.getRandomWord(wordList);
if (typeof o.transform === 'string' && o.transform.toLowerCase() === 'alternate') {
uppercase = !uppercase;
}
if (uppercase || type === 'W') {
value = value.toUpperCase();
} else {
value = value.toLowerCase();
}
}
password.push(value);
});
return password.join('');
};
注意:我不确定我所做的更改是否会按预期工作,无论如何我不需要在功能中读取外部单词列表文件,因此我可以在最坏的情况下完全删除该代码并仅加载带有单词的字符串数组。readCustomWordList
我遇到的第二个也是当前的问题是,在尝试让“main”函数使用它时出现错误。错误消息显示:。main is not defined
这是我正在使用的代码:
Using engine As New V8ScriptEngine("xkpasswd_engine", V8ScriptEngineFlags.None)
engine.ExecuteDocument("\generate.js", ModuleCategory.CommonJS)
Dim mainFunction As ScriptObject =
DirectCast(engine.Evaluate("main"), ScriptObject)
Dim opts As Object = New With {
.complexity = 3,
.separators = "#.-=+_",
.pattern = "wswswsdd",
.transform = "lowercase",
.wordList = Nothing
}
Dim password As String =
CStr(mainFunction.Invoke(asConstructor:=False, opts))
Console.WriteLine("Generated password: " & password)
End Using
我做错了什么以及如何解决它?
我想也许这只是弄清楚检索“main”的正确命名的问题?我试图获取“module.exports”,但我也收到错误。module is not defined
答:
要访问 CommonJS 模块的导出,您必须从另一个 CommonJS 模块调用。require
取而代之的是:
engine.ExecuteDocument("generate", ModuleCategory.CommonJS)
Dim mainFunction = DirectCast(engine.Evaluate("main"), ScriptObject) 'ERROR
试试这个:
Dim info As New DocumentInfo With {.Category = ModuleCategory.CommonJS}
Dim mainFunction = DirectCast(engine.Evaluate(info, "return require('generate')"), ScriptObject) 'OK
另一件事:Node.js显然可以将JSON文件视为CommonJS模块,但ClearScript不能。要实现此目的,请在 .它应如下所示:module.exports =
xkpasswd-words.json
module.exports = ["aa","aah", ...
评论
toLowerCase
module.exports =
xkpasswd-words.json
评论