WaniKani Forums: Large Image Embedder

Embeds images which Discourse refuses to embed

Versione datata 14/02/2018. Vedi la nuova versione l'ultima versione.

// ==UserScript==
// @name         WaniKani Forums: Large Image Embedder
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  Embeds images which Discourse refuses to embed
// @author       Kumirei
// @include      https://community.wanikani.com*
// @require      https://greatest.deepsurf.us/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @grant        none
// ==/UserScript==

(function() {
		'use strict';

		// initialise script and set triggers
		initialiseScript();
		setTriggers();
})();

//embeds images if there are any broken ones to find
function embedImages() {
		$('.large-image-placeholder a').each(function() {
				console.log('swooosh');
				var url = $(this).attr('href');
				var img = document.createElement('img');
				img.src = url;
				var elem = $(this.closest('.large-image-placeholder'));
				elem.empty();
				elem.append(img);
		});
}

//calls the function to embed if there are any images to embed, or waits until there are
function initialiseScript() {
		if ($('.large-image-placeholder').length != 0) {
				embedImages();
		}
		else {
				waitForKeyElements('.large-image-placeholder a', function(){embedImages();});
		}
}

//sets the triggers for when the script should fire
function setTriggers() {
		window.addEventListener('load', function(){initialiseScript();}); //page load

		window.addEventListener('popstate', function(){initialiseScript();}); //back and forward buttons

		//navigating
		(function(history){
				var pushState = history.pushState;
				history.pushState = function(state) {
						initialiseScript();
						return pushState.apply(history, arguments);
				};
		})(window.history);

		//scrolling
		var i = 0;
		window.onscroll = function() {
				if (i % 50 == 0) {
						if ($('.large-image-placeholder').length != 0) {
								embedImages();
						}
				}
				i++;
		};
}