2014-02-04 04:37:22 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-02-04 04:37:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads an image.
|
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
class ImageProvider {
|
|
|
|
/**
|
|
|
|
* @param {string} imageQueryParameter When defined, is a query parameter to add to every image request
|
|
|
|
*/
|
|
|
|
constructor( imageQueryParameter ) {
|
|
|
|
this.imageQueryParameter = imageQueryParameter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AJAX call cache.
|
|
|
|
*
|
|
|
|
* @property {Object.<string, jQuery.Promise>} cache
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
this.cache = {};
|
|
|
|
}
|
2014-11-16 11:31:47 +00:00
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Loads an image and returns it. When the browser supports it, the image is loaded as an AJAX
|
|
|
|
* request.
|
2020-06-26 10:21:04 +00:00
|
|
|
*
|
2023-05-20 12:38:59 +00:00
|
|
|
* @param {string} url
|
|
|
|
* @return {jQuery.Promise.<HTMLImageElement>} A promise which resolves to the image object.
|
|
|
|
* When loaded via AJAX, it has progress events, which return an array with the content loaded
|
|
|
|
* so far and with the progress as a floating-point number between 0 and 100.
|
2014-02-13 09:52:40 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
get( url ) {
|
|
|
|
const cacheKey = url;
|
|
|
|
const extraParam = {};
|
2014-02-04 04:37:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( this.imageQueryParameter ) {
|
|
|
|
try {
|
|
|
|
const uri = new mw.Uri( url );
|
|
|
|
extraParam[ this.imageQueryParameter ] = null;
|
|
|
|
url = uri.extend( extraParam ).toString();
|
|
|
|
} catch ( error ) {
|
|
|
|
return $.Deferred().reject( error.message );
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 11:31:47 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( !this.cache[ cacheKey ] ) {
|
|
|
|
this.cache[ cacheKey ] = this.rawGet( url, this.imagePreloadingSupported() );
|
|
|
|
this.cache[ cacheKey ].fail( ( error ) => {
|
|
|
|
mw.log( `${this.constructor.name} provider failed to load: `, error );
|
|
|
|
} );
|
2020-10-13 19:26:23 +00:00
|
|
|
}
|
2014-02-04 04:37:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return this.cache[ cacheKey ];
|
2014-02-13 09:52:40 +00:00
|
|
|
}
|
2014-02-04 04:37:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Internal version of get(): no caching, no performance metrics.
|
|
|
|
*
|
|
|
|
* @param {string} url
|
|
|
|
* @param {boolean} [cors] if true, use CORS for preloading
|
|
|
|
* @return {jQuery.Promise.<HTMLImageElement>} a promise which resolves to the image object
|
|
|
|
*/
|
|
|
|
rawGet( url, cors ) {
|
|
|
|
const img = new window.Image();
|
|
|
|
const deferred = $.Deferred();
|
2014-02-13 15:48:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// This attribute is necessary in Firefox, which needs it for the image request after
|
|
|
|
// the XHR to hit the cache by being a proper CORS request.
|
|
|
|
if ( cors ) {
|
|
|
|
img.crossOrigin = 'anonymous';
|
|
|
|
}
|
2014-02-13 15:48:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
img.onload = () => deferred.resolve( img );
|
|
|
|
img.onerror = () => deferred.reject( `could not load image from ${url}` );
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
img.src = url;
|
Use cross-origin img attribute instead of data URI
After lots of experimenting with Wireshark and
current Chrome + Firefox on Ubuntu 13.10, this is my
current understanding of the caching when preloading images
with AJAX requests:
* on Chrome, the image request always comes from browser cache
* Firefox makes two separate requests by default
* Firefox with img.crossOrigin = 'anonymous' makes two separate
requests, but the second one is a 304 (does not load the
image twice)
* when the image has already been cached by the browser (but not in
this session), Chrome skips both requests; Firefox skips the AJAX
request, but sends the normal one, and it returns with 304.
"wish I knew this when I started" things:
* the Chrome DevTools has an option to disable cache. When this is
enabled, requests in the same document context still come from
cache (so if I load the page, fire an AJAX request, then without
reloading the page, fire an AJAX request to the same URL, then the
second request will be cached), but an AJAX request - image request
pair is an exception from this.
* when using Ctrl-F5 in Firefox, requests on that page will never hit
the cache (even AJAX request fired after user activity; even if
two identical requests follow each other). When using clear cache
+ normal reload, this is not the case.
* if the image does not have an Allow-Origin header and is loaded
with crossOrigin=true, Firefox will refuse to load it. Chrome will
log an error in the console saying it refused to load it, but will
actually load it.
* Wireshark rocks.
Pushed some tech debt (browser + domain whitelist) into other tickets:
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/232
https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/233
Reverted commits:
8a8d74f01d3dbd6d0c43b7fadc5284d204091761.
63021d0b0e95442cce101f9f92de8f0ff97d5f49.
Change-Id: I84ab2f3ac0a9706926adf7fe8726ecd9e9f843e0
Bug: 61542
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/207
2014-02-23 21:46:18 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return deferred;
|
|
|
|
}
|
2014-02-13 15:48:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the current browser supports AJAX preloading of images.
|
|
|
|
* This means that:
|
|
|
|
* - the browser supports CORS requests (large wiki farms usually host images on a
|
|
|
|
* separate domain) and
|
|
|
|
* - either AJAX and normal image loading uses the same cache (when an image is used by a CORS
|
|
|
|
* request, and then normally by setting img.src, it is only loaded once)
|
|
|
|
* - or (as is the case with Firefox) they are cached separately, but that can be changed by
|
|
|
|
* setting the crossOrigin attribute
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
imagePreloadingSupported() {
|
|
|
|
// This checks if the browser supports CORS requests in XHRs
|
|
|
|
return window.XMLHttpRequest !== undefined && 'withCredentials' in new XMLHttpRequest();
|
|
|
|
}
|
|
|
|
}
|
2014-03-03 16:17:07 +00:00
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
module.exports = ImageProvider;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|