文章标签 ‘JS’
20147 月8

JAVA:网易微博模拟登陆

网易微博登陆验证,第一次请求使用BASE64加密、第二次请求使用MD5+RSA加密,比较变态,于是使用JAVA+JS相结合的方式,调用其JS方法得到加密字符串。

/core1.7.0.js 是经过处理的,删掉几行在JAVA引用中会报错的浏览器对象。

 

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.velocity.util.StringUtils;
import org.nutz.lang.Files;
import org.nutz.lang.util.ClassTools;
import org.nutz.repo.Base64;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Wizzer on 14-7-7.
 */
public class Netease {
    static String index_url = "http://t.163.com/session";
    static String login1_url = "http://reg.163.com/services/httpLoginExchgKeyNew";
    static String login2_url = "http://reg.163.com/httpLoginVerifyNew.jsp";
    static String status_url = "http://t.163.com/share/check/status";
    UrlUtil urlUtil = new UrlUtil();

    public static void main(String[] args) {
        CookieStore cookieStore = new Netease().login("email", "password");
    }

    public CookieStore login(String userid, String password) {
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(login1_url + "?rnd=" + Base64.encodeToString(userid.getBytes(), true) + "&jsonp=setLoginStatus");
            get.setHeader("Accept", "*/*");
            get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36");
            HttpResponse response = client.execute(get);
            int code = response.getStatusLine().getStatusCode();
            if (code == 200) {
                InputStream in = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line = "", res = "";
                while (null != (line = reader.readLine())) {
                    res += line;
                }
                System.out.println("res:::" + res);
                if (res.contains("200")) {
                    String[] str = StringUtils.split(urlUtil.getStr(res, "setLoginStatus(\"", "\")"), "\\n");
                    String o = str[1], h = str[2];
                    ScriptEngineManager sem = new ScriptEngineManager();
                    ScriptEngine se = sem.getEngineByName("javascript");
                    se.eval(getJs());
                    String jiami = "";
                    if (se instanceof Invocable) {
                        Invocable invoke = (Invocable) se;
                        jiami = invoke.invokeFunction("getCode",
                                password, o, h).toString();

                        System.out.println("jiami = " + jiami);
                    }

                    DefaultHttpClient client2 = new DefaultHttpClient();
                    client2.setCookieStore(client.getCookieStore());
                    HttpGet get2 = new HttpGet(login2_url + "?rcode=" + jiami + "&product=t&jsonp=setLoginStatus&savelogin=0&username=" + userid);
                    get2.setHeader("Accept", "*/*");
                    get2.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36");
                    HttpResponse response2 = client2.execute(get2);
                    int code2 = response2.getStatusLine().getStatusCode();
                    if (code2 == 200) {
                        InputStream in2 = response2.getEntity().getContent();
                        BufferedReader reader2 = new BufferedReader(new InputStreamReader(in2));
                        String line2 = "", res2 = "";
                        while (null != (line2 = reader2.readLine())) {
                            res2 += line2;
                        }
                        System.out.println("res2:::" + res2);
                        if (res.contains("200")) {
                            return client2.getCookieStore();
                        }
                    }
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private String getJs() {
        String jscontent = Files.read(ClassTools.getClassLoader().getResource("").getPath() + "netease" + "/core1.7.0.js");
        jscontent += "function getCode(p,o,h){\n" +
                "\t\t\t\tvar l=new RSAKey();\n" +
                "\t\t\t\tl.setPublic(h,o);\n" +
                "\t\t\t\treturn l.encrypt(getMd5(p));\t\t\t\t\n" +
                "   }";
        return jscontent;
    }
}
201311 月7

JS+CSS 幻灯片下载效果非常好

QQ截图20131107163030

使用时候注意两处,
一是图片宽度、高度要固定,
二是如果图片宽度变了,那s.js也要改成一样的。

下载地址:

http://pan.baidu.com/s/1eiYiz

20127 月31

JS:实现复选框上下级联动

function sel(obj){
         var id=obj.value;
         var qx=document.getElementsByName("id");
         for(var i = 0; i < qx.length; i ++)
           {
               if(qx[i].type == "checkbox")
                {
                  var v=qx[i].value;
                  if(v!=""&&v.length>id.length&&v.startWith(id)){
                      if(obj.checked){
                          qx[i].checked=true;
                      } else{
                           qx[i].checked=false;
                      }
                  }
                   if(v!=""&&v.length<id.length&&id.startWith(v)){
                      if(obj.checked){
                      } else{
                           qx[i].checked=false;
                      }
                  }

                }
           }
<input onclick="sel(this)" type="checkbox" name="id" value="0001" />0001

<input onclick="sel(this)" type="checkbox" name="id" value="00010001" />00010001

<input onclick="sel(this)" type="checkbox" name="id" value="00010002" />00010002

<input onclick="sel(this)" type="checkbox" name="id" value="00010003" />00010003
20121 月31

解决思路:通过百度地图JS API将经纬度反解析为位置信息

使用百度地图JS API,制作一个自刷新页面,将从数据库查询出的经纬度作为一个坐标点传给百度API JS,当获得位置信息后执行JS里的AJAX方法访问另外一个页面,将地址保存入库。 

<script language=”JavaScript”>function myrefresh(){ window.location.reload(force=true);}setTimeout(‘myrefresh()’,1000);</script>

————————————————–

window.location.reload(force=true)

绕过缓存,从服务器下载文档。

window.location.reload(force=false)

这个是默认的,检测服务器上文档是否改变,如没有改变,就从缓存调出。

 

ps:因google反地址解析API有频率限制,所以只好用百度地图了,没有找到百度地图URL形式的API,只能另辟蹊跷。:)

20108 月10

开源的JS JAVA工作流设计器

XiorkFlow  这个设计器UI还是不错的,可以自动生成XML文件,只是节点和连接线如果能双击弹出窗口设置相关属性就好了,没办法,只能进行二次开发了。。:(

谁有更好的开源工作流设计器吗?

下载地址:http://www.pudn.com/downloads78/sourcecode/java/detail298487.html

其他不多述了,自己下了部署下,可以看看效果。

20108 月5

JS获取URL传递的参数值

function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return unescape(r[2]);
        return null;
    }
20108 月1

JS 实现DIV随浏览器窗口大小变化

以下是宽度变化的示例,高度也可参考:

<script type="text/javascript">
    function initWidth() {
        var bodyWidth = document.compatMode == "BackCompat"?document.body.clientWidth:document.documentElement.clientWidth;
        document.getElementById("Content").style.width = bodyWidth - 180;
    }
    initWidth();
    if (document.attachEvent) {
        window.attachEvent('onresize', initWidth);
    } else {
        window.addEventListener('resize', initWidth, false);
    }
</script>
20107 月22

在移植不同JS框架遇到报错的问题

一般是 $ 函数冲突造成的,例如:

function $() {
  var results = [], element;
  for (var i = 0; i < arguments.length; i++) {
    element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    results.push(Element.extend(element));
  }
  return results.reduce();
}

 和

var $ = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};

function $(ele) {
  if (typeof(ele) == 'string'){
    ele = document.getElementById(ele)
    if(!ele){
  		return null;
    }
  }
  if(ele){
  	Core.attachMethod(ele);
	}
  return ele;
}

等。。。。找出问题费了一定功夫。。:(

20107 月16

JS:cookies 存、取、删除实例

function SetCookie(name,value)
{
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
20107 月7

实现鼠标拖动div交换位置

<html>
<head>
<title>月影</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style>
body {margin:0px;padding:0px;font-size:12px;text-align:center;}
body > div {text-align:center; margin-right:auto; margin-left:auto;}
.content{width:900px;}
.content .left{
        float:left;
        width:20%;
        border:1px solid #FF0000;
        margin:3px;
}
.content .center{float:left;border:1px solid #FF0000;margin:3px;width:57%}
.content .right{float:right;width:20%;border:1px solid #FF0000;margin:3px}
.mo{height:auto;border:1px solid #CCC;margin:3px;background:#FFF}
.mo h1{background:#ECF9FF;height:18px;padding:3px;cursor:move}
.mo .nr{height:80px;border:1px solid #F3F3F3;margin:2px}
h1{margin:0px;padding:0px;text-align:left;font-size:12px}
</style>
<script>
var dragobj={}
window.onerror=function(){return false}
function on_ini(){
        String.prototype.inc=function(s){return this.indexOf(s)>-1?true:false}
        var agent=navigator.userAgent
        window.isOpr=agent.inc("Opera")
        window.isIE=agent.inc("IE")&&!isOpr
        window.isMoz=agent.inc("Mozilla")&&!isOpr&&!isIE
        if(isMoz){
                Event.prototype.__defineGetter__("x",function(){return this.clientX+2})
                Event.prototype.__defineGetter__("y",function(){return this.clientY+2})
        }
        basic_ini()
}
function basic_ini(){
        window.$=function(obj){return typeof(obj)=="string"?document.getElementById(obj):obj}
        window.oDel=function(obj){if($(obj)!=null){$(obj).parentNode.removeChild($(obj))}}
}
window.onload=function(){
        on_ini()
        var o=document.getElementsByTagName("h1")
        for(var i=0;i<o.length;i++){
                o[i].onmousedown=function(e){
                        if(dragobj.o!=null)
                                return false
                        e=e||event
                        dragobj.o=this.parentNode
                        dragobj.xy=getxy(dragobj.o)
                        dragobj.xx=new Array((e.x-dragobj.xy[1]),(e.y-dragobj.xy[0]))
                        dragobj.o.style.width=dragobj.xy[2]+"px"
                        dragobj.o.style.height=dragobj.xy[3]+"px"
                        dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
                        dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
                        dragobj.o.style.position="absolute"
                        var om=document.createElement("div")
                        dragobj.otemp=om
                        om.style.width=dragobj.xy[2]+"px"
                        om.style.height=dragobj.xy[3]+"px"
                        dragobj.o.parentNode.insertBefore(om,dragobj.o)
                        return false
                }
        }
}
document.onselectstart=function(){return false}
window.onfocus=function(){document.onmouseup()}
window.onblur=function(){document.onmouseup()}
document.onmouseup=function(){
        if(dragobj.o!=null){
                dragobj.o.style.width="auto"
                dragobj.o.style.height="auto"
                dragobj.otemp.parentNode.insertBefore(dragobj.o,dragobj.otemp)
                dragobj.o.style.position=""
                oDel(dragobj.otemp)
                dragobj={}
        }
}
document.onmousemove=function(e){
        e=e||event
        if(dragobj.o!=null){
                dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
                dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
                createtmpl(e)
        }
}
function getxy(e){
        var a=new Array()
        var t=e.offsetTop;
        var l=e.offsetLeft;
        var w=e.offsetWidth;
        var h=e.offsetHeight;
        while(e=e.offsetParent){
                t+=e.offsetTop;
                l+=e.offsetLeft;
        }
        a[0]=t;a[1]=l;a[2]=w;a[3]=h
  return a;
}
function inner(o,e){
        var a=getxy(o)
        if(e.x>a[1]&&e.x<(a[1]+a[2])&&e.y>a[0]&&e.y<(a[0]+a[3])){
                if(e.y<(a[0]+a[3]/2))
                        return 1;
                else
                        return 2;
        }else
                return 0;
}
function createtmpl(e){
        for(var i=0;i<12;i++){
                if($("m"+i)==dragobj.o)
                        continue
                var b=inner($("m"+i),e)
                if(b==0)
                        continue
                dragobj.otemp.style.width=$("m"+i).offsetWidth
                if(b==1){
                        $("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i))
                }else{
                        if($("m"+i).nextSibling==null){
                                $("m"+i).parentNode.appendChild(dragobj.otemp)
                        }else{
                                $("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i).nextSibling)
                        }
                }
                return
        }
        for(var j=0;j<3;j++){
                if($("dom"+j).innerHTML.inc("div")||$("dom"+j).innerHTML.inc("DIV"))
                        continue
                var op=getxy($("dom"+j))
                if(e.x>(op[1]+10)&&e.x<(op[1]+op[2]-10)){
                        $("dom"+j).appendChild(dragobj.otemp)
                        dragobj.otemp.style.width=(op[2]-10)+"px"
                }
        }
}
</script>
</head>
<body>
<div>
        <div id=dom0>
                <div id=m0>
                        <h1>dom0</h1>
                        <div></div>
                </div>
                <div id=m1>
                        <h1>dom1</h1><div></div>
                </div>
                <div id=m2><h1>dom2</h1><div></div></div>
                <div id=m3><h1>dom3</h1><div></div></div>
        </div>
        <div id=dom1>
                <div id=m4><h1>dom4</h1><div></div></div>
                <div id=m5><h1>dom5</h1><div></div></div>
                <div id=m6><h1>dom6</h1><div></div></div>
                <div id=m7><h1>dom7</h1><div></div></div>
        </div>
        <div id=dom2>
                <div id=m8><h1>dom8</h1><div></div></div>
                <div id=m9><h1>dom9</h1><div></div></div>
                <div id=m10><h1>dom10</h1><div></div></div>
                <div id=m11><h1>dom11</h1><div></div></div>
        </div>
</div>
</body>
</html>