自动滚动窗口抓取网页全图
新建fap模拟程序,在源码编辑器中输入下面的源代码,点击“编译生成模拟程序”按钮。
<?xml version="1.0" encoding="gb2312" ?>
<ApeML LID="CHS" MD="0" 抓屏模式="0" 名称="网页抓图演示(自动滚屏)" 作者="精灵加加 yhhe.net" 信息窗口="true" 速度="30" 循环次数="1" 自定义属性="用户选项=0">
<!--抓网页全图示例
抓图时自动滚屏合成图片-->
<脚本区块 语言="LAScript" 名称="init" 延时="0" 启用="true" 编辑密钥="" 运行公钥="">
<![CDATA[
if(_VERSION<2005700)then
win.messageBox("本程序必须模拟精灵v7.0以上版本支持");
end;
import("std");--导入std库,不导入也没关系,主要是导入_MIX_SRCCOPY,不过_MIX_COPY为0,不导入为nil值作为参数还是转换为0的
_url = win.inputBox("请指定要抓屏生成图片的网址","自动滚屏网页抓图小工具");
wb = web.new(); --创建一个web窗体
wb:setTitle(false); --不要边框标题栏这些
wb:setScroll(false); --不要滚动条
wb:go(_url);--打开网址
wb:wait("");--等待网页打开
wb:show(true); --显示web窗体
local hwnd = wb:getWindow(); --得到窗口句柄
local x,y,x2,y2 = win.getRect(hwnd); --得到窗口大小
local w = x2 - x; --算出宽
local h = y2 -y; --算出高
win.setForeground(hwnd);delay(1000); --前置窗体
img = image.new( w,h ); --创建图片
img:capture(hwnd,0,0,w,h); --抓取窗口图片,坐标当澹是0 x 0
function capweb()
img2 = image(img);--复制原来的img
img = image.new(img:width(),img:height()+h); --增加图片一页的高度
img:mix(img2,_MIX_COPY,0,0); --把原来的图片贴到新图片的上面
local imgweb = image.new( w,h );
win.setForeground(hwnd);delay(100);
imgweb:capture(hwnd,0,0,w,h);
img:mix(imgweb,_MIX_COPY,0,img:height()-h); --把新图片补到下面
end;
function capweb2(t)
local th=t;
img2 = image(img);--复制原来的img
img = image.new(img:width(),img:height()+th);--增加图片不足一页的高度
img:mix(img2,_MIX_COPY,0,0);--把原来的图片贴到新图片的上面
local imgweb = image.new( w,th );
win.setForeground(hwnd);delay(100);
imgweb:capture(hwnd,0,h-th,w,th);
img:mix(imgweb,_MIX_COPY,0,img:height()-th);--把新图片补到下面
end;
local js = [[
var LAScript = external;
var currentpos = -1;
var n = 0;
function xbody()
{
if( document.documentElement && document.documentElement.scrollTop )
return document.documentElement;
return document.body;
}
while(currentpos!=xbody().scrollTop)
{
currentpos = xbody().scrollTop;
window.scroll(0,currentpos + ]]..h..[[);
n = xbody().scrollTop-currentpos;
if
(n<]]..h..[[)
{
LAScript.capweb2(n); //直接调用LAScript里的抓屏函数,这里是不足一屏的高度
break;
}
else
{
LAScript.capweb();//直接调用LAScript里的抓屏函数
}
}
]]
wb:doScript(js,"javascript");
local url = urlparse.new(_url);--创建一个网址分析器
local deffile = url:path(); --取到网址中的文件路径
if(deffile )then
deffile = string.gsub(deffile,"/","\\");
deffile = win.pathName( deffile ); --取到文件名
else
deffile = "web"
end;
deffile = deffile..".jpg"; --生成默认的文件名
--打开保存文件对话框
local file = win.saveDlg("保存图片","*.jpg|*.jpg|*.bmp|*.bmp|*.png|*.png||",deffile)
img:save( file );
web.del(wb);--删除web窗体
]]>
</脚本区块>
<数据区块 />
</ApeML>