icson.zhougmail.com / 豆果新增

// ==UserScript==
// @name        豆果新增
// @namespace   cn.dietcare
// @version     1
// @include  http://www.douguo.com/upfood.html
// @grant       none
// ==/UserScript==
$(function () {
  function addCssByStyle(cssString) {
    var doc = document;
    var style = doc.createElement('style');
    style.setAttribute('type', 'text/css');
    if (style.styleSheet) { // IE
      style.styleSheet.cssText = cssString;
    } else { // w3c
      var cssText = doc.createTextNode(cssString);
      style.appendChild(cssText);
    }
    var heads = doc.getElementsByTagName('head');
    if (heads.length)
    heads[0].appendChild(style);
     else
    doc.documentElement.appendChild(style);
  }
  addCssByStyle('.list_box {position:absolute;margin-left:1px;width:191px;background:#f4f0ec;border:1px solid #deded3;line-height:20px;overflow:auto;max-height:100px;} .list_box ul li {padding:3px 0px 3px 10px;list-style:none;} .list_box ul li:hover {background:#abcee4;color:#333333;}');
  var paramname = null;
  function sortArr(m, n) {
    if (m.length < n.length)
    return - 1; //小于,返回-1
     else if (m.length > n.length)
    return 1; //大于,返回1
     else return 0; //等于,返回0
  }
  $.extend({
    getdata: function (key, index) {
      var name = '';
      if (key == 'zhu') {
        name = $('input[name=\'zhuliao[]\']:eq(' + index + ')').val();
      } else {
        name = $('input[name=\'fuliao[]\']:eq(' + index + ')').val();
      }
      if ('null' != name && name != paramname && name.length != 0) {
        $.ajax({
          type: 'get',
          async: true,
          url: 'http://dietcare.cn:16168/dietservice/rest/queryIngredient',
          data: 'token=c8a6aed59bc175af999261f64e0e02aaa2d60476d6bd2194ac5241ba6f34513193fb01dc3e43d662&name=' + name,
          dataType: 'jsonp', //数据类型为jsonp  
          jsonp: 'jsonpCallback', //服务端用于接收callback调用的function名的参数  
          success: function (data) {
            var str = '';
            var str2 = '';
            if (data.data.length == 0) {
              str += ';';
            } else {
              /*$.each(data.data, function (index, content) {
												if(str.indexOf(content.name) < 0){
													str += '<li>' + content.name + '</li>';
											 	}		
											str2 += content.name+","+content.unit+"<span>/"+content.unit_weight+"g</span>;";
											});*/
              $.each(data.data, function (index, content) {
                if (index == 0) {
                  str += content.name + ';'
                } else {
                  var tempstr = str;
                  tempstr = tempstr.substring(0, tempstr.length - 1);
                  var ary = tempstr.split(';');
                  var flag = true;
                  for (var i = 0, j = ary.length; i < j; i++) {
                    if (ary[i] == content.name) {
                      flag = false;
                      break;
                    }
                  }
                  if (flag) {
                    str += content.name + ';'
                  }
                }
                str2 += content.name + ',' + content.unit + '<span>/' + content.unit_weight + 'g</span>;';
              });
            }
            str = str.substring(0, str.length - 1);
            if (str != '' && str.trim().length != 0 && str.trim() != ';') {
              var conary = str.split(';');
              str = '';
              for (var i = 0, j = conary.length; i < j; i++) {
                str += '<li>' + conary[i] + '</li>';
              }
              var datadiv = '<div id=\'tt\'><ul> ' + str + '</ul><input type=\'hidden\' name=\'tthide\' value=\'<li>g</li>\'/></div>';
              var datadiv3 = '<div id=\'tt3\'><ul> ' + str + '</ul><input type=\'hidden\' name=\'tthide3\' value=\'<li>g</li>\'/></div>';
              if (str == ';') {
                datadiv = '<div id=\'tt\'><input type=\'hidden\' name=\'tthide\' value=\'<li>g</li>\'/></div>';
                datadiv3 = '<div id=\'tt3\'><input type=\'hidden\' name=\'tthide3\' value=\'<li>g</li>\'/></div>';
              }
              if (key == 'zhu') {
                if ($('#tt').length > 0) {
                  $('#tt').remove();
                }
                $('input[name=\'zhuliao[]\']:eq(' + index + ')').after(datadiv);
                $('#tt').addClass('list_box');
              } else {
                if ($('#tt3').length > 0) {
                  $('#tt3').remove();
                }
                $('input[name=\'fuliao[]\']:eq(' + index + ')').after(datadiv3);
                $('#tt3').addClass('list_box');
              }
              if (str == ';') {
                $('#tt').hide();
                $('#tt3').hide();
              }
              $('#tt ul li').on('click', function () {
                var val = $(this).html();
                if (val === '') {
                  //$("input[name='zhuliao[]']:eq("+index+")").css("border-color","red").attr("title","由于部分数据无法匹配,您可以进行更换,或者等待我们进行添加.");
                } else {
                  $('input[name=\'zhuliao[]\']:eq(' + index + ')').val($(this).html());
                  var strAry = str2.substring(0, str2.length - 1).split(';');
                  str2 = '';
                  for (var i = 0; i < strAry.length; i++) {
                    if (strAry[i].split(',') [0] == val) {
                      str2 += strAry[i].split(',') [1] + ';';
                    }
                  }
                  if (str2.length != 0) {
                    var str2ary = str2.split(';');
                    var ttval = $('input[name=\'tthide\']').val();
                    var rs = '';
                    for (var i = 0; i < str2ary.length; i++) {
                      if (ttval.indexOf(str2ary[i].substring(0, str2ary[i].indexOf('<span>'))) < 0) {
                        rs += '<li>' + str2ary[i] + '</li>';
                      }
                    }
                    if (rs != '') {
                      $('input[name=\'tthide\']').val(ttval + rs);
                    }
                  }
                }
                $('#tt').hide();
              }).mousemove(function () {
                $(this).css('cursor', 'pointer');
              }).mouseout(function () {
                $(this).css('cursor', 'none');
              });
              $('#tt3 ul li').on('click', function () {
                var val = $(this).html();
                if (val === '') {
                  //$("input[name='fuliao[]']:eq("+index+")").css("border-color","red").attr("title","由于部分数据无法匹配,您可以进行更换,或者等待我们进行添加.");
                } else {
                  $('input[name=\'fuliao[]\']:eq(' + index + ')').val($(this).html());
                  var strAry = str2.substring(0, str2.length - 1).split(';');
                  str2 = '';
                  for (var i = 0; i < strAry.length; i++) {
                    if (strAry[i].split(',') [0] == val) {
                      str2 += strAry[i].split(',') [1] + ';';
                    }
                  }
                  if (str2.length != 0) {
                    var str2ary = str2.split(';');
                    var ttval = $('input[name=\'tthide3\']').val();
                    var rs = '';
                    for (var i = 0; i < str2ary.length; i++) {
                      if (ttval.indexOf(str2ary[i].substring(0, str2ary[i].indexOf('<span>'))) < 0) {
                        rs += '<li>' + str2ary[i] + '</li>';
                      }
                    }
                    if (rs != '') {
                      $('input[name=\'tthide3\']').val(ttval + rs);
                    }
                  }
                }
                $('#tt3').hide();
              }).mousemove(function () {
                $(this).css('cursor', 'pointer');
              }).mouseout(function () {
                $(this).css('cursor', 'none');
              });
            } else {
              var datadiv = '<div id=\'tt\'><ul> ' + str + '</ul><input type=\'hidden\' name=\'tthide\' value=\'<li>g</li>\'/></div>';
              var datadiv3 = '<div id=\'tt3\'><ul> ' + str + '</ul><input type=\'hidden\' name=\'tthide3\' value=\'<li>g</li>\'/></div>';
              if (str == ';') {
                datadiv = '<div id=\'tt\'><input type=\'hidden\' name=\'tthide\' value=\'<li>g</li>\'/></div>';
                datadiv3 = '<div id=\'tt3\'><input type=\'hidden\' name=\'tthide3\' value=\'<li>g</li>\'/></div>';
              }
              if (key == 'zhu') {
                if ($('#tt').length > 0) {
                  $('#tt').remove();
                }
                $('input[name=\'zhuliao[]\']:eq(' + index + ')').after(datadiv);
                $('#tt').hide();
              } else {
                if ($('#tt3').length > 0) {
                  $('#tt3').remove();
                }
                $('input[name=\'fuliao[]\']:eq(' + index + ')').after(datadiv3);
                $('#tt3').hide();
              }
            }
            var sss = ''
            var zhuVal = '';
            $('input[name=\'zhuliaoValue[]\']:eq(' + index + ')').focus(function () {
              sss = setInterval(function () {
                var value = '';
                value = $('input[name=\'zhuliaoValue[]\']:eq(' + index + ')').val();
                if (/^\d+\.?\d?$/.test(value) || /^[1-9]\d?\/[1-9]\d?$/.test(value) || /一|二|三|四|五|六|七|八|九|半|十/.test(value)) {
                  if (zhuVal != value) {
                    /*if(value.length>0){
														value = value.replace(/[^0-9]/ig,"");
													}*/
                    if ($('input[name=\'tthide\']').length > 0) {
                      var lival = $('input[name=\'tthide\']').val();
                      if (lival.indexOf('适') != - 1 || lival.indexOf('适量') != - 1 || lival.indexOf('少') != - 1 || lival.indexOf('少许') != - 1) {
                        var $lival = $('<ul>' + lival + '</ul>');
                        $lival.find('li').each(function () {
                          var li = $(this).html();
                          if (li.indexOf('适') != - 1 || li.indexOf('适量') != - 1 || li.indexOf('少') != - 1 || li.indexOf('少许') != - 1) {
                            $(this).remove();
                          }
                        });
                        lival = $lival.html();
                      }
                      var datadiv2 = '<div id=\'tt2\'><ul>' + lival + '</ul></div>';
                      if ($('#tt2').length > 0) {
                        $('#tt2').remove();
                      }
                      $('input[name=\'zhuliaoValue[]\']:eq(' + index + ')').after(datadiv2);
                      $('#tt2').addClass('list_box').css('width', '85px');
                      $('#tt2 ul li').each(function () {
                        $(this).html(value + '' + $(this).html());
                      });
                      $('#tt2 ul li').on('click', function () {
                        var html = $(this).html();
                        if (html.indexOf('span') != - 1) {
                          html = html.substring(0, html.indexOf('span') - 1)
                        }
                        $('input[name=\'zhuliaoValue[]\']:eq(' + index + ')').val(html);
                        $('#tt2').hide();
                      }).mousemove(function () {
                        $(this).css('cursor', 'pointer');
                      }).mouseout(function () {
                        $(this).css('cursor', 'none');
                      });
                    }
                  }
                } else if (value.indexOf('适') != - 1 || value.indexOf('适量') != - 1 || value.indexOf('少') != - 1 || value.indexOf('少许') != - 1) {
                  var lival = $('input[name=\'tthide\']').val();
                  if (zhuVal != value) {
                    var datadiv2 = '';
                    if ((value.indexOf('适') != - 1 || value.indexOf('适量') != - 1) && (lival.indexOf('适') != - 1 || lival.indexOf('适量') != - 1)) {
                      var $lival = $('<ul>' + lival + '</ul>');
                      $lival.find('li').each(function () {
                        var li = $(this).html();
                        if (li.indexOf('适') != - 1 || li.indexOf('适量') != - 1) {
                          datadiv2 = '<div id=\'tt2\'><ul><li>' + $(this).html() + '</li></ul></div>';
                          return;
                        }
                      });
                    }
                    if ((value.indexOf('少') != - 1 || value.indexOf('少许') != - 1) && (lival.indexOf('少') != - 1 || lival.indexOf('少许') != - 1)) {
                      var $lival = $('<ul>' + lival + '</ul>');
                      $lival.find('li').each(function () {
                        var li = $(this).html();
                        if (li.indexOf('少') != - 1 || li.indexOf('少许') != - 1) {
                          datadiv2 = '<div id=\'tt2\'><ul><li>' + $(this).html() + '</li></ul></div>';
                          return;
                        }
                      });
                    }
                    if ($('#tt2').length > 0) {
                      $('#tt2').remove();
                    }
                    $('input[name=\'zhuliaoValue[]\']:eq(' + index + ')').after(datadiv2);
                    $('#tt2').addClass('list_box').css('width', '85px');
                    /* $('#tt2 ul li').each(function(){
															$(this).html(value+""+$(this).html());
														}); */
                    $('#tt2 ul li').on('click', function () {
                      $('input[name=\'zhuliaoValue[]\']:eq(' + index + ')').val($(this).html().substring(0, $(this).html().indexOf('<span>')));
                      $('#tt2').hide();
                    }).mousemove(function () {
                      $(this).css('cursor', 'pointer');
                    }).mouseout(function () {
                      $(this).css('cursor', 'none');
                    });
                  }
                } else {
                  $('#tt2').hide();
                }
                zhuVal = value;
              }, 200);
            }).blur(function () {
              clearInterval(sss);
            });
            var fuVal = '';
            $('input[name=\'fuliaoValue[]\']:eq(' + index + ')').focus(function () {
              sss = setInterval(function () {
                var value = '';
                value = $('input[name=\'fuliaoValue[]\']:eq(' + index + ')').val();
                if (/^\d+\.?\d?$/.test(value) || /^[1-9]\d?\/[1-9]\d?$/.test(value) || /一|二|三|四|五|六|七|八|九|半|十/.test(value)) {
                  if (fuVal != value) {
                    /*if(value.length>0){
														value = value.replace(/[^0-9]/ig,"");
													}*/
                    if ($('input[name=\'tthide3\']').length > 0) {
                      var lival = $('input[name=\'tthide3\']').val();
                      if (lival.indexOf('适') != - 1 || lival.indexOf('适量') != - 1 || lival.indexOf('少') != - 1 || lival.indexOf('少许') != - 1) {
                        var $lival = $('<ul>' + lival + '</ul>');
                        $lival.find('li').each(function () {
                          var li = $(this).html();
                          if (li.indexOf('适') != - 1 || li.indexOf('适量') != - 1 || li.indexOf('少') != - 1 || li.indexOf('少许') != - 1) {
                            $(this).remove();
                          }
                        });
                        lival = $lival.html();
                      }
                      var datadiv2 = '<div id=\'tt4\'><ul> ' + lival + '</ul></div>';
                      if ($('#tt4').length > 0) {
                        $('#tt4').remove();
                      }
                      $('input[name=\'fuliaoValue[]\']:eq(' + index + ')').after(datadiv2);
                      $('#tt4').addClass('list_box').css('width', '85px');
                      $('#tt4 ul li').each(function () {
                        $(this).html(value + '' + $(this).html());
                      });
                      $('#tt4 ul li').on('click', function () {
                        var html = $(this).html();
                        if (html.indexOf('span') != - 1) {
                          html = html.substring(0, html.indexOf('span') - 1)
                        }
                        $('input[name=\'fuliaoValue[]\']:eq(' + index + ')').val(html);
                        $('#tt4').hide();
                      }).mousemove(function () {
                        $(this).css('cursor', 'pointer');
                      }).mouseout(function () {
                        $(this).css('cursor', 'none');
                      });
                    }
                  }
                } else if (value.indexOf('适') != - 1 || value.indexOf('适量') != - 1 || value.indexOf('少') != - 1 || value.indexOf('少许') != - 1) {
                  var lival = $('input[name=\'tthide3\']').val();
                  if (lival.indexOf('适') != - 1 || lival.indexOf('适量') != - 1 || lival.indexOf('少') != - 1 || lival.indexOf('少许') != - 1) {
                    if (fuVal != value) {
                      var datadiv2 = '';
                      if ((value.indexOf('适') != - 1 || value.indexOf('适量') != - 1) && (lival.indexOf('适') != - 1 || lival.indexOf('适量') != - 1)) {
                        var $lival = $('<ul>' + lival + '</ul>');
                        $lival.find('li').each(function () {
                          var li = $(this).html();
                          if (li.indexOf('适') != - 1 || li.indexOf('适量') != - 1) {
                            datadiv2 = '<div id=\'tt4\'><ul><li>' + $(this).html() + '</li></ul></div>';
                            return;
                          }
                        });
                      }
                      if ((value.indexOf('少') != - 1 || value.indexOf('少许') != - 1) && (lival.indexOf('少') != - 1 || lival.indexOf('少许') != - 1)) {
                        var $lival = $('<ul>' + lival + '</ul>');
                        $lival.find('li').each(function () {
                          var li = $(this).html();
                          if (li.indexOf('少') != - 1 || li.indexOf('少许') != - 1) {
                            datadiv2 = '<div id=\'tt4\'><ul><li>' + $(this).html() + '</li></ul></div>';
                            return;
                          }
                        });
                      }
                      if ($('#tt4').length > 0) {
                        $('#tt4').remove();
                      }
                      $('input[name=\'fuliaoValue[]\']:eq(' + index + ')').after(datadiv2);
                      $('#tt4').addClass('list_box').css('width', '85px');
                      $('#tt4 ul li').on('click', function () {
                        $('input[name=\'fuliaoValue[]\']:eq(' + index + ')').val($(this).html().substring(0, $(this).html().indexOf('<span>')));
                        $('#tt4').hide();
                      }).mousemove(function () {
                        $(this).css('cursor', 'pointer');
                      }).mouseout(function () {
                        $(this).css('cursor', 'none');
                      });
                    }
                  }
                } else {
                  $('#tt4').hide();
                }
                fuVal = value;
              }, 200);
            }).blur(function () {
              clearInterval(sss);
            });
          },
          error: function () {
            alert('fail');
          }
        });
      }
      if (name == '' || name.length == 0) {
        if ($('#tt').length > 0) {
          $('#tt').remove();
        }
        if ($('#tt3').length > 0) {
          $('#tt3').remove();
        }
      }
      paramname = name;
    }
  });
  var interval = '';
  $('input[name=\'zhuliao[]\']').each(function (index) {
    $(this).focus(function () {
      interval = setInterval('$.getdata(\'zhu\',' + index + ')', 200);
    }).blur(function () {
      clearInterval(interval);
    });
  });
  var interval2 = '';
  $('input[name=\'fuliao[]\']').each(function (index) {
    $(this).focus(function () {
      interval2 = setInterval('$.getdata(\'fu\',' + index + ')', 200);
    }).blur(function () {
      clearInterval(interval2);
    });
  });
  $('*').click(function () {
    if ($('#tt').length > 0) {
      var csstt1 = $('#tt').css('display');
      if ('block' == csstt1) {
        $('#tt').hide();
      }
    }
    if ($('#tt2').length > 0) {
      var csstt2 = $('#tt2').css('display');
      if ('block' == csstt2) {
        $('#tt2').hide();
      }
    }
    if ($('#tt3').length > 0) {
      var csstt3 = $('#tt3').css('display');
      if ('block' == csstt3) {
        $('#tt3').hide();
      }
    }
    if ($('#tt4').length > 0) {
      var csstt4 = $('#tt4').css('display');
      if ('block' == csstt4) {
        $('#tt4').hide();
      }
    }
  });
  /* var rs = '';
		  $("input[name='taginfo']").focus(function(){
			  var zhuVal ='';
			 $("input[name='zhuliao[]']").each(function(){
				 zhuVal+=$(this).val()+";";
			 });
			 if(zhuVal.length !=0){
				 zhuVal = zhuVal.substring(0,zhuVal.length-1);
			 }
			 
			 var fuVal ='';
			 $("input[name='fuliao[]']").each(function(){
				 fuVal+=$(this).val()+";";
			 });
			 if(fuVal.length !=0){
				 fuVal = fuVal.substring(0,fuVal.length-1);
			 }
			 var name = zhuVal+";"+fuVal;
				if(rs != name ){
				 $.ajax({
				      type: 'get',
				      async: true,
				      url: 'http://192.168.88.247:8080/dietservice/rest/queryDisease',
				      data: 'token=c8a6aed59bc175af999261f64e0e02aaa2d60476d6bd2194ac5241ba6f34513193fb01dc3e43d662&name=' + name,
				      dataType: 'jsonp', //数据类型为jsonp  
				      jsonp: 'jsonpCallback', //服务端用于接收callback调用的function名的参数  
				      success: function (data) {
				        var str = '';
				        $.each(data.data, function (index, content) {
				          str += '<a class="cooktag" info="upfood" href="javascript:void(0)" name="douguo123_zj">' + content.name + '</a>';
				        });
				       	if($("a[name='douguo123_zj']").length > 0){
				       		$("a[name='douguo123_zj']").remove();
				       	}
						$("div[class='hotap mb15 clearfix']").append($(str));
				      },
				      error: function () {
				        alert('fail');
				      }
				    });
				}
			  rs = name;
		  });*/
});