大家好,我们一起来研究一个信息采集系统的设计,如果您有更好的方案请在评论中阐述 谢谢
近期做了一个在线工具网站 http://www.webtool.cn/, 网站整体采用AJAX设计,全静态处理。
在项目中需要访问一些网络资源,并进行资源处理。
但是遇到了以下几个问题。
1.服务端处理压力过重,如果访问量较大 那么势必会影响整体速度;
2.如果使用AJAX去访问网络资源,那么会有跨域权限问题;
3.如果某工具的使用量比较大,那么以服务端IP去频繁爬某一网络资源很有可能会被封IP。
综合以上的问题,我们想到了用客户端去采集网络资源,并使用JS正则分析的方式去处理。
好,现在开始说说怎样来完成这一系列工作:
首先,请看看效果:
http://www.webtool.cn/tools/list/n2399997.htm
此工具名为网上购物价格比较,可以迅速实时的获取包括淘宝网、拍拍网、易趣网、京东网、当当网、卓越网在内的大型B2C,C2C购物网站数据,并进行数据处理
为客户提供一个商品比较的功能。
我们先看看使用的效果图
此列表是可以展开的,可以查看更多的商品,并且可以通过链接直接到达商品的内容页面。
怎么实现的呢?
第一步,javascript 通过AJAX 访问位于服务端的C#代码,获取返回的结果框架和JS函数,这些函数主要是用于提供ACTIVEX 调用的回调函数。
sys.tools.getResults(...)
第二步,C#返回responseText和脚本
来一段代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
2 {
3 string key = paras[ 4 ];
4 string price = paras[ 5 ];
5 string range = paras[ 6 ];
6 string [] sites = paras[ 7 ].Split( ' | ' );
7 string [] parms = paras[ 8 ].Split( ' | ' );
8 bool checkTaobao = (sites[ 0 ] == " 1 " ) ? true : false ;
9 bool checkPaipai = (sites[ 1 ] == " 1 " ) ? true : false ;
10 bool checkDangdang = (sites[ 2 ] == " 1 " ) ? true : false ;
11 bool checkJingdong = (sites[ 3 ] == " 1 " ) ? true : false ;
12 bool checkZhuoyue = (sites[ 4 ] == " 1 " ) ? true : false ;
13 bool checkEbay = (sites[ 5 ] == " 1 " ) ? true : false ;
14 bool showTack = (parms[ 0 ] == " 1 " ) ? true : false ;
15 bool showImg = (parms[ 1 ] == " 1 " ) ? true : false ;
16 bool showXinyong = (parms[ 2 ] == " 1 " ) ? true : false ;
17
18
19
20 string line = " search?q={0}&commend=all&sort=price-asc&style=list&isnew=2&viewIndex=7&source=search1&start_price={1}&end_price={2}&filter=reserve_price%5B{3}%2C{4}%5D&s={5}#J_FilterTabBar " ;
21 double prc = double .Parse(price);
22 double rag = double .Parse(range);
23 string price_start = Convert.ToString(prc - rag);
24 string price_end = Convert.ToString(prc + rag);
25 key = System.Web.HttpUtility.UrlDecode(key);
26 string linf = string .Format(line, System.Web.HttpUtility.UrlEncode(key, System.Text.Encoding.GetEncoding( " gb2312 " )), price_start, price_end, price_start, price_end, " 0 " );
27 BaseCollectActiveXObject axoTaobao = new BaseCollectActiveXObject();
28
29 axoTaobao.CallBack = " CreateTaobaoItems " ;
30 axoTaobao.CharSet = " gbk " ;
31 axoTaobao.Domain = " search.taobao.com " ;
32 axoTaobao.Flag = " taobao_item " ;
33 axoTaobao.Method = " get " ;
34 axoTaobao.Params = "" ;
35 axoTaobao.Path = linf;
36 axoTaobao.Port = " 1129 " ;
37 axoTaobao.Referer = "" ;
38 axoTaobao.RefUrl = " tools/sapi/tool.ashx " ;
39 axoTaobao.TID = DES.Encrypt( " 2399997 " );
40
41 base .AddResources(axoTaobao);
42
43 string ppath = " cgi-bin/comm_search?KeyWord={0}&PTAG=30415.1.1 " ;
44 ppath = string .Format(ppath, key);
45
46 BaseCollectActiveXObject axoPaiPai = new BaseCollectActiveXObject();
47 axoPaiPai.CallBack = " ProxyPaiPaiItems " ;
48 axoPaiPai.CharSet = " gb2312 " ;
49 axoPaiPai.Domain = " search.paipai.com " ;
50 axoPaiPai.Flag = " paipai_item " ;
51 axoPaiPai.Method = " get " ;
52 axoPaiPai.Params = "" ;
53 axoPaiPai.Path = ppath;
54 axoPaiPai.Port = " 8080 " ;
55 axoPaiPai.Referer = "" ;
56 axoPaiPai.RefUrl = " tools/sapi/tool.ashx " ;
57 axoPaiPai.TID = DES.Encrypt( " 2399997 " );
58 base .AddResources(axoPaiPai);
59
60 string jdPath = " Search?keyword={0}&psort=2&r={1}-{2} " ;
61 jdPath = string .Format(jdPath, key, price_start, price_end);
62
63 BaseCollectActiveXObject axoJD = new BaseCollectActiveXObject();
64 axoJD.CallBack = " CreateJDItems " ;
65 axoJD.CharSet = " gbk " ;
66 axoJD.Domain = " search.360buy.com " ;
67 axoJD.Flag = " jd_item " ;
68 axoJD.Method = " get " ;
69 axoJD.Params = "" ;
70 axoJD.Path = jdPath;
71 axoJD.Port = " 8080 " ;
72 axoJD.Referer = "" ;
73 axoJD.RefUrl = " tools/sapi/tool.ashx " ;
74 axoJD.TID = DES.Encrypt( " 2399997 " );
75 base .AddResources(axoJD);
76
77 string zyPath = " s/ref=nb_ss?url=search-alias%3Daps&keywords={0}&searchKind=keyword " ;
78 zyPath = string .Format(zyPath, key);
79
80 BaseCollectActiveXObject axoZY = new BaseCollectActiveXObject();
81 axoZY.CallBack = " CreateZYItems " ;
82 axoZY.CharSet = " utf-8 " ;
83 axoZY.Domain = " www.amazon.cn " ;
84 axoZY.Flag = " zy_item " ;
85 axoZY.Method = " get " ;
86 axoZY.Params = "" ;
87 axoZY.Path = zyPath;
88 axoZY.Port = " 8080 " ;
89 axoZY.Referer = "" ;
90 axoZY.RefUrl = " tools/sapi/tool.ashx " ;
91 axoZY.TID = DES.Encrypt( " 2399997 " );
92 base .AddResources(axoZY);
93 string ddPath = " mall/search_mall.php?sort=1&q={0}&cat=&lowp={1}&highp={2}&loc=&guan=&store= " ;
94 string ddk = "" ;
95 ddk = System.Web.HttpUtility.UrlEncode(key, System.Text.Encoding.GetEncoding( " gb2312 " ));
96 ddPath = string .Format(ddPath, ddk, price_start, price_end);
97 BaseCollectActiveXObject axoDD = new BaseCollectActiveXObject();
98 axoDD.CallBack = " CreateDDItems " ;
99 axoDD.CharSet = " gb2312 " ;
100 axoDD.Domain = " search.dangdang.com " ;
101 axoDD.Flag = " dd_item " ;
102 axoDD.Method = " get " ;
103 axoDD.Params = "" ;
104 axoDD.Path = ddPath;
105 axoDD.Port = " 8080 " ;
106 axoDD.Referer = "" ;
107 axoDD.RefUrl = " tools/sapi/tool.ashx " ;
108 axoDD.TID = DES.Encrypt( " 2399997 " );
109 base .AddResources(axoDD);
110
111 string epm = " keyword={0}&category=0 " ;
112 epm = string .Format(epm, System.Web.HttpUtility.UrlEncode(key, System.Text.Encoding.GetEncoding( " utf-8 " )));
113 BaseCollectActiveXObject axoEbay = new BaseCollectActiveXObject();
114 axoEbay.CallBack = " proxyEBayItems " ;
115 axoEbay.CharSet = " utf-8 " ;
116 axoEbay.Domain = " search.eachnet.com " ;
117 axoEbay.Flag = " ebay_item " ;
118 axoEbay.Method = " post " ;
119 axoEbay.Params = epm;
120 axoEbay.Path = " Search " ;
121 axoEbay.Port = " 8080 " ;
122 axoEbay.Referer = "" ;
123 axoEbay.RefUrl = " tools/sapi/tool.ashx " ;
124 axoEbay.TID = DES.Encrypt( " 2399997 " );
125 base .AddResources(axoEbay);
126
127
128 StringBuilder sb = new StringBuilder();
129 switch (paras[ 9 ])
130 {
131 case " frame " :
132 sb.Append( " <table class='sys_output_table sys_td_center' style='width:100%;'> " );
133 sb.Append( " <tr> " );
134 sb.Append( " <th>网站</th> " );
135 sb.Append( " <th>图片</th> " );
136
137 sb.Append( " <th>名称</th> " );
138 sb.Append( " <th>价格</th> " );
139 sb.Append( " <th>商家地址</th> " );
140 if (showXinyong)
141 {
142 sb.Append( " <th>信誉</th> " );
143 }
144 sb.Append( " <th>商家</th> " );
145 sb.Append( " </tr> " );
146
147 if (checkTaobao)
148 buildImageFrame(sb, " 淘宝网 " , showImg, " trTaobao " , " taobao_child " );
149 if (checkPaipai)
150 buildImageFrame(sb, " 拍拍网 " , showImg, " trPaipai " , " pp_child " );
151 if (checkEbay)
152 buildImageFrame(sb, " 易趣网 " , showImg, " trEBay " , " ebay_child " );
153 if (checkDangdang)
154 buildImageFrame(sb, " 当当网 " , showImg, " trDangdang " , " dd_child " );
155 if (checkJingdong)
156 buildImageFrame(sb, " 京东网 " , showImg, " trJingdong " , " jd_child " );
157 if (checkZhuoyue)
158 buildImageFrame(sb, " 卓越网 " , showImg, " trZuoyue " , " zy_child " );
159 sb.Append( " </table> " );
160 sb.Append( " <script> " );
161 sb.Append( " function ProxyPaiPaiItems(obj){ " );
162 sb.Append( " var mtsv; " );
163 sb.Append( " var data = obj.responseText.toString(); " );
164 sb.Append( " var index = data.indexOf('Location'); " );
165 sb.Append( " if(index > 0 && index < 500){ " );
166 sb.Append( " var ps = /location[\\s\\S]*?http:\\/\\/([^\\/]*?)\\/([\\s\\S]*?\\.html)/gi; " );
167 sb.Append( " if(mtsv = ps.exec(data)){ " ); // 如果匹配则说明还要转址
168 sb.Append( " var path = mtsv[2]; " ); // 首先需要执行字符串替换
169 sb.Append( " if(path){ " );
170 sb.Append( " path = path.replace(/-1-40-([\\d]{2})/,'-1-40-6'); " );
171 sb.Append( " path = path.replace(/4-3([\\s\\S]{3}-2-[0|2]--128-[0|1]-0)/gi,'4-3-- " + Convert.ToString( int .Parse(price_start) * 100 ) + " - " + Convert.ToString( int .Parse(price_end) * 100 ) + " -2-0--128-1-0'); " );
172 sb.Append( " path = path.replace(/\\s+?/g,''); " );
173 sb.Append( " sys.behavior.$doAX($('tid').value,mtsv[1],'8080',path,'get','','paipai_proxy','tools/sapi/tool.ashx','','gb2312','ProxyPaiPaiItems'); " );
174 sb.Append( " } " );
175 sb.Append( " } " );
176 sb.Append( " } " );
177 sb.Append( " else{ " );
178 sb.Append( " CreatePaipaiItems(data); " );
179 sb.Append( " } " );
180 sb.Append( " } " );
181 sb.Append( " function CreatePaipaiItems(data){ " );
182 sb.Append( " var res = data; " );
183 sb.Append( " var mts; " );
184 sb.Append( " var ppPatten = /<li[^\\\ " ] *? \\\ " item_list\\\ " > [\\s\\S] *?< span class = \\\ " name\\\ " >< a[ ^> ] *? href = \\\ " ([^\\\ " ] *? )\\\ " [^>]*?>([\\s\\S]*?)<\\/a>[\\s\\S]*?<img[^>]*?init_src=\\\ " ([ ^ \\\ " ]*?)\\\ " \\ /> [\\s\\S] *?< li class = \\\ " place\\\ " > ([ ^> ] *? ) < \\ / li > [\\s\\S] *?< li class = \\\ " price\\\ " > \\s *?< em > ([ ^< ] *? ) < \\ / em > [\\s\\S] *?< a[ ^> ] *? href = \ " ([^\\.\\\ " ] *? \\.paipai.com)\\\ " [^>]*?>([^<]*?)<\\/a>[\\s\\S]*?<\\/li>/gi; " );
185 sb.Append( " var x = 0; " );
186 sb.Append( " var Paipai_child_td_table = $crt('table'); " );
187 sb.Append( " while(mts = ppPatten.exec(res)){ " );
188 sb.Append( " if(x == 0){ " );
189 sb.Append( " $('trPaipai').cells[1].innerHTML = '<img src=\ "' + mts[3] + ' \ " />'; " );
190 sb.Append( " $('trPaipai').cells[2].innerHTML = '<a href=\ "' + mts[1] + ' \ " target=\ " _blank\ " >' + mts[2] + '</a>'; " );
191 sb.Append( " $('trPaipai').cells[3].innerHTML = mts[5]; " );
192 sb.Append( " $('trPaipai').cells[4].innerHTML = mts[4]; " );
193 sb.Append( " $('trPaipai').cells[5].innerHTML = '<a href=\ "' + mts[6] + ' \ " title=\ " 进入商铺\ " target=\ " _blank\ " >' + mts[7] + '</a>'; " );
194 sb.Append( " var Paipai_child = $('pp_child'); " );
195 sb.Append( " $('trPaipai').className='t_2399997_taobao_extend'; " );
196 sb.Append( " $('trPaipai').title='点击这里可以展开或折叠更多的拍拍网搜索结果'; " );
197 sb.Append( " $('trPaipai').onclick = function(evt){ " );
198 sb.Append( " if($('pp_child').style.display=='none'){$('pp_child').style.display='block';}else{$('pp_child').style.display='none';} " );
199 sb.Append( " }; " );
200 sb.Append( " var Paipai_child_td = Paipai_child.insertCell(Paipai_child.cells.length); " );
201 sb.Append( " Paipai_child_td.colSpan=6; " );
202 sb.Append( " Paipai_child_td_table.id = 'Paipai_child_td_table'; " );
203 sb.Append( " $app(Paipai_child_td_table,Paipai_child_td); " );
204 sb.Append( " } " );
205 sb.Append( " else{ " );
206 sb.Append( " try{ " );
207 sb.Append( " var container = Paipai_child_td_table; " );
208 sb.Append( " if(mts && container){ " );
209 sb.Append( " try{ " );
210 sb.Append( " var item = container.insertRow(container.rows.length); " );
211 sb.Append( " } " );
212 sb.Append( " catch(e){ " );
213 sb.Append( " alert('列表生成失败!'); " );
214 sb.Append( " } " );
215 sb.Append( " var item_0 = item.insertCell(item.cells.length); " );
216 sb.Append( " item_0.innerHTML = '<img src=\ "' + mts[3] + ' \ " />'; " );
217 sb.Append( " var item_1 = item.insertCell(item.cells.length); " );
218 sb.Append( " item_1.innerHTML = '<a href=\ "' + mts[1] + ' \ " target=\ " _blank\ " >' + mts[2] + '</a>'; " );
219 sb.Append( " var item_2 = item.insertCell(item.cells.length); " );
220 sb.Append( " item_2.innerHTML = mts[5]; " );
221 sb.Append( " var item_3 = item.insertCell(item.cells.length); " );
222 sb.Append( " item_3.innerHTML = mts[4]; " );
223 sb.Append( " var item_4 = item.insertCell(item.cells.length); " );
224 sb.Append( " item_4.innerHTML = '<a href=\ "' + mts[6] + ' \ " title=\ " 进入商铺\ " target=\ " _blank\ " >' + mts[7] + '</a>'; " );
225 sb.Append( " } " );
226 sb.Append( " } " );
227 sb.Append( " catch(e){ " );
228 sb.Append( " alert(e); " );
229 sb.Append( " } " );
230 sb.Append( " } " );
231 sb.Append( " x++; " );
232 sb.Append( " } " );
233 sb.Append( " } " );
234 sb.Append( " function proxyEBayItems(obj){ " );
235 sb.Append( " var mtsv; " );
236 sb.Append( " var data = obj.responseText.toString(); " );
237 sb.Append( " var index = data.indexOf('Location'); " );
238 sb.Append( " if(index > 0 && index < 500){ " );
239 sb.Append( " var ps = /location[\\s\\S]*?http:\\/\\/([^\\/]*?)\\/([\\s\\S]*?\\.html)/gi; " );
240 sb.Append( " if(mtsv = ps.exec(data)){ " ); // 如果匹配则说明还要转址
241 sb.Append( " var path = mtsv[2]; " ); // 首先需要执行字符串替换
242 sb.Append( " if(path){ " );
243 sb.Append( " path = path.replace(/==--0-2.html/gi,'==-6-0--- " + Convert.ToString( int .Parse(price_start)) + " - " + Convert.ToString( int .Parse(price_end)) + " -2.html'); " );
244 sb.Append( " path = path.replace(/\\s+?/g,''); " );
245 sb.Append( " sys.behavior.$doAX($('tid').value,mtsv[1],'8080',path,'get','','paipai_proxy','tools/sapi/tool.ashx','','utf-8','proxyEBayItems'); " );
246 sb.Append( " } " );
247 sb.Append( " } " );
248 sb.Append( " } " );
249 sb.Append( " else{ " );
250 sb.Append( " CreateEBayItems(data); " );
251 sb.Append( " } " );
252 sb.Append( " } " );
253 sb.Append( " function CreateEBayItems(data){ " );
254 sb.Append( " var res = data; " );
255 sb.Append( " var mts; " );
256 sb.Append( " var ebPatten = /<ul>[\\s\\S]*?<img[^>]*?src=\\\ " ([ ^ \\\ " ]*?)\\\ " [ ^> ] *?> [\\s\\S] *?< h2 > \\s *?< a[ ^> ] *? href = \\\ " ([^\\\ " ] *? )\\\ " [^>]*?>([\\s\\S]*?)<\\/a>\\s*?<\\/h2>[\\s\\S]*?<p>[\\s\\S]*?<a[^>]*?href=\\\ " ([ ^ \\\ " ]*?)\\\ " [ ^> ] *?> ([\\s\\S] *? ) < \\ / a > [\\s\\S] *?< \\ / p > [\\s\\S] *?< li class = \\\ " v\\\ " > [\\s\\S] *?< strong > ([ ^< ] *? ) < \\ / strong > [\\s\\S] *?< \\ / li > [\\s\\S] *?< li class = \\\ " l\\\ " > ([ ^< ] *? ) < \\ / li > [\\s\\S] *?< \\ / ul >/ gi; " );
257 sb.Append( " var x = 0; " );
258 sb.Append( " var ebay_child_td_table = $crt('table'); " );
259 sb.Append( " while(mts = ebPatten.exec(res)){ " );
260 sb.Append( " if(x == 0){ " );
261 sb.Append( " $('trEBay').cells[1].innerHTML = '<img src=\ "' + mts[1] + ' \ " />'; " );
262 sb.Append( " $('trEBay').cells[2].innerHTML = '<a href=\ "' + mts[2] + ' \ " target=\ " _blank\ " >' + mts[3] + '</a>'; " );
263 sb.Append( " $('trEBay').cells[3].innerHTML = mts[6]; " );
264 sb.Append( " $('trEBay').cells[4].innerHTML = mts[7]; " );
265 sb.Append( " $('trEBay').cells[5].innerHTML = '<a href=\ "' + mts[4] + ' \ " title=\ " 进入商铺\ " target=\ " _blank\ " >' + mts[5] + '</a>'; " );
266 sb.Append( " var ebay_child = $('ebay_child'); " );
267 sb.Append( " $('trEBay').className='t_2399997_taobao_extend'; " );
268 sb.Append( " $('trEBay').title='点击这里可以展开或折叠更多的易趣网搜索结果'; " );
269 sb.Append( " $('trEBay').onclick = function(evt){ " );
270 sb.Append( " if($('ebay_child').style.display=='none'){$('ebay_child').style.display='block';}else{$('ebay_child').style.display='none';} " );
271 sb.Append( " }; " );
272 sb.Append( " var ebay_child_td = ebay_child.insertCell(ebay_child.cells.length); " );
273 sb.Append( " ebay_child_td.colSpan=6; " );
274 sb.Append( " ebay_child_td_table.id = 'ebay_child_td_table'; " );
275 sb.Append( " $app(ebay_child_td_table,ebay_child_td); " );
276 sb.Append( " } " );
277 sb.Append( " else{ " );
278 sb.Append( " try{ " );
279 sb.Append( " var container = ebay_child_td_table; " );
280 sb.Append( " if(mts && container){ " );
281 sb.Append( " try{ " );
282 sb.Append( " var item = container.insertRow(container.rows.length); " );
283 sb.Append( " } " );
284 sb.Append( " catch(e){ " );
285 sb.Append( " alert('列表生成失败!'); " );
286 sb.Append( " } " );
287 sb.Append( " var item_0 = item.insertCell(item.cells.length); " );
288 sb.Append( " item_0.innerHTML = '<img src=\ "' + mts[1] + ' \ " />'; " );
289 sb.Append( " var item_1 = item.insertCell(item.cells.length); " );
290 sb.Append( " item_1.innerHTML = '<a href=\ "' + mts[2] + ' \ " target=\ " _blank\ " >' + mts[3] + '</a>'; " );
291 sb.Append( " var item_2 = item.insertCell(item.cells.length); " );
292 sb.Append( " item_2.innerHTML = mts[6]; " );
293 sb.Append( " var item_3 = item.insertCell(item.cells.length); " );
294 sb.Append( " item_3.innerHTML = mts[7]; " );
295 sb.Append( " var item_4 = item.insertCell(item.cells.length); " );
296 sb.Append( " item_4.innerHTML = '<a href=\ "' + mts[4] + ' \ " title=\ " 进入商铺\ " target=\ " _blank\ " >' + mts[5] + '</a>'; " );
297 sb.Append( " } " );
298 sb.Append( " } " );
299 sb.Append( " catch(e){ " );
300 sb.Append( " alert(e); " );
301 sb.Append( " } " );
302 sb.Append( " } " );
303 sb.Append( " x++; " );
304 sb.Append( " } " );
305 sb.Append( " } " );
306 sb.Append( " var key = $('tbxKey').value; " );
307 sb.Append( " var price = ($('tbxValue').value == '') ? 'none' : $('tbxValue').value; " );
308 sb.Append( " var range = ($('tbxRange').value == '') ? 'none' : $('tbxRange').value; " );
309 sb.Append( " var site = ($('cbxTaobao').checked ? '1' : '0') + '|' + ($('cbxPaipai').checked ? '1' : '0') + '|' + ($('cbxDangdang').checked ? '1' : '0') + '|' + ($('cbxJingdong').checked ? '1' : '0') + '|' + ($('cbxZhuoyue').checked ? '1' : '0'); " );
310 sb.Append( " var para = ($('cbxShowTack').checked ? '1' : '0') + '|' + ($('cbxShowImg').checked ? '1' : '0') + '|' + ($('cbxShowXin').checked ? '1' : '0'); " );
311 sb.Append( " if($('cbxTaobao').checked){ " );
312 DoResources(sb, " taobao_item " );
313 sb.Append( " } " );
314 sb.Append( " if($('cbxPaipai').checked){ " );
315 // create paipai.com proxy;
316 DoResources(sb, " paipai_item " );
317 sb.Append( " } " );
318 sb.Append( " if($('cbxJingdong').checked){ " );
319 DoResources(sb, " jd_item " );
320 sb.Append( " } " );
321 sb.Append( " if($('cbxZhuoyue').checked){ " );
322 DoResources(sb, " zy_item " );
323 sb.Append( " } " );
324 sb.Append( " if($('cbxDangdang').checked){ " );
325 DoResources(sb, " dd_item " );
326 sb.Append( " } " );
327 sb.Append( " if($('cbxEBay').checked){ " );
328 DoResources(sb, " ebay_item " );
329 sb.Append( " } " );
330 sb.Append( " </script> " );
331 break ;
332 }
333
334 return sb.ToString();
335 }
以上代码是其中一个片段。
3.创建ACTIVEX,继续一端JS脚本。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
{
_err : false ,
_tsArray : [],
_getInstance : function () {
if ( this ._tsArray.length > 0 ){
for ( var y = 0 ;y < this ._tsArray.length; y ++ ) {
if ( this ._tsArray[y] && this ._tsArray[y].State){
if ( this ._tsArray[y].State == 0 || this ._tsArray[y].State == 4 )
{
this ._tsArray[y] = null ;
this ._tsArray.splice(y, 1 );
return this ._tsArray[ this ._tsArray.length] = this ._createZHTools();
}
}
}
}
this ._tsArray[ this ._tsArray.length] = this ._createZHTools();
if ( this ._tsArray[ this ._tsArray.length - 1 ]){
return this ._tsArray[ this ._tsArray.length - 1 ];
}
else {
return null ;
}
},
_createZHTools : function () {
try {
var objTools = new ActiveXObject( " ZHDAPS.Tools.2 " );
if ( ! objTools){
ZHTools._err = true ;
ZHTools._showDownloadMsg();
}
if (objTools.State){
objTools.State = 1 ;
}
}
catch (e){
// 显示ActiveX控件下载
ZHTools._err = true ;
ZHTools._showDownloadMsg();
}
return objTools;
},
_send: function (tid,domain,port,path,method,params,flag,refUrl,referer,charset,callback){
if ( ! ZHTools._err){
var tools = this ._getInstance();
if (tools){
with (tools)
{
try
{
OnDataChanged = callback;
Send(tid,domain,port,path,method,params,flag,refUrl,referer,charset);
}
catch (e)
{
alert(e);
}
}
}
}
},
clear : function (){
for ( var y = 0 ;y < this ._tsArray.length; y ++ ) {
this ._tsArray[y].Dispose();
this ._tsArray[y] = null ;
this ._tsArray.splice(y, 1 );
}
},
_showDownloadMsg : function (){
if (confirm( ' 请安装智汇工具插件\n此工具需要ZHSoft 客户端插件支持,您确认要下载吗(此插件是安全的)? ' )){
window.location.href = " http://www.cnblogs.com/install/ClientDlls.htm " ;
}
}
}
当插件已经被安装的时候开始创建插件并调用,如果没有安装就提示安装。
在插件中需要使用多线程,然后使用IDispatch.Invoke方法调用页面中的JS回调函数。
关于更多的技术细节,将在后面的章节中加入。谢谢阅读。