MediaWiki talk:LAPI.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Dependencies on other scripts

[edit]

This script makes use of the following other Javascripts:

Script Type of
dependency
Purpose
MediaWiki:AjaxSubmit.js

Usage

[edit]

Used e.g. in ImageAnnotator. Lupo 09:06, 17 June 2009 (UTC)[reply]

JavaScript return errors in this file

[edit]

Hi @User:Lupo,

I noticed in Firefox requesting a commons page in ResourceLoader debug mode (e.g. https://commons.wikimedia.org/wiki/File:WMF_Monthly_Metrics_Meeting_August_6,_2015.webm?debug=1 ), the JavaScript console complains about this file:

 SyntaxError: unreachable code after return statement index.php:471:10
        return
          'Exception ' + name + ': ' + msg
          + (file ? '\nFile ' + file + (line ? ' (' + line + ')' : "") : "")
          ;
 SyntaxError: unreachable code after return statement index.php:876:8
 SyntaxError: unreachable code after return statement  index.php:876:8
      return
        element.ownerDocument.defaultView.getComputedStyle (element, null).getPropertyValue (property);

Both return statements return immediately and the line(s) afterwards do nothing, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return#Automatic_Semicolon_Insertion . You need to return something on the same line, and have the trailing + on the same line.

Interestingly the wiki CodeEditor doesn't complain about these (it warns about some other stuff), nor does chromium-browser. But Firefox and jshint do.

--SPage (WMF) (talk) 20:52, 25 August 2015 (UTC)[reply]

Commons:User_scripts/reports/MediaWiki:LAPI.js reports these errors (plus others), I think as ISSUE: line 875 character 13: Missing semicolon. - Evidence: return -- SPage (WMF) (talk) 21:07, 25 August 2015 (UTC)[reply]
I totally missed this. Has been fixed nearly a year later in this diff. Lupo 09:09, 11 June 2016 (UTC)[reply]

This redefines Object and String.prototype everywhere

[edit]

It seems this is loaded everywhere on commons, which means every page gets a modified Object with e.g. mergeSome() and a String.prototype.decodeXML(), etc. See the guidance in mw:ResourceLoader/Migration guide (users). I wonder if some bug in this exposed by recent changes to JS loading might be causing phab:T110245 - TimedText page "Select language and press the Go button" often does nothing in Firefox. -- SPage (WMF) (talk) 21:49, 25 August 2015 (UTC)[reply]

Minor bug fix

[edit]

{{Editrequest}}

Could somebody please apply this diff here? For context, see [1]. Lupo 18:57, 10 June 2016 (UTC)[reply]

✓ Done This is a case of "sync bugfix from en.wp", for those keeping track. Thanks! DMacks (talk) 08:42, 11 June 2016 (UTC)[reply]
Thanks. Lupo 09:06, 11 June 2016 (UTC)[reply]

URI malformed error

[edit]

File:Jan-Bart_de_Vreede_voice_-_en.ogg throws Uncaught URIError: URI malformed from getPreviewImage() (via MediaWiki:Gadget-ImageAnnotator.js). Presumably this is because it assumes a visual canvas, which isn't there for audio files. –Krinkle 17:37, 6 July 2016 (UTC)[reply]

✓ Done -- User: Perhelion 09:33, 26 September 2017 (UTC)[reply]

Make getPreviewImage accept query parameters

[edit]

{{Editprotected}}

Please replace getPreviewImage (around line 1400) by

		/**
		 * Get the preview image on a file description page.
		 * @param {string} [title] The title of the image to get,
		 *  defaults to the current page name without namespace.
		 * @param {Document} [doc] The document to get the image from,
		 *  defaults to the current document.
		 * @returns {HTMLImageElement|null} The preview image, or `null`
		 *  if it could not be found.
		 */
		getPreviewImage: function ( title, doc ) {
			var file_div = LAPI.$( 'file', doc );
			if ( !file_div ) {
				// Catch page without file...
				return null;
			}

			var imgs = file_div.getElementsByTagName( 'img' );
			title = title || mw.config.get( 'wgTitle' );

			for ( var i = 0; i < imgs.length; i++ ) {
				var src = imgs[i].getAttribute( 'src' );

				if ( src && !src.match( /^data/ ) ) {
					src = decodeURIComponent( src ).replace( '%26', '&' );

					if ( src.match( new RegExp( '^' + LAPI_file_store + '.*/' + title.replace( / /g, '_' ).escapeRE() + '([?/].*)?$' ) ) ) {
						return imgs[i];
					}
				}
			}

			return null;
		},

The main change is that the regexp accepts query parameters after the file name (e.g. https://upload.wikimedia.org/wikipedia/commons/d/dd/Hochfeiler_in_Alps.jpg?20091128123010 for File:Hochfeiler in Alps.jpg – previously it would only accept https://upload.wikimedia.org/wikipedia/commons/d/dd/Hochfeiler_in_Alps.jpg). In addition to this functional change, it also improves the code readability by introducing documentation and using String.prototype.match(), which returns a truthy value on match, instead of String.prototype.search(), which returns a falsy value if and only if it matches at the beginning of the string (i.e. it returns a truthy value if it doesn’t match, and returns a falsy value if it matches, as both regexes start with ^). —Tacsipacsi (talk) 20:14, 16 March 2023 (UTC)[reply]

After digging into the Git history, it looks like it was broken by phab:T38380. —Tacsipacsi (talk) 21:17, 20 March 2023 (UTC)[reply]
✓ Done Matma Rex (talk) 22:23, 20 March 2023 (UTC)[reply]