User:Maantietäjä/Citoid.js

/*** Citoid.js ***/

// Get a valid reference from the citoid // This is not a userscript // Provides the function "getCitoidRef" // Documentation at User:Maantietäjä/Citoid // By User:Maantietäjä

window.getCitoidRef = function(URL){ try { URL = decodeURIComponent(URL); } catch (e) {} URL = encodeURIComponent(URL); var dfd = $.Deferred; var cfg = mw.config.get('wgCitoidConfig'); var vecfg = mw.config.get('wgVisualEditorConfig'); var rest = cfg.fullRestbaseUrl || cfg.wbFullRestbaseUrl ? (cfg.fullRestbaseUrl || cfg.wbFullRestbaseUrl) + 'v1/data/citation/mediawiki/' : vecfg && vecfg.fullRestbaseUrl ? vecfg.fullRestbaseUrl + 'v1/data/citation/mediawiki/' : cfg.citoidServiceUrl ? cfg.citoidServiceUrl + '?format=mediawik&search=' : cfg ? '/api?format=mediawik&search=' : 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/' ;	dfd.notify('Loading reference data.'); $.get(rest + URL, {		action: 'query',		format : 'json'	}).then(function(res){		dfd.notify('Loaded reference data.');		var data = res[0];		dfd.notify('Loading Citoid template map.');		$.get(mw.config.get('wgScript'),{ title: 'MediaWiki:Citoid-template-type-map.json', action: 'raw', ctype: 'application/json' }).then(function(map){ dfd.notify('Loaded Citoid template map.'); var template = map[data.itemType]; dfd.notify('Loading template data.'); $.get(mw.config.get('wgScriptPath') + '/api.php', {				action: 'templatedata',				format: 'json',				formatversion: 2,				titles: 'Template:' + template,				doNotIgnoreMissingTitles: 1,				redirects: 1			}).then(function(tdata){				dfd.notify('Loaded template data.');				var out = {};				var str = '';				dfd.resolve(str, data.source);			},function{				dfd.reject('Unable to the template data from Template:' + template + '. Make sure that it exists and the API is enabled.');			}); },function{ dfd.reject('Unable to load MediaWiki:Citoid-template-type-map.json. make sure it exists.'); });	},function{		dfd.reject('Unable to get the data for the provided reference from citoid. Make sure it is valid and your internet connection to functioning.');	}); return dfd.promise; };