util.js 2.63 KB
/**
* 采集系统的工具库
*/
var config = require('./config');

var yasPath = config.yaPath;

var yasDomain = config.yasDomain;

//flash监测
exports.flashChecker = function ()
{
	var hasFlash = 0; //是否安装了flash
	var flashVersion = 0; //flash版本
	var isIE =/*@cc_on!@*/0; //是否IE浏览器
    var swf = null;
	if (isIE)
	{
		swf = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
		if (swf)
		{
			hasFlash = 1;
			flashVersion = swf.GetVariable("$version");
		}
	}
	else
	{
		if (navigator.plugins && navigator.plugins.length > 0)
		{
			swf = navigator.plugins["Shockwave Flash"];
			if (swf)
			{
				hasFlash = 1;
				flashVersion = swf.description.replace("Shockwave Flash",'');
			}
		}
	}
	return{
		f: hasFlash,
		v: flashVersion
	};
};

//hash算法
exports.Hash = function (str)
{
	var hash = 1,charCode = 0,idx;
	if(str)
	{
		hash = 0;
		for(idx = str.length - 1; idx >= 0; idx--)
		{
			charCode = str.charCodeAt(idx);
			hash = (hash << 6&268435455) + charCode+(charCode << 14);
			charCode = hash&266338304;
			if(charCode !== 0)
			{
				hash = hash ^ charCode>>21;
			}
		}
	}
	return hash;
};

//生成随机数
exports.Random = function ()
{
  return Math.round(Math.random() * 2147483647);
};

//hash客户端信息
exports.hashClientInfo = function ()
{
   var navigator = window.navigator;
   var history_length = window.history['length'];
   var arr = [
   navigator.appName,
   navigator.version,
   navigator.language,
   navigator.platform,
   navigator.userAgent,
   navigator.javaEnabled(),
   window.screen,
   window.screen.colorDepth,
   (window.document.cookie? window.document.cookie : ""),
   (window.document.referrer ? window.document.referrer : "")
   ];
   
   navigator = arr.join('');

   for(var len = navigator['length']; history_length > 0; )
   {
      navigator += history_length-- ^ len++;
   }
   return exports.Hash(navigator);
};

//浅层合并对象
exports.merge = function (obj1,obj2)
{
	var ret={};
	for(var attr in obj1)
	{
		ret[attr]=obj1[attr];
	}

	for(var attr2 in obj2)
	{
		ret[attr2]=obj2[attr2];
	}
	return ret;
};

//生成URL键值对
exports.genParam = function (obj)
{
  var arr = [];
  for(var key in obj)
  {
     arr.push(key+'='+obj[key]);
  }
  return arr.join('&');
};

//除去字符串前后的空格
exports.trim = function( text )
{
    if(String.prototype.trim)
    {
      return text === null?"": String.prototype.trim.call( text );
    }
    else
    {
        var trimLeft = /^\s+/;
        var trimRight = /\s+$/;
        var ret = '';
        if(text)
        {
			ret = text.toString().replace( trimLeft, "" );
			ret = ret.replace( trimRight, "" );
			return ret;
        }
	}
};