javascript - Use the page title as the screenshot file name in PhantomJS -


the following phantomjs code can used obtain page title <title> of web page

var page = require('webpage').create(); page.open(url, function(status) {     var title = page.evaluate(function() {         return document.title;     });     console.log('page title ' + title);     phantom.exit(); }); 

the following phantomjs code renders multiple urls png files.

    // render multiple urls file      var renderurlstofile, arrayofurls, system;      system = require("system");      /*     render given urls     @param array of urls render     @param callbackperurl function called after finishing each url, including last url     @param callbackfinal function called after finishing     */     renderurlstofile = function(urls, callbackperurl, callbackfinal) {     var getfilename, next, page, retrieve, urlindex, webpage;     urlindex = 0;     webpage = require("webpage");     page = null;     getfilename = function() {         return "rendermulti-" + urlindex + ".png";     };     next = function(status, url, file) {         page.close();         callbackperurl(status, url, file);         return retrieve();     };     retrieve = function() {         var url;         if (urls.length > 0) {             url = urls.shift();             urlindex++;             page = webpage.create();             page.viewportsize = {                 width: 800,                 height: 600             };             page.settings.useragent = "phantom.js bot";             return page.open("http://" + url, function(status) {                 var file;                 file = getfilename();                 if (status === "success") {                     return window.settimeout((function() {                         page.render(file);                         return next(status, url, file);                     }), 200);                 } else {                     return next(status, url, file);                 }             });         } else {             return callbackfinal();         }     };     return retrieve();     };      arrayofurls = null;      if (system.args.length > 1) {         arrayofurls = array.prototype.slice.call(system.args, 1);     } else {         console.log("usage: phantomjs render_multi_url.js [domain.name1, domain.name2, ...]");         arrayofurls = ["www.google.com", "www.bbc.co.uk", "www.phantomjs.org"];     }      renderurlstofile(arrayofurls, (function(status, url, file) {         if (status !== "success") {             return console.log("unable render '" + url + "'");         } else {             return console.log("rendered '" + url + "' @ '" + file + "'");         }     }), function() {         return phantom.exit();     }); 

the names of rendered files in format of "rendermulti-" + urlindex + ".png" . want page title+".png". how can modify above code requirement.

since page global, can change getfilename() in way:

getfilename = function() {     var title = page.evaluate(function() {         return document.title;     });     return title + ".png"; }; 

you don't need access page context (inside of page.evaluate()) title. can access page.title:

getfilename = function() {     return page.title + ".png"; }; 

it may case title contains characters cannot appear in directory or file. if contains example a/b, try write file b.png directory of course doesn't exist.

simply remove such characters:

return title.replace(/[\\\/:]/g, "_") + ".png"; 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -