From 88fa80b0ab3727719413e8439320f6305896cbc5 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 28 Apr 2019 15:32:51 +1000 Subject: [PATCH 1/2] Experimental Imgur album support --- manga-loader.user.js | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/manga-loader.user.js b/manga-loader.user.js index 1b33fa2..69f37f1 100644 --- a/manga-loader.user.js +++ b/manga-loader.user.js @@ -89,6 +89,7 @@ // @match *://www.930mh.com/manhua/*/*.html* // @match *://www.mangabox.me/reader/*/episodes/*/ // @match *://twocomic.com/view/comic_*.html?ch=* +// @match *://imgur.com/a/* // -- FOOLSLIDE START // @match *://manga.redhawkscans.com/reader/read/* // @match *://reader.s2smanga.com/read/* @@ -1421,6 +1422,64 @@ var implementations = [{ return W.ch > 1 ? W.replaceurl('ch', W.pi) : false; }, wait:'#TheImg' +}, { + name: 'imgur', + match: '^https?://imgur\.com/a/[^/]+', + _images: undefined, + getImages: function() { + // Cache the images object from Imgur's album image store. + if (this._images === undefined) { + // Prevents imgur's resize / scroll handlers from firing. + if (W.$ !== undefined) { + W.$(W).unbind('resize scroll'); + } + + // Prevents Raven from sending Manga Loader errors to Imgur. + if (W.Raven !== undefined) { + W.Raven._send = function() {}; + } + + // Get the React root div. + var els = document.querySelectorAll('div.post-images > div[data-reactroot]'); + if (els.length !== 1) { + log('imgur: unexpected number of GalleryPosts', 'exit'); + } + var el = els[0]; + + // Gets the React object corresponding to . + // Some internal React object is attached as a property of the root div, + // named '__reactInternalInstance' with a hash at the end. + // We can use the value of this property to get the object. + var elValues = Object.values(el); + if (elValues.length !== 1) { + log('imgur: unexpected number of React root values:', 'exit'); + } + var reactEl = elValues[0]._nativeContainerInfo._topLevelWrapper._renderedComponent._instance; + + // Finally, get the images object from the album image store. + this._images = reactEl.props.album_image_store.getImages(reactEl.props.data.hash); + } + return this._images; + }, + next: function() { + return location.href; + }, + img: function(num) { + // num is 0-indexed, or undefined for the current (first) page. + var p = this.getImages().images[num || 0]; + var url = W.Imgur.Environment.cdnUrl + '/' + p.hash + p.ext; + return url; + }, + curpage: function() { + return 1; + }, + numpages: function() { + return this.getImages().count; + }, + pages: function(url, num, cb, ex) { + var u = this.img(num-1); + cb(u, u); + } }]; // END OF IMPL From 887572e60c62780d145ed0cc8fbd2a2b5ce7733f Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 May 2019 22:27:21 +1000 Subject: [PATCH 2/2] imgur: Explicitly find __reactInternalInstance property --- manga-loader.user.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/manga-loader.user.js b/manga-loader.user.js index 69f37f1..c38e0c1 100644 --- a/manga-loader.user.js +++ b/manga-loader.user.js @@ -1450,11 +1450,16 @@ var implementations = [{ // Some internal React object is attached as a property of the root div, // named '__reactInternalInstance' with a hash at the end. // We can use the value of this property to get the object. - var elValues = Object.values(el); - if (elValues.length !== 1) { - log('imgur: unexpected number of React root values:', 'exit'); + + // React sometimes adds a '__reactEventHandlers' property too, so we + // should explicitly find the one which starts with '__reactInternalInstance'. + var elKeys = Object.keys(el).filter(function (key) { + return key.indexOf('__reactInternalInstance') === 0; + }); + if (elKeys.length !== 1) { + log('imgur: unexpected number of __reactInternalInstance keys', 'exit'); } - var reactEl = elValues[0]._nativeContainerInfo._topLevelWrapper._renderedComponent._instance; + var reactEl = el[elKeys[0]]._nativeContainerInfo._topLevelWrapper._renderedComponent._instance; // Finally, get the images object from the album image store. this._images = reactEl.props.album_image_store.getImages(reactEl.props.data.hash);