Skip to content

Instantly share code, notes, and snippets.

@StefRe
Created June 2, 2017 10:17
Show Gist options
  • Save StefRe/5585bef9452a90058d3c0e17923e5a39 to your computer and use it in GitHub Desktop.
Save StefRe/5585bef9452a90058d3c0e17923e5a39 to your computer and use it in GitHub Desktop.
GM script: Insert button to auto updating page with links to read pages for download
// ==UserScript==
// @name Google books images
// @namespace gist.github.com/StefRe
// @version 1.2
// @description Insert button to auto updating page with links to read pages for download
// @include https://books.google.*/books*
// @grant none
// ==/UserScript==
var imgsrcL, imgWindow, viewport, link;
function updateImgList() {
var imgL = viewport.getElementsByTagName('img'), imgsrc;
for (var i = 0; i < imgL.length; i++) {
imgsrc = imgL[i].src;
if (imgsrc == undefined || imgsrc == '') {
continue;
}
if (imgsrcL.indexOf(imgsrc) == - 1) {
imgsrcL.push(imgsrc);
//PA = regular page, PT = part, PP = preface page, PR = page without pagination, RA = volume
var p = /.*&pg=([^&]+).*/.exec(imgsrc);
if (p && p.length > 1) {
// only download page images, not restricted_logo, background...
var a = imgWindow.document.createElement('a');
a.href = imgsrc;
a.download = p[1] + '.png'; // TODO: title pages can be jpg
a.innerHTML = p[1];
var ni = imgWindow.document.createNodeIterator(imgWindow.document.body, NodeFilter.SHOW_ELEMENT,
function(node) {
return node.nodeName.toLowerCase() === 'a' && node.innerHTML > p[1] ?
NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
});
var nextNode = ni.nextNode();
imgWindow.document.body.insertBefore(a, nextNode);
imgWindow.document.body.insertBefore(imgWindow.document.createElement('br'), nextNode);
}
}
}
}
function Refresh() {
viewport = document.getElementById('viewport')
viewport.addEventListener('DOMSubtreeModified', updateImgList);
imgWindow = window.open();
imgsrcL = [];
updateImgList();
imgWindow.document.title = document.title;
}
link = document.createElement('a');
link.innerHTML = ' ⇩';
link.style.cursor = 'pointer';
link.onclick = Refresh;
document.getElementsByClassName('kd-appname')[0].appendChild(link);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment