Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add web.archive.org support #3

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions plugin/js/ParserFactory.js
Expand Up @@ -8,6 +8,15 @@ var parserFactory = (function () {

let parsers = new Map();

var isWebArchive = function(hostName) {
return extractHostName(hostName).startsWith("web.archive.org");
}

var stripWebArchive = function(url) {
var hostName = url.split('://');
return hostName[2] ? "https://" + hostName[2] : url;
}

var stripLeadingWww = function(hostName) {
return hostName.startsWith("www.") ? hostName.substring(4) : hostName;
}
Expand All @@ -21,6 +30,9 @@ var parserFactory = (function () {
};

var fetch = function(url) {
if (isWebArchive(url)) {
url = stripWebArchive(url);
}
let hostName = stripLeadingWww(extractHostName(url));
let constructor = parsers.get(hostName);
return (constructor === undefined) ? undefined : constructor();
Expand Down
32 changes: 22 additions & 10 deletions plugin/js/parsers/BakaTsukiImageCollector.js
Expand Up @@ -109,7 +109,7 @@ ImageElementConverter.prototype.replaceWithImagePageUrl = function (images) {
// replace tag with nested <img> tag, with new <img> tag
let imagePageUrl = BakaTsukiImageCollector.extractImagePageUrl(that.element);
let imageInfo = images.get(imagePageUrl);
if (imageInfo != null) {
if (imageInfo != null && that.element.parentElement != null) {
let newImage = imageInfo.createImageElement();
that.element.parentElement.replaceChild(newImage, that.element);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ BakaTsukiImageCollector.prototype.fetchImages = function (imageList, progressInd
sequence = sequence.then(function () {
return client.fetchHtml(imageInfo.imagePageUrl);
}).then(function (rawDom) {
that.updateImageInfoFromImagePage(rawDom, imageInfo);
return that.updateImageInfoFromImagePage(rawDom, imageInfo)
}).then(function () {
return client.fetchBinary(imageInfo.imagefileUrl);
}).then(function (arraybuffer) {
Expand All @@ -246,11 +246,23 @@ BakaTsukiImageCollector.prototype.fetchImages = function (imageList, progressInd
}

BakaTsukiImageCollector.prototype.updateImageInfoFromImagePage = function(dom, imageInfo) {
let div = util.getElement(dom, "div", e => (e.className === "fullImageLink"));
let img = util.getElement(dom, "img");
imageInfo.imagefileUrl = img.src;
imageInfo.height = img.height;
imageInfo.width = img.width;
return imageInfo;
}

let div = util.getElement(dom, "div", e => (e.className === "fullMedia"));
let a = util.getElement(div, "a");
imageInfo.imagefileUrl = a.href;
return new Promise(function(resolve, reject){
let img = new Image();
img.onload = function() {
imageInfo.height = img.height;
imageInfo.width = img.width;
resolve();
}
img.onerror = function(){
// If the image gives an error then set a general height and width
imageInfo.height = 1200;
imageInfo.width = 1600;
resolve();
}
// start downloading image after event handlers are set
img.src = a.href;
});
}