网上说微信中有个神奇代码faxinxi.la能够分享页面功能看,看了确实好奇,其实网上也说这就是一个网址。它通过微信的useragetn后台分析micromessage来限制之能在微信的内置浏览器中使用。通过模拟微信模拟器,查看他的代码,搞清了他的基本实现原理。他的分享也是利用了微信里的WeixinJsBridge来实现的。
当你选择某个功能,如贺卡,里的发送按钮时他执行如下代码 _card._post.
String.prototype.innerText=function(){return this.replace(/(<[^>]+>)|( )/ig,"");};
var _card={
_post:function(){
// 获取title标题 content内容 author发送人等内容并进行判断
var _title=$("card_title").innerHTML.innerText().trim(),_content=$("card_content").innerHTML.replace(/(<(br|div|\/div)>)+/ig,"\n").innerText(),_author=$("card_author").innerHTML.innerText().trim();
if(_title=="" || _title=="收卡人"){_system._toast("没有输入收卡人名字");return;}
if(_title.len()>20){_system._toast("收卡人名字在20字节以内");return;}
if(_content.len()<10){_system._toast("祝福语太短了");return;}
if(_content.len()>200){_system._toast("祝福语太长了");return;}
if(_author=="" || _author=="署名"){_system._toast("请署上你的大名");return;}
if(_author.len()>20){_system._toast("署名请在20字节以内");return;}
//之后调用_$,_$的意思是一个函数意思是发送连接进行后台存储,将你填写的信息保存下来,并回调调用 _card._ok 方法
_$("/servlet/card","type="+_cardType+"&title="+_title.encode()+"&content="+_content.encode()+"&author="+_author.encode(),"请稍候","_card._ok");
},
_ok:function(json){
//如果保存成功返回状态正常,则初始化一些分享的信息,包括分享的内容标题,图标,分享的链接,分享的描述。
if(json.state=="0"){_system._toast("你填写的内容有问题");return;}
dataForWeixin.MsgImg="http://img1.faxinxila.net/images/card_msg.png";
dataForWeixin.TLImg="http://img1.faxinxila.net/images/card.png";
dataForWeixin.path="card/"+_cardType+"/"+json.id;
dataForWeixin.title="收到一张来自"+$("card_author").innerHTML.innerText().trim()+"的"+_cardName;
dataForWeixin.desc=$("card_content").innerHTML.innerText().trim().left(88);
dataForWeixin.callback=function(){
_$("/servlet/share?kind=card&id="+json.id,"","");
};
// _system._guide()是调用另一个方法,显示指引选择微信分享的弹层
_system._guide();
}
};
window.onload=function(){$("card_body").show();$("card_loading").hide();};
String.prototype.len=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;};
String.prototype.trim=function(){return this.replace(/(^ *)|( *$)/g, "");};
String.prototype.encode=function(){return escape(this.replace(/·/g," ")).replace(/\+/g,"%2B");};
String.prototype.left=function(length){
if(this.len()>length){
var _temp=this;
_temp=_temp.replace(/([^\x00-\xff])/g,"$1>");
_temp=_temp.substring(0,length-2)+"..";
return _temp.replace(/>/g,"");
}else{
return this.toString();
}
};
Element.prototype.show=function(){this.style.display="block";};
Element.prototype.hide=function(){this.style.display="none";};
Element.prototype.center=function(top){
this.style.left=(_system._scroll().x+_system._zero(_system._client().bw-this.offsetWidth)/2)+"px";
this.style.top=(top?top:(_system._scroll().y+_system._zero(_system._client().bh-this.offsetHeight)/2))+"px";
};
var _system={
_client:function(){
return {w:document.documentElement.scrollWidth,h:document.documentElement.scrollHeight,bw:document.documentElement.clientWidth,bh:document.documentElement.clientHeight};
},
_scroll:function(){
return {x:document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft,y:document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop};
},
_cover:function(show){
if(show){
$("cover").show();
$("cover").style.width=(this._client().bw>this._client().w?this._client().bw:this._client().w)+"px";
$("cover").style.height=(this._client().bh>this._client().h?this._client().bh:this._client().h)+"px";
}else{
$("cover").hide();
}
},
_loading:function(text){
if(text){
this._cover(true);
$("loading").show();
$("loading_text").innerHTML=text;
$("loading").center();
window.onresize=function(){_system._cover(true);$("loading").center();};
}else{
this._cover(false);
$("loading").hide();
window.onresize=null;
}
},
_toast:function(text,fun){
$("toast").show();
$("toast").innerHTML=text;
$("toast").center();
setTimeout(function(){
$("toast").hide();
if(fun){(fun)();}
},3*1000);
},
_ok:function(text,fun){
$("ok").show();
$("ok_text").innerHTML=text;
$("ok").center();
window.onresize=function(){$("ok").center();};
setTimeout(function(){
window.onresize=null;
$("ok").hide();
(fun)();
},2*1000);
},
//分享的弹层,显示让你分享网址。
_guide:function(click){
this._cover(true);
$("guide").show();
$("guide").style.top=(_system._scroll().y+5)+"px";
window.onresize=function(){_system._cover(true);$("guide").style.top=(_system._scroll().y+5)+"px";};
if(click){$("cover").onclick=function(){
_system._cover();
$("guide").hide();
$("cover").onclick=null;
window.onresize=null;
};}
},
_zero:function(n){
return n<0?0:n;
},
_forbidden:function(text){
return text.match(/(老市长|bxl|b市长|flg)/)!=null;
},
_shareUrl:function(){
var domain=["url.lc","www.faxinxi.la","www.faxingxi.la","www.kanxinxi.la","www.zhaoxinxi.la","www.zhaofuwu.la","faxinxila.com","faxinxila.net","kanxinxila.com","kanxinxila.net","www.messagehelper.com","www.messagehelper.net","tongmang.com","tongmang.net","tuimang.com","teimang.com","rengmei.com","fxxl.xiaozhongapp.com","fxxl.xiaozongapp.com","showarticle.cc","showparty.cc","showvote.cc","showmore.cc","jootun.com","showinfo.cc","showdetail.cc","showhot.cc","shownew.cc","getall.cc","gethot.cc","getnew.cc","getone.cc","getdetail.cc","getmessage.cc","myvote.cc","myarticle.cc","newmessage.cc","newarticle.cc","newvote.cc","newparty.cc","sharemessage.cc","sharearticle.cc","sharevote.cc","shareparty.cc","showpost.cc","getpost.cc","newpost.cc","sharepost.cc","readit.cc","jointhem.cc","clickit.cc","showit.cc","allarticle.cc","allcard.cc","allparty.cc","allvote.cc","bestparty.cc","bestvote.cc","bestarticle.cc","bestmessage.cc","yourarticle.cc","yourvote.cc","yourparty.cc","yourcard.cc","yourmessage.cc","loveparty.cc","lovevote.cc","lovecard.cc","lovearticle.cc","ourparty.cc","ourvote.cc","ourcard.cc","ourarticle.cc","goodarticle.cc","goodvote.cc","goodparty.cc","goodmessage.cc","joootun.com","jooootun.com","joooootun.com"],path=["detail.jsp?r=","show.asp?l=","index.do?u=","info.do?i="];
return "http://"+domain[parseInt(domain.length*Math.random())]+"/"+path[parseInt(path.length*Math.random())]+dataForWeixin.path;
}
};
(function(){
//由于分享的东西已经初始化完成,所以这里直接调用微信的分享功能,完成对配置的连接的分享。
var onBridgeReady=function(){
WeixinJSBridge.on('menu:share:appmessage', function(argv){
WeixinJSBridge.invoke('sendAppMessage',{
"appid":dataForWeixin.appId,
"img_url":dataForWeixin.MsgImg,
"img_width":"120",
"img_height":"120",
"link":_system._shareUrl(),
"desc":dataForWeixin.desc,
"title":dataForWeixin.title
}, function(res){(dataForWeixin.callback)();});
});
WeixinJSBridge.on('menu:share:timeline', function(argv){
(dataForWeixin.callback)();
WeixinJSBridge.invoke('shareTimeline',{
"img_url":dataForWeixin.TLImg,
"img_width":"120",
"img_height":"120",
"link":_system._shareUrl(),
"desc":dataForWeixin.desc,
"title":dataForWeixin.title
}, function(res){});
});
WeixinJSBridge.on('menu:share:weibo', function(argv){
WeixinJSBridge.invoke('shareWeibo',{
"content":dataForWeixin.title,
"url":_system._shareUrl()
}, function(res){(dataForWeixin.callback)();});
});
WeixinJSBridge.on('menu:share:facebook', function(argv){
(dataForWeixin.callback)();
WeixinJSBridge.invoke('shareFB',{
"img_url":dataForWeixin.TLImg,
"img_width":"120",
"img_height":"120",
"link":_system._shareUrl(),
"desc":dataForWeixin.desc,
"title":dataForWeixin.title
}, function(res){});
});
};
if(document.addEventListener){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if(document.attachEvent){
document.attachEvent('WeixinJSBridgeReady' , onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady' , onBridgeReady);
}
})();
var _$=function(url,parameters,loadingMessage,functionName){
var request=new XMLHttpRequest();
if(loadingMessage!=""){_system._loading(loadingMessage);}
var method="POST";
if(parameters==""){method="GET";parameters=null;}
request.open(method,url,true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.onreadystatechange=function(){
if(request.readyState==4){
if(loadingMessage != ""){_system._loading();}
if(request.status==200){
if(functionName){
try{
var json = eval("("+ request.responseText+")");
eval(functionName+"(json)");
}catch(e){}
}
}else{
if(loadingMessage != ""){_system._toast("发生意外错误,请稍候再试");}
}
}
};
request.send(parameters);
};
当你选择某个功能,如贺卡,里的发送按钮时他执行如下代码 _card._post.
String.prototype.innerText=function(){return this.replace(/(<[^>]+>)|( )/ig,"");};
var _card={
_post:function(){
// 获取title标题 content内容 author发送人等内容并进行判断
var _title=$("card_title").innerHTML.innerText().trim(),_content=$("card_content").innerHTML.replace(/(<(br|div|\/div)>)+/ig,"\n").innerText(),_author=$("card_author").innerHTML.innerText().trim();
if(_title=="" || _title=="收卡人"){_system._toast("没有输入收卡人名字");return;}
if(_title.len()>20){_system._toast("收卡人名字在20字节以内");return;}
if(_content.len()<10){_system._toast("祝福语太短了");return;}
if(_content.len()>200){_system._toast("祝福语太长了");return;}
if(_author=="" || _author=="署名"){_system._toast("请署上你的大名");return;}
if(_author.len()>20){_system._toast("署名请在20字节以内");return;}
//之后调用_$,_$的意思是一个函数意思是发送连接进行后台存储,将你填写的信息保存下来,并回调调用 _card._ok 方法
_$("/servlet/card","type="+_cardType+"&title="+_title.encode()+"&content="+_content.encode()+"&author="+_author.encode(),"请稍候","_card._ok");
},
_ok:function(json){
//如果保存成功返回状态正常,则初始化一些分享的信息,包括分享的内容标题,图标,分享的链接,分享的描述。
if(json.state=="0"){_system._toast("你填写的内容有问题");return;}
dataForWeixin.MsgImg="http://img1.faxinxila.net/images/card_msg.png";
dataForWeixin.TLImg="http://img1.faxinxila.net/images/card.png";
dataForWeixin.path="card/"+_cardType+"/"+json.id;
dataForWeixin.title="收到一张来自"+$("card_author").innerHTML.innerText().trim()+"的"+_cardName;
dataForWeixin.desc=$("card_content").innerHTML.innerText().trim().left(88);
dataForWeixin.callback=function(){
_$("/servlet/share?kind=card&id="+json.id,"","");
};
// _system._guide()是调用另一个方法,显示指引选择微信分享的弹层
_system._guide();
}
};
window.onload=function(){$("card_body").show();$("card_loading").hide();};
String.prototype.len=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;};
String.prototype.trim=function(){return this.replace(/(^ *)|( *$)/g, "");};
String.prototype.encode=function(){return escape(this.replace(/·/g," ")).replace(/\+/g,"%2B");};
String.prototype.left=function(length){
if(this.len()>length){
var _temp=this;
_temp=_temp.replace(/([^\x00-\xff])/g,"$1>");
_temp=_temp.substring(0,length-2)+"..";
return _temp.replace(/>/g,"");
}else{
return this.toString();
}
};
Element.prototype.show=function(){this.style.display="block";};
Element.prototype.hide=function(){this.style.display="none";};
Element.prototype.center=function(top){
this.style.left=(_system._scroll().x+_system._zero(_system._client().bw-this.offsetWidth)/2)+"px";
this.style.top=(top?top:(_system._scroll().y+_system._zero(_system._client().bh-this.offsetHeight)/2))+"px";
};
var _system={
_client:function(){
return {w:document.documentElement.scrollWidth,h:document.documentElement.scrollHeight,bw:document.documentElement.clientWidth,bh:document.documentElement.clientHeight};
},
_scroll:function(){
return {x:document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft,y:document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop};
},
_cover:function(show){
if(show){
$("cover").show();
$("cover").style.width=(this._client().bw>this._client().w?this._client().bw:this._client().w)+"px";
$("cover").style.height=(this._client().bh>this._client().h?this._client().bh:this._client().h)+"px";
}else{
$("cover").hide();
}
},
_loading:function(text){
if(text){
this._cover(true);
$("loading").show();
$("loading_text").innerHTML=text;
$("loading").center();
window.onresize=function(){_system._cover(true);$("loading").center();};
}else{
this._cover(false);
$("loading").hide();
window.onresize=null;
}
},
_toast:function(text,fun){
$("toast").show();
$("toast").innerHTML=text;
$("toast").center();
setTimeout(function(){
$("toast").hide();
if(fun){(fun)();}
},3*1000);
},
_ok:function(text,fun){
$("ok").show();
$("ok_text").innerHTML=text;
$("ok").center();
window.onresize=function(){$("ok").center();};
setTimeout(function(){
window.onresize=null;
$("ok").hide();
(fun)();
},2*1000);
},
//分享的弹层,显示让你分享网址。
_guide:function(click){
this._cover(true);
$("guide").show();
$("guide").style.top=(_system._scroll().y+5)+"px";
window.onresize=function(){_system._cover(true);$("guide").style.top=(_system._scroll().y+5)+"px";};
if(click){$("cover").onclick=function(){
_system._cover();
$("guide").hide();
$("cover").onclick=null;
window.onresize=null;
};}
},
_zero:function(n){
return n<0?0:n;
},
_forbidden:function(text){
return text.match(/(老市长|bxl|b市长|flg)/)!=null;
},
_shareUrl:function(){
var domain=["url.lc","www.faxinxi.la","www.faxingxi.la","www.kanxinxi.la","www.zhaoxinxi.la","www.zhaofuwu.la","faxinxila.com","faxinxila.net","kanxinxila.com","kanxinxila.net","www.messagehelper.com","www.messagehelper.net","tongmang.com","tongmang.net","tuimang.com","teimang.com","rengmei.com","fxxl.xiaozhongapp.com","fxxl.xiaozongapp.com","showarticle.cc","showparty.cc","showvote.cc","showmore.cc","jootun.com","showinfo.cc","showdetail.cc","showhot.cc","shownew.cc","getall.cc","gethot.cc","getnew.cc","getone.cc","getdetail.cc","getmessage.cc","myvote.cc","myarticle.cc","newmessage.cc","newarticle.cc","newvote.cc","newparty.cc","sharemessage.cc","sharearticle.cc","sharevote.cc","shareparty.cc","showpost.cc","getpost.cc","newpost.cc","sharepost.cc","readit.cc","jointhem.cc","clickit.cc","showit.cc","allarticle.cc","allcard.cc","allparty.cc","allvote.cc","bestparty.cc","bestvote.cc","bestarticle.cc","bestmessage.cc","yourarticle.cc","yourvote.cc","yourparty.cc","yourcard.cc","yourmessage.cc","loveparty.cc","lovevote.cc","lovecard.cc","lovearticle.cc","ourparty.cc","ourvote.cc","ourcard.cc","ourarticle.cc","goodarticle.cc","goodvote.cc","goodparty.cc","goodmessage.cc","joootun.com","jooootun.com","joooootun.com"],path=["detail.jsp?r=","show.asp?l=","index.do?u=","info.do?i="];
return "http://"+domain[parseInt(domain.length*Math.random())]+"/"+path[parseInt(path.length*Math.random())]+dataForWeixin.path;
}
};
(function(){
//由于分享的东西已经初始化完成,所以这里直接调用微信的分享功能,完成对配置的连接的分享。
var onBridgeReady=function(){
WeixinJSBridge.on('menu:share:appmessage', function(argv){
WeixinJSBridge.invoke('sendAppMessage',{
"appid":dataForWeixin.appId,
"img_url":dataForWeixin.MsgImg,
"img_width":"120",
"img_height":"120",
"link":_system._shareUrl(),
"desc":dataForWeixin.desc,
"title":dataForWeixin.title
}, function(res){(dataForWeixin.callback)();});
});
WeixinJSBridge.on('menu:share:timeline', function(argv){
(dataForWeixin.callback)();
WeixinJSBridge.invoke('shareTimeline',{
"img_url":dataForWeixin.TLImg,
"img_width":"120",
"img_height":"120",
"link":_system._shareUrl(),
"desc":dataForWeixin.desc,
"title":dataForWeixin.title
}, function(res){});
});
WeixinJSBridge.on('menu:share:weibo', function(argv){
WeixinJSBridge.invoke('shareWeibo',{
"content":dataForWeixin.title,
"url":_system._shareUrl()
}, function(res){(dataForWeixin.callback)();});
});
WeixinJSBridge.on('menu:share:facebook', function(argv){
(dataForWeixin.callback)();
WeixinJSBridge.invoke('shareFB',{
"img_url":dataForWeixin.TLImg,
"img_width":"120",
"img_height":"120",
"link":_system._shareUrl(),
"desc":dataForWeixin.desc,
"title":dataForWeixin.title
}, function(res){});
});
};
if(document.addEventListener){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if(document.attachEvent){
document.attachEvent('WeixinJSBridgeReady' , onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady' , onBridgeReady);
}
})();
var _$=function(url,parameters,loadingMessage,functionName){
var request=new XMLHttpRequest();
if(loadingMessage!=""){_system._loading(loadingMessage);}
var method="POST";
if(parameters==""){method="GET";parameters=null;}
request.open(method,url,true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.onreadystatechange=function(){
if(request.readyState==4){
if(loadingMessage != ""){_system._loading();}
if(request.status==200){
if(functionName){
try{
var json = eval("("+ request.responseText+")");
eval(functionName+"(json)");
}catch(e){}
}
}else{
if(loadingMessage != ""){_system._toast("发生意外错误,请稍候再试");}
}
}
};
request.send(parameters);
};