var locationName = null;
var nextButton = null;
var pageNumber = 0;
function scanButtons()
{
// Scan the page for anchor elements that act as page buttons.
nextButton = null;
var filename = null;
var previousButton = null;
var anchors = document.getElementsByTagName("a");
for (let i = 0; i < anchors.length; i++) {
var attributes = anchors[i].attributes;
for (let j = 0; j < attributes.length; j++) {
if (attributes[j].name == "data-dt-idx") {
var className = anchors[i].className;
if (className.indexOf("disabled") == -1) {
if (className.indexOf("current") != -1) {
// Set the page number to the text of the "current" button.
pageNumber = parseInt(anchors[i].innerText);
}
else if (className.indexOf("previous") != -1) {
// Set the previous button.
previousButton = anchors[i];
}
else if (className.indexOf("next") != -1) {
// Set the next button.
nextButton = anchors[i];
}
else {
// Add the href attribute to the page button.
filename = locationName + "-" + anchors[i].innerText + ".html";
anchors[i].setAttribute("href", filename);
}
}
}
}
}
if (previousButton != null) {
// Add the href attribute to the previous button.
filename = locationName + "-" + (pageNumber - 1) + ".html";
previousButton.setAttribute("href", filename);
}
if (nextButton != null) {
// Add the href attribute to the next button.
filename = locationName + "-" + (pageNumber + 1) + ".html";
nextButton.setAttribute("href", filename);
}
}
function savePage()
{
// Scan the buttons and build the file name.
scanButtons();
var filename = locationName + "-" + pageNumber + ".html";
if (nextButton == null) {
// If the page does not have a next button, tell SiteSucker to save the last page.
window.webkit.messageHandlers.saveLast.postMessage(filename);
}
else {
// If the page has a next button, tell SiteSucker to save the page.
window.webkit.messageHandlers.save.postMessage(filename);
// Wait 3 seconds for the page to be saved and then click the next button.
setTimeout(clickButton, 3000);
}
}
function clickButton()
{
// Click the next button, wait 3 seconds for the page to change, then save the page.
nextButton.click();
setTimeout(savePage, 3000);
}
window.addEventListener('load', (event) => {
// After the page has loaded, get the location name and scan the buttons.
locationName = window.location.href.slice(window.location.href.lastIndexOf("/") + 1);
scanButtons();
if (nextButton != null) {
// If the page has a next button, build the file name and tell SiteSucker to save the page.
var filename = locationName + "-" + pageNumber + ".html";
window.webkit.messageHandlers.save.postMessage(filename);
// Wait 3 seconds for the page to be saved and then click the next button.
setTimeout(clickButton, 3000);
}
});