mirror of https://gitee.com/zorlan/skycaiji
275 lines
10 KiB
PHP
275 lines
10 KiB
PHP
<style type="text/css">
|
||
#win_json_tree{width:100%;overflow:hidden;}
|
||
#win_json_tree ul{list-style:none;margin:0px;padding:0px;padding-left:20px;}
|
||
#win_json_tree li{padding:0;margin:0;}
|
||
#win_json_tree .tree{cursor:pointer;margin-left:-16px;margin-right:2px;}
|
||
|
||
#win_json_tree .node{color:#7F007F;}
|
||
#win_json_tree .val{}
|
||
</style>
|
||
<script src="__PUBLIC__/static/js/admin/json_tree.js?{$Think.config.html_v}"></script>
|
||
<div id="win_test_cont_url">
|
||
<div class="form-group">
|
||
<div class="input-group">
|
||
<div class="input-group-btn">
|
||
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><em class="test-tips" style="font-style:normal">抓取字段</em> <span class="caret"></span></button>
|
||
<ul class="dropdown-menu">
|
||
<li><a href="javascript:;" data-test="get_fields" data-title="抓取字段">抓取字段</a></li>
|
||
<li><a href="javascript:;" data-test="get_html" data-title="抓取源码">抓取源码</a></li>
|
||
<li><a href="javascript:;" data-test="elements" data-title="分析网页">分析网页</a></li>
|
||
<li><a href="javascript:;" data-test="get_paging_urls" data-title="抓取分页">抓取分页</a></li>
|
||
<li><a href="javascript:;" data-test="get_relation_urls" data-title="抓取关联页">抓取关联页</a></li>
|
||
</ul>
|
||
</div>
|
||
{if condition="!empty($url_post)"}
|
||
<div class="input-group-addon" style="color:green;border-right:0;">POST模式</div>
|
||
{/if}
|
||
<input type="text" class="form-control" name="url" value="{$cont_url}" placeholder="输入内容页网址" />
|
||
<div class="input-group-btn"><button type="button" class="btn btn-default test-sub">确定</button></div>
|
||
</div>
|
||
</div>
|
||
|
||
{if condition="!empty($input_urls)"}
|
||
<div id="win_get_fields_input">
|
||
<div class="panel panel-default">
|
||
<div class="panel-heading">有字段需要从以下数据源抓取页面,请输入网址</div>
|
||
<div class="panel-body">
|
||
{if condition="isset($input_urls['source_url'])"}
|
||
<div class="form-group">
|
||
<div class="input-group">
|
||
<div class="input-group-addon">起始页</div>
|
||
<input type="text" name="source_url" class="form-control" value="{$input_urls['source_url']}" />
|
||
</div>
|
||
</div>
|
||
{/if}
|
||
{if condition="is_array($input_urls['level_url'])"}
|
||
{foreach $input_urls['level_url'] as $level_url}
|
||
<div class="form-group">
|
||
<div class="input-group">
|
||
<div class="input-group-addon">多级页:{$level_url['name']}</div>
|
||
<input type="text" name="level_{$level_url['level']}" class="form-control" value="{$level_url['url']}" />
|
||
</div>
|
||
</div>
|
||
{/foreach}
|
||
{/if}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
{/if}
|
||
|
||
<div class="vals">
|
||
</div>
|
||
|
||
</div>
|
||
<script type="text/javascript">
|
||
'use strict';//严格模式
|
||
//预览
|
||
function win_test_view(id){
|
||
if($('#'+id).next().attr('id')=='ifm_test_view'){
|
||
//折叠
|
||
$('#ifm_test_view').remove();
|
||
}else{
|
||
$('#ifm_test_view').remove();//删除旧的
|
||
var html=$('#'+id).val();
|
||
html=html.replace(/<script[^<>]*>[\s\S]*?<\/script>/ig,'');//去掉脚本代码
|
||
html=html.replace(/<meta[^<>]*charset[^<>]*>/i,'');//去掉编码
|
||
var ifm='<iframe id="ifm_test_view" style="width:100%;border:1px solid #ccc;"></iframe>';
|
||
$('#'+id).after(ifm);
|
||
$('#ifm_test_view').bind('load',function(){
|
||
if($("#ifm_test_view").contents().find('body').html().length<=0){
|
||
//火狐浏览器需要重新赋值
|
||
$("#ifm_test_view").contents().find('body').html(html);
|
||
}
|
||
var iframeObj = document.getElementById('ifm_test_view');
|
||
var height=(iframeObj.Document?iframeObj.Document.body.scrollHeight:iframeObj.contentDocument.body.offsetHeight);
|
||
$('#ifm_test_view').attr('height',height+'px');
|
||
});
|
||
$("#ifm_test_view").contents().find('body').html(html);
|
||
}
|
||
}
|
||
|
||
//分析网页
|
||
function test_elements(id){
|
||
var cont_url=$('#win_test_cont_url input[name="url"]').val();
|
||
var url="{:url('Cpattern/browser?coll_id='.$collData['id'].'&url=_url_')}";
|
||
url=url.replace('_url_',encodeURIComponent(cont_url));
|
||
window.open(url,'_blank');
|
||
}
|
||
//下拉选择
|
||
$('#win_test_cont_url .dropdown-menu a[data-test]').bind('click',function(){
|
||
var btnObj=$(this).parents('.dropdown-menu').eq(0).siblings('button.dropdown-toggle').eq(0);
|
||
btnObj.attr('data-test',$(this).attr('data-test'));
|
||
btnObj.find('.test-tips').text($(this).attr('data-title'));
|
||
if($(this).attr('data-test')=='get_fields'){
|
||
//显示输入数据源网址
|
||
$('#win_get_fields_input').show();
|
||
}else{
|
||
$('#win_get_fields_input').hide();
|
||
}
|
||
});
|
||
|
||
{if condition="!empty($test)"}
|
||
$('#win_test_cont_url .dropdown-menu a[data-test="{$test}"]').trigger('click');
|
||
{/if}
|
||
|
||
//测试按钮
|
||
$('#win_test_cont_url button.test-sub').bind('click',function(){
|
||
var cont_url=$('#win_test_cont_url input[name="url"]').val();
|
||
if(!cont_url){
|
||
toastr.error('请输入网址');
|
||
}else{
|
||
$('#win_test_cont_url .vals').html('<img src="'+window.site_config.pub+'/static/images/loading.gif">');
|
||
var testOp=$('#win_test_cont_url button.dropdown-toggle').attr('data-test');
|
||
testOp=testOp?testOp:'get_fields';//默认获取字段
|
||
var url="{:url('Cpattern/test?op=_op_&coll_id='.$collData['id'].'&cont_url=_url_')}";
|
||
url=url.replace('_op_',testOp).replace('_url_',encodeURIComponent(cont_url));
|
||
if(testOp=='elements'){
|
||
test_elements();
|
||
$('#win_test_cont_url .vals').html('已过滤所有脚本代码,页面所见即所得!');
|
||
}else if(testOp=='get_html'){
|
||
//获取源码,防止源代码输出错误
|
||
$.ajax({
|
||
type:'get',
|
||
url:url,
|
||
dataType:'html',
|
||
success:function(data){
|
||
if(htmlIsJson(data)){
|
||
//AJAX错误提示
|
||
var json=eval('(' + data + ')');
|
||
if(json.msg){
|
||
//json提示
|
||
$('#win_test_cont_url .vals').html(json.msg);
|
||
}else{
|
||
//json源码
|
||
var html='<label>源码:</label><textarea class="form-control" rows="5">'+data
|
||
+'</textarea><label style="margin-top:15px;">JSON解析:</label><div id="win_json_tree"></div>';
|
||
$('#win_test_cont_url .vals').html(html);
|
||
//加载json树形
|
||
jsonTree.treeId='#win_json_tree';
|
||
jsonTree.treeClass='.tree';
|
||
jsonTree.load(data);
|
||
}
|
||
}else{
|
||
var eleId=generateUUID();
|
||
data=data.replace(/\</g,'<').replace(/\>/g,'>');//编码
|
||
var html='<label>源码:</label><a href="javascript:;" onclick="test_elements()">分析网页</a><textarea id="'
|
||
+eleId+'" class="form-control" rows="20">'+data+'</textarea>';
|
||
$('#win_test_cont_url .vals').html(html);
|
||
}
|
||
}
|
||
});
|
||
}else{
|
||
if(testOp=='get_fields'){
|
||
//附加数据源网址
|
||
$('#win_get_fields_input').find('input').each(function(){
|
||
url+='&'+$(this).attr('name')+'='+encodeURIComponent($(this).val());
|
||
});
|
||
//表格标题左右拉伸
|
||
window.win_test_table_thead_mousedown=null;//点击初始
|
||
var thEle='.table-test-loop thead th';
|
||
$('#win_test_cont_url').off('mousedown',thEle).on('mousedown',thEle,function(e){
|
||
$(this).css('background','#f1f1f1');
|
||
window.win_test_table_thead_mousedown={
|
||
table_width:$('#win_test_cont_url .table-test-loop').width(),
|
||
th_width:$(this).width(),
|
||
page_x:e.pageX
|
||
};
|
||
$(this).off('mousemove').bind('mousemove',function(e){
|
||
var def=window.win_test_table_thead_mousedown;
|
||
if(def){
|
||
var moveWidth=e.pageX-def.page_x;
|
||
$("#win_test_cont_url .table-test-loop").width(def.table_width+moveWidth);
|
||
$(this).width(def.th_width+moveWidth);
|
||
}
|
||
});
|
||
$(this).off('mouseout mouseup').bind('mouseout mouseup',function(e){
|
||
$(this).css('background','');
|
||
window.win_test_table_thead_mousedown=null;//结束
|
||
});
|
||
});
|
||
}
|
||
$.ajax({
|
||
type:'get',
|
||
url:url,
|
||
dataType:'json',
|
||
success:function(data){
|
||
if(data.code==1){
|
||
var html='';
|
||
if(testOp=='get_paging_urls'){
|
||
html='<div class="page-header"><b>成功抓取到分页链接</b></div><ul>';
|
||
for(var i in data.data){
|
||
html+='<li>'+data.data[i]+'</li>';
|
||
}
|
||
html+='</ul>';
|
||
}else if(testOp=='get_fields'){
|
||
if(data.msg){
|
||
toastr.success(data.msg);
|
||
}
|
||
if(data.data.length>1){
|
||
//循环数据
|
||
html+='<div class="table-responsive"><table class="table table-bordered table-hover table-test-loop"><thead><tr><td></td>';
|
||
var fields=data.data[0];//第一条数据
|
||
for(var f in fields){
|
||
html+='<th>'+f+'</th>';
|
||
}
|
||
html+='</tr></thead><tbody>';
|
||
|
||
var curNum=0;
|
||
for(var i in data.data){
|
||
curNum++;
|
||
var vals=data.data[i];//每条数据
|
||
html+='<tr><td>'+curNum+'</td>';
|
||
for(var f in vals){
|
||
if(vals[f]){
|
||
html+='<td><input type="text" value="'+(vals[f].replace(/"/g, '"'))+'" /></td>';
|
||
}else{
|
||
html+='<td></td>';
|
||
}
|
||
}
|
||
html+='</tr>';
|
||
}
|
||
html+='</tbody>';
|
||
}else{
|
||
//一条数据
|
||
var vals=data.data[0];
|
||
for(var f in vals){
|
||
var browse='';
|
||
var eleId=generateUUID();
|
||
if((/<[^<>]*>/).test(vals[f])){
|
||
//检测到html代码
|
||
browse='<a href="javascript:;" onclick="win_test_view(\''+eleId+'\')">预览</a>';
|
||
}
|
||
vals[f]=vals[f].replace(/\</g,'<').replace(/\>/g,'>');//编码
|
||
html+='<label>'+f+':</label>'+browse+'<textarea id="'+eleId+'" class="form-control">'+vals[f]+'</textarea><br>';
|
||
}
|
||
}
|
||
}else if(testOp=='get_relation_urls'){
|
||
html='<div class="page-header"><b>关联页网址</b></div><ul>';
|
||
for(var i in data.data){
|
||
html+='<li>'+i+':'+data.data[i]+'</li>';
|
||
}
|
||
html+='</ul>';
|
||
}
|
||
$('#win_test_cont_url .vals').html(html);
|
||
}else{
|
||
$('#win_test_cont_url .vals').html(data.msg);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
}
|
||
});
|
||
|
||
//回车测试
|
||
$('#win_test_cont_url input[name="url"]').bind('keyup',function(event){
|
||
if(event.keyCode=="13"){
|
||
$('#win_test_cont_url button').trigger("click");
|
||
}
|
||
});
|
||
|
||
|
||
if($('#win_test_cont_url input[name="url"]').val().length>0){
|
||
//有网址自动触发
|
||
$('#win_test_cont_url button').trigger("click");
|
||
}
|
||
</script> |