Jump to content

User:Chlod/Scripts/DirectPDFDownload.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Direct PDF Download
// Author: Chlod
// Version: 1.0.1-REL

// Forces the "Download as PDF" button to directly download the PDF version of
// the article.

$(document).ready(function () {
	$("li#coll-download-as-rl").on("mouseover", function (event) {
		var a = event.target.querySelector("a");
		if (!a || !a.getAttribute("href").includes("show-download-screen")) {
			return;
		}
		a.setAttribute(
			"href",
			"https://en.wikipedia.org/api/rest_v1/page/pdf/" + 
			encodeURIComponent(
				mw.util.wikiUrlencode(new URL(a.getAttribute("href"), window.location.href).searchParams.get("page"))
			)
		);
	});
});