encode脚本和normal脚本混用的问题与解决方法


半年前第一次做脚本编码的时候,由于没有什么使用经验,于是在51js上询问了一下encode脚本和normal脚本混用是否有什么问题呢?结果没有得到任何有建设性的意见,这也至少说明了两个问题,一是没有人在意,二是就没有什么问题嘛。当然我更乐意于接受后一种结果,就开始了encode脚本和normal脚本的混合使用。

在这样的理解下做了很多的脚本,似乎也真的没有出现过什么问题,于是更加笃信自己当初的判断。结果又一次被IE暗算了,encode后的脚本和normal的脚本混和使用不是没有问题,也不是都有问题,只是在特定的条件下会出问题,真是晕死。看下面这个示例:

复制代码 代码如下:

<html>
<head>
<title>JScript Encode Research</title>
<meta name="author" content="birdshome@cnblogs" />
</head>
<body>
<script language="jscript.encode" type="text/jscript.encode">
#@~^8gAAAA==~,P~,P,Pr(L^Ycw.WDWOza+Rtn/klo~xP6E mOkGUv#@#@&,~P,P~~, @#@&~,P~P,~,P~,P,lVDDcB}4%+1Y 2MWYKOXa+Rtnd/moBbi@#@&,P~P,~P,8I@#@&PP~~,P~P@#@&,P~,P,PP}4NnmDR\+k/CLP',WE mYbGU`*@#@&P~P~~,P~ @#@&P,P~~,PP~~,P~l^nMYcEr(L+1Yc\+k/CoBbI@#@&P,~P,PP,NIGjkAAA==^#~@
</script>
<script language="jscript.encode" type="text/jscript.encode">
#@~^FgEAAA==~,P~,P,P0!x1OkKx~2mG[`#,`8@#@&@#@&~~P,P,P~2U^KNnRa.WDWOza+R\nk/Co~{PW!x1YkKxvb@#@&P~P,P~~, @#@&~P,PP,~~P,P,.kOndkU+vv2 mG[Rw.GDWOXancHnk/mo+E#p@#@&,P~P,P~~)i@#@&@#@&,PP,~~P,2 mGNn t+d/mL+,'~W!xmOrKxc#@#@&,P~,P,PPP@#@&~P,P~P,P~~,PMrYSk ncBAx1W[+ \/dlTnB*i@#@&,PP~~,P~8p~,V0MAAA==^#~@
</script>
<script language="jscript" type="text/jscript">
function Normal() {}
Normal.prototype.Message = function()
{
WriteLine('Normal.prototype.Message');
};
Normal.Message = function()
{
WriteLine('Normal.Message');
};
</script>
<script language="jscript" type="text/jscript">
var msg = '.prototype.Message" Fail.<br>';
function WriteLine(msg) { document.write(msg + '<br><br>'); }

var o = new Object();
try { o.Message(); }
catch(e) { WriteLine('Call "Object' + msg + e.message); }
try { Object.Message(); }
catch(e) { WriteLine('Call "Object.Message" Fail. <br>' + e.message); }

var e = new Encode();
try { e.Message(); }
catch(e) { WriteLine('Call "Encode' + msg + e.message); }
Encode.Message();

var n = new Normal();
try { n.Message(); }
catch(e) { WriteLine('Call "Normal' + msg + e.message); }
Normal.Message();
</script>
</body>
</html>

把上面的代码存为一个*.htm文件,打开后得到结果为:

Call "Object.prototype.Message" Fail.
Object doesn't support this property or method
Call "Object.Message" Fail.
Object doesn't support this property or method
Encode.prototype.Message
Encode.Message
Normal.prototype.Message
Normal.Message
上面那两段jscript.encode的代码很简单,就是: Object.prototype.Message = function()
{
alert('Object.prototype.Message');
};
Object.Message = function()
{
alert('Object.Message');
};
function Encode() {}
Encode.prototype.Message = function()
{
WriteLine('Encode.prototype.Message');
};
Encode.Message = function()
{
WriteLine('Encode.Message');
};
如果我们把上面两段代码替换那个html中的两段jscript.encode的代码,后面的运行将不会出任何异常,会得到这样的输出: Object.prototype.Message
Object.Message
...
上面这些代码实例的试验,已经详细的说明了encode脚本代码的问题。就是,不能在非编码脚本中,引用编码脚本中导入到JScript内置对象上的原型(prototype)方法和静态方法。上面示例中的Object就是JScript的一个内置对象,我们分别导入了一个prototpye方法和静态方法Message()。而对于非内置对象Encode,我们在已编码代码中导入的prototype和static方法,都可以在非编码代码中正常的访问。

那么怎么访问内置对象的导入方法呢?其实解决起来也不复杂,只是比较繁琐。我们需要使用一些wrapper方法,把他们和被编码的代码放在一起,就可以在非编码代码中访问了,比如上面的Object的导入,我们可以这样包装它:

Object.prototype.Message = function()
{
WriteLine('Object.prototype.Message');
};
Object.Message = function()
{
WriteLine('Object.Message');
};
var obj = new Object();

function ObjectPrototypeMessage()
{
obj.Message();
}
function ObjectMessage()
{
Object.Message();
}
这时,我们就可以通过ObjectPrototypeMessage和ObjectMessage这样的wrapper方法访问到已编码代码中内置对象的导入方法了。
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3