

How to get URLs in JavaScript for uploaded files in MediaWiki
source link: https://hexmode.com/2022/09/15/how-to-get-urls-in-javascript-for-uploaded-files-from-js-in-mediawiki/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to get URLs in JavaScript for uploaded files in MediaWiki

Today, I was trying to remove hard-coded paths from some JS code.
In my particular case, I started with a stem that looked like this:
/img_auth.php/c/c9/Logo.png
But MediaWiki provides different ways to configure it to serve images.
The hard-coded url wouldn’t work in a wiki that had $wgHashedUploadDirectory set to false. And it wouldn’t work in a wiki where img_auth.php wasn’t being used.
I wanted to be able to provide the client-side with a URL that would display the image in the file Logo.png no matter how the wiki was configured.
I thought someone else might have a clue, so I asked in the MediaWiki Extensions room on Matrix. Marijn told me about the little known Special:Filepath (or Special:Redirect/file) special page that would do this.
After a little testing, it worked. A bit of JQuery and MW.js and I have the following that provides the client with the appropriate url path regardless of the wiki’s configuration:
var url=mw.util.getUrl("Special:Redirect/file/Logo.png");
$("#image-here").append("<img src='" + url + "' />");
Image credit: Tomas Castelazo, CC BY-SA 3.0, via Wikimedia Commons
Recommend
-
133
Google uploaded linux kernel sources for Pixel Visual Core(Google Easel Image Co-processor) based Intel Monette Hill Platform. ...
-
21
Platform Evolution is one of the five pillars of the Wikimedia Foundation's Medium Term Plan. The need to evolve our platform is very evident when it comes to how we design, develop, and deliver experiences to users in th...
-
6
JavaScript – Base64 Data URLs Outside of the Latin1 Range – Chris West's BlogIt is often handy to create downloadable content on the fly using JavaScript. Sometimes you may want to do so while also encoding the contents as base64. That is w...
-
9
Tech
-
3
Download uploaded files only from Django Admin Django • Jul 25, 2022...
-
5
A Uniform Resource Locator (URL) is what leads you to a page or file on the internet. URLs serve as the addresses of things on the internet.All valid URLs follow certain patterns. So if you know those patterns, you can determine whether a U...
-
7
A Handy Guide to Parsing URLs in JavaScriptA Handy Guide to Parsing URLs in JavaScriptMarch 1st 2023 New Story7 min...
-
5
How To Parse URLs in JavaScript In this article, readers will do a...
-
8
Code is available on GitHub Files with AppSync AppSync, as all serverless solutions, builds on the “small an...
-
9
How to Download Files From URLs With Python – Real PythonFacilitating File Downloads With Python While it’s possible to download files from
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK