web.js
var page = require('webpage').create(),
system = require('system'),
address,output,csvPath,nodePathFile,outOriginalimg,PCSPuserAgent;
var fs = require("fs");
if (system.args.length < 0) {
console.log('Usage: rasterize.js URL filename');
phantom.exit(1);
} else {
address = system.args[1];// ��ȁE�url
output=system.args[2];
csvPath=system.args[3];// �����csv
PCSPuserAgent=system.args[6];
console.log(PCSPuserAgent);
var BeforecsvPath=csvPath.replace('.csv','_1.csv');
var csvindex=0;
nodePathFile=system.args[4];// ��ȁE�nodepath�ļ�
outOriginalimg=system.args[5];// ��ȡԭͼ
if(PCSPuserAgent.indexOf("iPhone") > -1)
{
page.settings = {
userAgent:PCSPuserAgent,
javascriptEnabled: true,
loadImages: true
};
}
else
{
page.settings = {
javascriptEnabled: true,
loadImages: true
};
}
page.viewportSize = {width:414,height:30};
page.open(address, function (status) {
// fs.write('test.txt', "childNodes Num{_}Node Name{_}NodeIndexPath{_}Width{_}Height{_}x{_}y{_}Dispaly{_}ImgUrlOrBackImgUrl" + "\r\n", 'a');
if(fs.exists(csvPath))
{
fs.remove(csvPath);
}
window.setTimeout(function () {
VisiteHtmlDom("0",BeforecsvPath);
console.log("---------------------FS MOVE------------------------------");
fs.move(BeforecsvPath,csvPath);
}, 40000);
waitFor2(
function csvCreate()
{
if(fs.exists(csvPath)){
if(csvindex<5)
{
csvindex++;
console.log("csvindex Index:"+csvindex);
var csvPathNow=csvPath.replace('.csv','__'+csvindex.toString()+'.csv');
var BeforecsvPathNow=csvPathNow.replace('.csv','_1.csv');
if(fs.exists(csvPathNow))
{
fs.remove(csvPathNow);
}
console.log("csvindex:"+csvindex+","+"csvPathNow:"+csvPathNow+","+"BeforecsvPathNow:"+BeforecsvPathNow);
VisiteHtmlDom("0",BeforecsvPathNow);
fs.move(BeforecsvPathNow,csvPathNow);
}
else
{
return true;
}
}
},
function csvCreate2()
{
console.log("-----------------csvPath2 END---------------");
}
);
waitFor(
function nodePathFileCheck()
{
if(!fs.exists(nodePathFile))
{
console.log('nodepathFile Not Find')
}
else
{
console.log("Find nodePathFile");
return true;
}
} ,
function heheda()
{
window.setTimeout(function () {
console.log("---------------------Capture Original Begin------------------------------");
var scrollheight=page.evaluate(function () {
return document.body.scrollHeight;
});
page.viewportSize = {width:414,height:scrollheight};
page.render(outOriginalimg);
console.log("---------------------Capture Begin------------------------------");
markCapture();
console.log("scrollheight:"+scrollheight);
window.setTimeout(function () {
page.render(output);
page.close();
console.log('render ok');
phantom.exit();
}, 10000);
}, 25000);
}
);
});
}
function VisiteHtmlDom(nodePath,inputcsvPath) {
var nodeinfo = page.evaluate(function (str) {
var root = document.getElementsByTagName('html');
var htmlNode = root[0];
var xpathArr = str.split("/");
for (var i = 1; i < xpathArr.length; i++) {
var index = parseInt(xpathArr[i]);
if((htmlNode.nodeName.indexOf("SCRIPT") > -1) || (htmlNode.nodeName.indexOf("NOSCRIPT") > -1)|| (htmlNode.nodeName.indexOf("HEAD") > -1))
{
htmlNode = htmlNode;
}
else if(htmlNode.nodeName.indexOf("IFRAME") > -1&&window.getComputedStyle(htmlNode).display!="none")
{
var iframdom = htmlNode.contentWindow.document;
var roots = iframdom.getElementsByTagName('html');
htmlNode=roots[0];
}
else
{
htmlNode = htmlNode.childNodes[index];
}
}
if ((htmlNode.nodeName.indexOf("#text") > -1))
{
return htmlNode.childNodes.length + "{_}" + htmlNode.nodeName+ "{_}" + str+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+ ("{_}" + htmlNode.nodeValue||"").replace(/\r|\n/ig, "");
}
else if((htmlNode.nodeName.indexOf("SCRIPT") > -1) || (htmlNode.nodeName.indexOf("NOSCRIPT") > -1)|| (htmlNode.nodeName.indexOf("HEAD") > -1)|| (htmlNode.nodeName.indexOf("#comment") > -1))
{
return "0"+"{_}"+htmlNode.nodeName + "{_}" + str+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+"{_}";
}
else {
//
var bgImgUrl= htmlNode.style.getPropertyValue("background-image");
// var bgImgUrl=htmlNode.style.getPropertyValue("background-image").replace(/^(url)\(|\)/g, '');
if(bgImgUrl!=null)
bgImgUrl=bgImgUrl.replace(/^(url)\(|\)/g, '');
var width = window.getComputedStyle(htmlNode).width;
var height = window.getComputedStyle(htmlNode).height;
var left = window.getComputedStyle(htmlNode).left;
var top = window.getComputedStyle(htmlNode).top;
var display = window.getComputedStyle(htmlNode).display;
switch (htmlNode.nodeName) {
case "IMG":
var imgUrl = htmlNode.src; //ȡͼƬ�ĵ�ַ
return htmlNode.childNodes.length + "{_}" + htmlNode.nodeName + "{_}" + str + "{_}" + width + "{_}" + height + "{_}" + left + "{_}" + top + "{_}" + display + "{_}" + imgUrl+ "{_}"+ "{_}"; //img bgurl txt
break;
case "EMBED":
case "#COMMENT":
return htmlNode.childNodes.length + "{_}" +htmlNode.nodeName+"{_}" + str+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+"{_}"+"{_}";
break;
case "IFRAME":
var iframeUrl = htmlNode.src;
if(window.getComputedStyle(htmlNode).display!="none")
{
return "1" + "{_}" + htmlNode.nodeName + "{_}" + str + "{_}" + width + "{_}" + height + "{_}" + left + "{_}" + top + "{_}" + display + "{_}" + iframeUrl+ "{_}"+ "{_}"; //img bgurl txt
}
else
{
return "0" + "{_}" + htmlNode.nodeName + "{_}" + str + "{_}" + width + "{_}" + height + "{_}" + left + "{_}" + top + "{_}" + display + "{_}" + iframeUrl+ "{_}"+ "{_}"; //img bgurl txt
}
default:
return htmlNode.childNodes.length + "{_}" + htmlNode.nodeName + "{_}" + str + "{_}" + width + "{_}" + height + "{_}" + left + "{_}" + top + "{_}" + display + "{_}" +"{_}"+ bgImgUrl+"{_}";
break;
}
}
}, nodePath);
console.log("create CSV");
fs.write(inputcsvPath, nodeinfo + "\r\n", 'a');
// if(!(nodeinfo.indexOf("undefined")>-1))
var childNodesCount = nodeinfo.split("{_}")[0].split("/")[0];
for (var childIndex = 0; childIndex < childNodesCount;childIndex++) {
var childNodesPath = nodePath + "/" + childIndex.toString();
VisiteHtmlDom(childNodesPath,inputcsvPath);
}
}
function markCapture()
{
var stream = fs.open(nodePathFile, 'r');
while(!stream.atEnd()) {
var line = stream.readLine();
page.evaluate(function (line) {
var root = document.getElementsByTagName('html');//��ȡdom
var htmlNode = root[0];
var xpathArr=line.split("/");
for(var i=1;i<xpathArr.length;i++)
{
var index=parseInt(xpathArr[i].substring(xpathArr[i].length - 3, 3));
htmlNode= htmlNode.childNodes[index];
if(htmlNode.nodeName.indexOf("IFRAME") > -1)
{
var iframdom = htmlNode.contentWindow.document;
var roots = iframdom.getElementsByTagName('html');
htmlNode=roots[0];
i++;
}
}
if (htmlNode.nodeName.indexOf("text")>-1)
{
htmlNode.parentNode.style.boxSizing = "border-box";
htmlNode.parentNode.style.border = "5px solid #ff0000";
}
else
{
htmlNode.style.boxSizing = "border-box";
htmlNode.style.border = "5px solid #ff0000";
}
},line);
}
stream.close();
}
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 120000, //< Default Max Timout is 3m
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 5000); //< repeat 5000ms
};
function waitFor2(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 120000, //< Default Max Timout is 3m
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
if(condition)
{
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}
}, 5000); //< repeat 5000ms
};
web.js的更多相关文章
- git报错 error: cannot stat ‘'web/js': Permission denied
切换分支时报错: error: cannot stat ‘'web/js': Permission denied 解决方法:退出编辑器.浏览器.资源管理器等,然后再切换就可以了.
- ERROR in index.web.js from UglifyJs
使用weexpack构建weex应用时,npm run serve一直报这个错误 ERROR in index.web.js from UglifyJs Unexpected token: name ...
- 用weex create 创建app项目 报 ERROR in index.web.js from UglifyJs 错误!
用weex create创建一个APP项目,安装依赖后运行报 这个是package.json index.web.js 在dist目录下是build时生成的. 上面的答案没有给大家细节,不好意思致歉下 ...
- web & js & touch & gesture
web & js & touch & gesture 触摸 & 手势 https://caniuse.com/#feat=touch js https://develo ...
- TSGCTF-web Beginner's Web (js内置方法__defineSetter__)
const fastify = require('fastify'); const nunjucks = require('nunjucks'); const crypto = require('cr ...
- Web Js 按键事件……Enter提交事件 Enter Js事件
$(document).ready(function(){ document.onkeydown = function (event){ if (event.keyCode==13) //回车键的键值 ...
- Web Js推断键盘出发事件
window.document.onkeydown = disableRefresh; function disableRefresh(evt){ evt = (evt) ? evt : wind ...
- Node.js实战(三)之第一个Web服务器
这次的示例同样也可以说是HelloWorld,只不过不同的是这是web服务器示例. (1)编写web.js,内容如下: var http = require("http") fun ...
- React Native 项目运行在 Web 浏览器上面
React Native 的出现,让前端工程师拥有了使用 JavaScript 编写原生 APP 的能力.相比之前的 Web app 来说,对于性能和用户体验提升了非常多. 但是 React Nati ...
随机推荐
- 腾讯云申请的64位ubuntu服务器配置php环境
腾讯云申请的64位ubuntu服务器配置php环境 一.首先还是安装Lamp组合 Linux+Apache+Mysql+php 直接命令 sudo apt-get install apache2 su ...
- [CB] 支付宝区块链的应用- 区块链发票医保理赔.
全国第一单区块链理赔.发票开出:1分钟报销 区块链技术和概念随着比特币等虚拟电子货币的兴起而尽人皆知,但是区块链的用途可不仅仅只玩币,尤其是在“矿难”到来之后,区块链正在向更多应用领域渗透.最 ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- BZOJ4078 WF2014Metal Processing Plant(二分答案+2-SAT)
题面甚至没给范围,由数据可得n<=200.容易想到二分答案,暴力枚举某集合的价值,2-SATcheck一下即可.这样是O(n4logn)的. 2-SAT复杂度已经是下界,考虑如何优化枚举.稍微改 ...
- bzoj1040 骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...
- 洛谷4859 BZOJ3622 已经没什么好害怕的了(DP,二项式反演)
题目链接: 洛谷 BZOJ 题目大意:有两个长为 $n$ 的序列 $a,b$,问有多少种重排 $b$ 的方式,使得满足 $a_i>b_i$ 的 $i$ 的个数比满足 $a_i<b_i$ 的 ...
- raft 论文
raft 论文,摘自 http://www.infoq.com/cn/articles/raft-paper raft动画:https://raft.github.io/ raft说明动画:
- 基于Maven构建Web项目
1.下载Maven,并配置好环境变量 2.打开命令行窗口,输入以下命令构建Maven Web项目 mvn archetype:generate -DgroupId=com.hello -Dartifa ...
- JAVA实现组合、排列、重复排列(多层循环)
1.代码 package com.hdwang; import java.util.ArrayList; import java.util.Arrays; import java.util.List; ...
- 解题:CF1063F String Journey
题面 分析性质以进行DP 性质1:一定有一个最优解通过每次删除第一个或最后一个字符达到 这个脑补一下就能证明了 那么我们设$dp[i]$表示后缀$[i,n]$选出一个前缀所能达到的最大长度,从右往左D ...