This is an old revision of this page, as edited by Opencooper(talk | contribs) at 11:18, 3 December 2018(bring back external links test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.Revision as of 11:18, 3 December 2018 by Opencooper(talk | contribs)(bring back external links test)
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
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.
/* Determine whether or not the current article has a redirect from its URL pointing to it */functionsetup(){// If we're not reading an article, do nothingif(!(mw.config.get("wgAction")==="view"&&mw.config.get("wgIsArticle")&&mw.config.get("wgRevisionId")===mw.config.get("wgCurRevisionId")&&mw.config.get("wgPageName")!=="Main_Page")){return;}getWikidata();}// First try to get the official site off WikidatafunctiongetWikidata(){varwikidataId=mw.config.get('wgWikibaseItemId');if(wikidataId===null){getInfobox();return;}// API docs: https://www.wikidata.org/w/api.php?action=help&modules=wbgetclaimsvarurl;$.ajax({url:"https://www.wikidata.org/w/api.php",data:{action:"wbgetclaims",entity:wikidataId,property:"P856",languages:"en",format:"json",origin:"*"},success:function(response){if(typeofresponse.claims.P856!="undefined"){wikidataUrl=response.claims.P856[0].mainsnak.datavalue.value;console.log("domainRedirect.js: Found Url via wikidata: "+wikidataUrl);getRedirects(wikidataUrl);}else{// console.log("domainRedirect.js: Wikidata failed");getInfobox();}}});}// Then try getting a website in the infoboxfunctiongetInfobox(){varinfoboxUrl;$(".infobox tr").each(function(){if($(this).children("th").text()=="Website"){infoboxUrl=$(this).children("td").children("a").attr("href");returnfalse;}});if(infoboxUrl){console.log("domainRedirect.js: Found Url via infobox: "+infoboxUrl);getRedirects(infoboxUrl);}else{// console.log("domainRedirect.js: Infobox failed");getExternals();}}// Finally, try to get it from the external linksfunctiongetExternals(){// First try getting {{official website}}varofficialUrl=$(".official-website a").attr("href");if(officialUrl){console.log("domainRedirect.js: Found Url via {{official site}}: "+officialUrl);getRedirects(officialUrl);}else{// console.log("domainRedirect.js: No official site");}// Then try the external links section and the first link that says officialvarexternalUrl=$("h2").has("#External_links").nextUntil("h2").find("li a.external").first();if(externalUrl&&/Official/i.test(externalUrl.text())){console.log("domainRedirect.js: Found Url via External links section: "+externalUrl);getRedirects(externalUrl.attr("href"));}else{// console.log("domainRedirect.js: No external links list found");console.log("domainRedirect.js: No candidates found");}}functiongetRedirects(candidateUrl){// API docs: https://www.mediawiki.org/wiki/API:Redirects$.ajax({url:"https://en.wikipedia.org/w/api.php",data:{action:"query",prop:"redirects",format:"json",titles:mw.config.get("wgPageName"),rdprop:"title",rdshow:"!fragment",rdlimit:"max"},success:function(response){varpageId=mw.config.get("wgArticleId");varredirectObject=response.query.pages[pageId];if(typeofredirectObject.redirects!="undefined"){redirects=redirectObject.redirects;checkRedirectExistence(redirects,candidateUrl);}else{// console.log("domainRedirect.js: No inbound redirects");displayCandidate(candidateUrl);}}});}functioncheckRedirectExistence(redirects,candidateUrl){for(varrofredirects){vartitle=r.title;if(!/\./.test(title)){continue;}// console.log("domainRedirect.js: Looking for " + title + " in " + candidateUrl);// We search for the title inside the URL, but we could always use the hostnamevartitleEscaped=mw.RegExp.escape(title);varre=newRegExp(titleEscaped,"i");if(re.test(candidateUrl)){console.log("domainRedirect.js: Domain redirect already present: "+title);return;}}displayCandidate(candidateUrl);}functiondisplayCandidate(candidateUrl){varpageTitle=mw.config.get("wgPageName").replace(/_/g," ");varparser=newURL(candidateUrl);candidateDomain=parser.hostname;candidateDomain=candidateDomain.replace(/^w+\d*\./,"");// get rid of wwwvarcreateUrl="https://en.wikipedia.org/w/index.php?title="+candidateDomain+"&action=edit";varpreload="&preload=User:Opencooper/domainRedirectPreloads.js&preloadparams[]="+pageTitle+"&summary=Create%20redirect";varcreateLink="<a href='"+createUrl+preload+"' title='"+candidateUrl+"'>"+candidateDomain+"</a>";$("#contentSub").append("<span class='mw-redirectedfrom' id='domainRedirect'>"+" (Possible ___domain redirect: "+createLink+")</span>");}$(setup);