Skip to content

Commit

Permalink
Merge remote-tracking branch 'belldandu/master' into sonako
Browse files Browse the repository at this point in the history
Conflicts:
	plugin/js/parsers/BakaTsukiImageCollector.js
  • Loading branch information
dteviot committed Jun 21, 2016
2 parents 1e245fa + de40496 commit 09dbe2c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
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
29 changes: 21 additions & 8 deletions plugin/js/parsers/BakaTsukiImageCollector.js
Expand Up @@ -109,7 +109,7 @@ ImageElementConverter.prototype.replaceWithImagePageUrl = function (images) {
let that = this;
// replace tag with nested <img> tag, with new <img> tag
let imageInfo = images.get(that.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 @@ -241,7 +241,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 @@ -253,11 +253,24 @@ 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(div, "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;
});
}

0 comments on commit 09dbe2c

Please sign in to comment.