Przejdź do zawartości

Wikiskryba:Sohom Datta/common.js

Z Wikiźródeł, wolnej biblioteki

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5.
  • Opera: Naciśnij klawisze Ctrl+F5.
/* global mw, $ */
/**
 * Preload scans for next page.
 * 
 * Info o instalacji:
 * https://pl.wikisource.org/wiki/Dyskusja_wikiskryby:Nux/PreloadScans.js
 * 
 * Loads images so they are avilable in cache when going to a next page.
 * 
 * License: CC-BY or MIT.
 * Copyright © 2022 Maciej Nux Jaros.
 * Author(s): Nux.
 * 
 * Repo:
 * https://github.com/Eccenux/varia-linter-edit/tree/master/ws-varia-js
 */
(function(){

	/** Load next on edit page. */
	function loadOnEdit() {
		var imgEl = document.querySelector('.prp-page-image img');
		if (!imgEl) {
			console.warn('[preloadimg]', 'image not found');
			return false;
		}

		// add all current images to map
		var imgs = imgEl.srcset.split(',').map(function (v) { v.trim().replace(/ [0-9.]+x/, '') });
		imgs.push(imgEl.src);
		// de-dup just in case...
		imgs = Array.from(new Set(imgs));

		// load next images
		return loadNext(imgs);
	}

	/** Re-map to next and load images.  */
	function loadNext(imgSet) {
		// re-map to next page
		var matches = 0;
		var imgs = imgSet.map(function (src) { return src.replace(/(\/page)([0-9]+)/, function(a, pre, current){
			var next = parseInt(current, 10) + 1;
			matches++;
			return pre + next;
		});});

		if (!matches) {
			console.error('[preloadimg]', 'was not able to get next page url');
			return false;
		}

		preload(imgs);
		return true;
	}

	/** Preload array of images. */
	function preload(imgs) {
		var preload = [];
		imgs.forEach(function(src) {
			var i = new Image();
			i.src = src;
			console.log('loading:', src);
			preload.push(i);
		});
	}

	/** Load next on view page. */
	function loadOnView() {
		var imgUrl = mw.config.get('prpImageThumbnail', '')
		if (!imgUrl.length) {
			console.warn('[preloadimg]', 'config not found');
			return false;
		}

		// load next images
		return loadNext([imgUrl]);
	}

	// if: edit & Page
	if (mw.config.get( 'wgCanonicalNamespace') === 'Page' && (mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit') ) {
		$(function() {
			var waitSeconds = 12;
			setTimeout(function() {
				loadOnEdit();
			}, waitSeconds * 1000);
		});
	}

	// if: view & Page
	if (mw.config.get( 'wgCanonicalNamespace') === 'Page' && (mw.config.get('wgAction') === 'view') ) {
		$(function() {
			var waitSeconds = 5;
			setTimeout(function() {
				loadOnView();
			}, waitSeconds * 1000);
		});
	}

})();