Jump to content

User:Sir Sputnik/transfermarkt.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.
let domains = [".com", ".de", ".co.uk", ".ch", ".at", ".it", ".pt", ".pl", ".nl", ".es", ".gr", ".ro", ".fr", ".rs", ".com.tr", ".be", ".ie", ".co.in", ".dk", ".us", ".ru", ".ps", ".co.za", ".md", ".cm", ".lu", ".com.br", ".com.mt", ".com.hr", ".co.id", ".bg", ".cn", ".sg", ".my", ".mx", ".press", ".co", ".world", ".tv",".jp", ".pe", ".com.ar" ];
let protocols = ["http", "https"];

if(mw.config.get('wgPageName') == 'User:Sir_Sputnik/tm-list') {
	$('.mw-parser-output').prepend('<ul id="tm-links"></ul>');
	for(var tld of domains){
		for(var p of protocols){
			showArticles(tld,p,0);		
		}
	}
}

function showArticles(topLevelDomain,protocol,pageNum) {
	
	let offset = 5000*pageNum;
	$.ajax({
		method: "GET",
		url: `${location.origin}/w/index.php?title=Special:LinkSearch&limit=5000&offset=${offset}&target=${protocol}%3A%2F%2F*.transfermarkt${topLevelDomain}`,
		success: function(res){
			let parser = new DOMParser();
			let doc = parser.parseFromString(res, "text/html");
			let content = doc.getElementById("content");
			let articles = $(content).find('li a').not(".external").filter(function() { 
				return !$(this).text().includes(":") && !$(this).siblings(".external").text().includes("news")
			});
			console.log(articles.length + " articles found");
			articles.each(function(){
				let li = $(this).parent();
				li.detach();
				$("#tm-links").append(li);
			});
			
			if($(content).find("a.mw-nextlink").length != 0){
				console.log('continuing to next page');
				showArticles(topLevelDomain,protocol,pageNum+1)
			}
		}
	})
}