Reputation: 769
How can I get all the URL redirects in slimerJS and save them into an array then render a screenshot of the final URL.
For example if I run red.com through slimer but it redirects through php to blue.com which then redirects again to yellow.com through javascript, how can I save these redirects into an array?
Once the redirecting has finished I would then take a screenshot of the final URL, so yellow.com in the example above.
I'm using slimer 1.0 because I want to use slimer in headless mode.
Here's some simplified code I wrote over the weekend.
call to slimer (windows)
./slimerjs/src/slimerjs.bat --headless slimer-redirects.js http://google.com
The call to google.com using the script below doesn't pick up the redirect to google.co.uk (I'm in UK). Strangely though the code below does detect javascript redirects. Another problem with the code below is it doesn't seem to pick up multiple i.e > 2 redirects when it does work.
var system = require("system");
var page;
var myurl = system.args[1];
var renderPage = function(url) {
page = require('webpage').create();
page.onNavigationRequested = function(url, type, willNavigate, main) {
if (url != myurl) {
myurl = url;
// page.close(); // makes some redirects hang. google.com for example
setTimeout('renderPage(myurl)', 1);
}
};
page.open(myurl)
.then(function (status) {
if (status === "success") {
// I would then render screenshot here and exit
console.log("Final URL is: " + myurl);
slimer.exit();
}
});
};
renderPage(myurl);
It seems quite a simple task to just get the redirect URL's but I can't figure it out. If it's not possible with slimer I would also like to know of other options. Chrome headless with or without puppeteer maybe?
Upvotes: 1
Views: 509
Reputation: 906
You can use callback onUrlChanged() for saving redirect URL
var urls = [];
page.onUrlChanged = function(url) {
urls.push(url);
};
This snippet should help you
Upvotes: 1