脚本导致DevTools崩溃

时间:2016-08-10 06:26:28

标签: javascript python google-chrome google-chrome-devtools

我正在尝试编写一个娱乐聊天机器人,其主要功能之一是模因查找。我首先在Python中编写了这个,但我现在用JavaScript重写它,因为它可以完全在客户端运行。

这是我的JavaScript public static List<T> BindList<T>(DataTable dt) { // Example 1: // Get private fields + non properties //var fields = typeof(T).GetFields(BindingFlags.NonPublic | BindingFlags.Instance); // Example 2: Your case // Get all public fields var fields = typeof(T).GetFields(); List<T> lst = new List<T>(); foreach (DataRow dr in dt.Rows) { // Create the object of T var ob = Activator.CreateInstance<T>(); foreach (var fieldInfo in fields) { foreach (DataColumn dc in dt.Columns) { // Matching the columns with fields if (fieldInfo.Name == dc.ColumnName) { Type type = fieldInfo.FieldType; // Get the value from the datatable cell object value = GetValue(dr[dc.ColumnName], type); // Set the value into the object fieldInfo.SetValue(ob, value); break; } } } lst.Add(ob); } return lst; } static object GetValue(object ob, Type targetType) { if (targetType == null) { return null; } else if (targetType == typeof(String)) { return ob + ""; } else if (targetType == typeof(int)) { int i = 0; int.TryParse(ob + "", out i); return i; } else if (targetType == typeof(short)) { short i = 0; short.TryParse(ob + "", out i); return i; } else if (targetType == typeof(long)) { long i = 0; long.TryParse(ob + "", out i); return i; } else if (targetType == typeof(ushort)) { ushort i = 0; ushort.TryParse(ob + "", out i); return i; } else if (targetType == typeof(uint)) { uint i = 0; uint.TryParse(ob + "", out i); return i; } else if (targetType == typeof(ulong)) { ulong i = 0; ulong.TryParse(ob + "", out i); return i; } else if (targetType == typeof(double)) { double i = 0; double.TryParse(ob + "", out i); return i; } else if (targetType == typeof(DateTime)) { // do the parsing here... } else if (targetType == typeof(bool)) { // do the parsing here... } else if (targetType == typeof(decimal)) { // do the parsing here... } else if (targetType == typeof(float)) { // do the parsing here... } else if (targetType == typeof(byte)) { // do the parsing here... } else if (targetType == typeof(sbyte)) { // do the parsing here... } else if........ .................. return ob; } 功能:

meme()

这里是原始的Python函数,如果有帮助的话:

function meme(srch) {
  reqUrl = "http://api.pixplorer.co.uk/image?amount=1&size=tb&word=meme";
  memesrch = "";
  while (srch) {
    memesrch += "+" + srch[0];
    srch.slice(1);
  }
  reqUrl += memesrch;
  $.get(reqUrl, function( result ) {
    memeUrl = result['images'][0]['imageurl'];
  });
  return "<a href='" + memeUrl + "'><img src='" + memeUrl + "' style='height: 130px;' /></a>";
}

我的问题是,当我在页面加载/重新加载后首次在控制台中运行def meme(srch): reqUrl = "http://api.pixplorer.co.uk/image?amount=1&size=tb&word=meme" if srch: memesrch = "" while srch: memesrch += srch[0] + "+" srch = srch[1:] memesrch = memesrch[:-1] reqUrl += memesrch memeUrl = eval(urllib2.urlopen(reqUrl).read())['images'][0]['imageurl'] return "<a href='" + memeUrl + "'><img src='" + memeUrl + "' style='height: 130px;' /></a>" 时,它表示变量meme()未定义。然后,从第二次开始,它工作正常。但是,如果我然后输入memeUrl或者在数组中输入任何字符串,或者甚至只是meme(["doge"])之类的字符串给meme("hello")函数,它就不会返回任何内容,甚至不返回任何内容一个错误。之后,我输入的任何内容都不会返回任何内容,甚至不会返回meme()1+13。几秒钟后,网页崩溃了。以下是一些屏幕截图:Screenshot of DevToolsScreenshot of Webpage Crash

我不知道导致这些的原因是什么,因为我能想到的唯一可能导致此问题的是无限循环,但我的代码中没有一个。

1 个答案:

答案 0 :(得分:0)

有一个无限循环:

while (srch) {
    ...
}

srch.slice不会更改原始数组&#34; srch&#34;。 也许这解决了你的问题:而不是你的while循环执行以下操作:

memesrch = (srch typeof Array)
    ? '+' + srch.join('+')
    : '+' + srch;
相关问题