基于phantomjs的截图优化JS信息
星期三, 2023-04-05 | Author: Lee | computer, JAVA-and-J2EE, linux | 460 views
phantomjs是比较老的一种模拟抓取及截图,这个是以前处理截图的一种优化js信息做个留档
以后应该是不用了
img.js和rasterize.js两个文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | var page = require('webpage').create(), system = require('system'), address, output, size; if (system.args.length < 3 || system.args.length > 5) { phantom.exit(1); } else { address = system.args[1]; output = system.args[2]; //定义宽高 /* page.viewportSize = { width : 1024, height : 768 };*/ page.open(address, function(status) { var bb = page.evaluate(function() { return document.getElementsByTagName('html')[0].getBoundingClientRect(); }); page.clipRect = { top : bb.top, left : bb.left, width : bb.width, height : bb.height }; window.setTimeout(function() { page.render(output); page.close(); console.log('渲染成功...'); console.log(address); phantom.exit(); }, 1000); }); } |
rasterize.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | var page = require('webpage').create(); var system = require('system'); var address, output, size; if (system.args.length < 3 || system.args.length > 7) { console.log('Usage: rasterize.js URL filename paperwidth*paperheight|paperformat portrait|landscape margin zoomfactor'); console.log(' paperwidth*paperheight|paperformat examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"'); console.log(' margin examples: "1cm", "2in"'); phantom.exit(1); } else { address = system.args[1]; output = system.args[2]; page.viewportSize = { width: 800, height: 800 }; //size size = system.args[3].split('*'); page.paperSize = size.length === 2 ? { width: size[0], height: size[1], orientation: system.args[4], margin: system.args[5] } : { format: system.args[3], orientation: system.args[4], margin: system.args[5] }; page.zoomFactor = Number(system.args[6]); var statusCode; page.onResourceReceived = function (resource) { if (resource.url === address) { statusCode = resource.status; } }; page.open(address, function (status) { if (status !== 'success') { console.error('Unable to load the address (' + statusCode + '): ' + address, status); return phantom.exit(100); } else { if (page.evaluate(function () { return typeof html2pdf === 'object'; })) { var paperSize = page.paperSize; setUpHeaderOrFooter('header'); setUpHeaderOrFooter('footer'); page.paperSize = paperSize; } return window.setTimeout(function () { page.render(output); phantom.exit(); }, 1000); } function setUpHeaderOrFooter(headerOrFooter) { var hasHeaderOrFooter = page.evaluate(function (headerOrFooter) { return typeof html2pdf[headerOrFooter] === 'object'; }, headerOrFooter); if (hasHeaderOrFooter) { var height, contents; var typeOfHeight = page.evaluate(function (headerOrFooter) { return html2pdf[headerOrFooter].height && typeof html2pdf[headerOrFooter].height; }, headerOrFooter); if (typeOfHeight === 'string') { height = page.evaluate(function (headerOrFooter) { return html2pdf[headerOrFooter].height; }, headerOrFooter); } else { console.error('html2pdf.' + headerOrFooter + '.height has wrong type: ' + typeOfHeight); return phantom.exit(100); } var typeOfContent = page.evaluate(function (headerOrFooter) { return html2pdf[headerOrFooter].contents && typeof html2pdf[headerOrFooter].contents; }, headerOrFooter); if (typeOfContent === 'string' || typeOfContent === 'function') { contents = phantom.callback(function (pageNum, numPages) { return getHtmlWithStyles(headerOrFooter, pageNum, numPages); }); } else { console.error('html2pdf.' + headerOrFooter + '.contents has wrong type: ' + typeOfContent); return phantom.exit(100); } paperSize[headerOrFooter] = { height: height, contents: contents }; return null; } } function getHtmlWithStyles(headerOrFooter, pageNumber, totalPages) { return page.evaluate(function (headerOrFooter, pageNumber, totalPages) { var contents = html2pdf[headerOrFooter].contents; var html = typeof contents === 'string' ? contents : html2pdf[headerOrFooter].contents(pageNumber, totalPages); html = html .replace(/\{\{pagenumber\}\}/gi, pageNumber) .replace(/\{\{totalpages\}\}/gi, totalPages); //Style/footer/Header super-container var superHost = document.createElement('div'); superHost.innerHTML = html; // Styles will be placed before this element. First styles, then foorter/header elements var stylesGoesBefore = superHost.firstChild; var addStyle = function (styleStr) { var newStyle = document.createElement('style'); newStyle.setAttribute('type', 'text/css'); newStyle.innerHTML = styleStr; stylesGoesBefore.insertBefore( newStyle ); }; // https://developer.mozilla.org/en-US/docs/Web/API/document.styleSheets // https://developer.mozilla.org/en-US/docs/Web/API/StyleSheet.ownerNode for (var i = 0; i < document.styleSheets.length; i++) { var styleSheet = document.styleSheets[i]; // CSS from style html element if (styleSheet.ownerNode.nodeName.toLowerCase() === 'style') { var cssStr = styleSheet.ownerNode.innerHTML; addStyle( cssStr ); // CSS from link html element } else if (styleSheet.ownerNode.nodeName.toLowerCase() === 'link') { var xhReq = new XMLHttpRequest(); xhReq.open('GET', styleSheet.href, false); xhReq.send(null); var serverResponse = xhReq.responseText; addStyle( serverResponse ); } } return superHost.outerHTML; }, headerOrFooter, pageNumber, totalPages); } }); } |
文章作者: Lee
本文地址: https://www.pomelolee.com/2349.html
除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址
No comments yet.
Leave a comment
Search
相关文章
热门文章
最新文章
文章分类
- ajax (10)
- algorithm-learn (3)
- Android (6)
- as (3)
- computer (85)
- Database (30)
- disucz (4)
- enterprise (1)
- erlang (2)
- flash (5)
- golang (3)
- html5 (18)
- ios (4)
- JAVA-and-J2EE (186)
- linux (143)
- mac (10)
- movie-music (11)
- pagemaker (36)
- php (50)
- spring-boot (2)
- Synology群晖 (2)
- Uncategorized (6)
- unity (1)
- webgame (15)
- wordpress (33)
- work-other (2)
- 低代码 (1)
- 体味生活 (40)
- 前端 (21)
- 大数据 (8)
- 游戏开发 (9)
- 爱上海 (19)
- 读书 (4)
- 软件 (3)