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.
// Determines whether or not the current article has a redirect from its URL pointing to it// License: CC0// FIXME: isn't case insensitive; see [[The Psycho Ex-Wife]]// FIXME: Doesn't work with TLDs like .co.ukfunctionsetup(){// If we're not reading an article, do nothingif(!(mw.config.get("wgAction")==="view"&&mw.config.get("wgIsArticle")&&!location.search.split('oldid=')[1]&&!mw.config.get("wgIsMainPage"))){return;}// Make sure we have somewhere to put resultif(!$("#contentSub").length){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;retrievalMethod="Wikidata";checkRedirect(wikidataUrl);}else{// console.log("domainRedirect.js: Wikidata failed");getInfobox();}}});}// Then try getting a website in the infoboxfunctiongetInfobox(){varinfoboxUrl;// Get {{Url|}}infoboxUrl=$(".infobox .url a").last().attr("href");if(infoboxUrl){retrievalMethod="{{Url}}";checkRedirect(infoboxUrl);return;}else{// console.log("domainRedirect.js: Infobox {{Url}} failed");}// Get |website= $(".infobox tr").each(function(){if($(this).children("th").text()=="Website"){infoboxUrl=$(this).children("td").children("a").attr("href");returnfalse;}});if(infoboxUrl){retrievalMethod="website=";checkRedirect(infoboxUrl);}else{// console.log("domainRedirect.js: Infobox website= failed");getExternals();}}// Finally, try to get it from the external linksfunctiongetExternals(){// First try getting {{official website}}varofficialUrl=$(".official-website a").first().attr("href");if(officialUrl){retrievalMethod="{{official site}}";checkRedirect(officialUrl);return;}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())){varexternalHref=externalUrl.attr("href");retrievalMethod="§External_links";checkRedirect(externalHref);}else{// console.log("domainRedirect.js: No external links list found");console.info("domainRedirect.js: No candidates found");}}functioncheckRedirect(candidateUrl){varparser=newURL(candidateUrl);varcandidateDomain=parser.hostname;candidateDomain=candidateDomain.replace(/^w+\d*\./,"");// get rid of www// API docs: https://www.mediawiki.org/wiki/API:QueryvarapiUrl=location.origin+"/w/api.php";$.ajax({url:apiUrl,data:{action:"query",format:"json",titles:candidateDomain,},success:function(response){if(response.query.pages["-1"]){displayCandidate(candidateUrl);}else{console.info("domainRedirect.js: Domain redirect already present: "+candidateDomain);return;}}});}functiondisplayCandidate(candidateUrl){varpageTitle=mw.config.get("wgTitle");pageTitle=encodeURIComponent(pageTitle).replace(/'/g,"%27");varparser=newURL(candidateUrl);varcandidateDomain=parser.hostname;candidateDomain=candidateDomain.replace(/^w+\d*\./,"");// get rid of www$("#contentSub").append("<span class='mw-redirectedfrom' id='domainRedirect'></span>");vardomainType="domain";if(candidateDomain.split(".").length-1>=2){domainType="subdomain";}elseif(parser.pathname!="/"){domainType="subpage";}if(domainType!="domain"){console.info("domainRedirect.js: Ignoring "+domainType+" redirect: "+candidateDomain);$("#domainRedirect").hide();}varcreateUrl="https://en.wikipedia.org/w/index.php?title="+candidateDomain+"&action=edit";vareditSummary="Create redirect (using "+"[[User:Opencooper/domainRedirect|domainRedirect.js]])";vareditSummaryEncoded=encodeURIComponent(editSummary);createUrl+="&preload=User:Opencooper/domainRedirectPreloads.js&preloadparams[]="+pageTitle+"&summary="+editSummaryEncoded;vardomainMarkup="<a title='via "+retrievalMethod+"' href='"+parser.origin+"'>"+candidateDomain+"</a>";varcandidateText="Possible "+domainType+" redirect: "+domainMarkup;varcreateLink="<a href='"+createUrl+"' title='Create Redirect'>"+" +</a>";varvisitLink="";if(candidateUrl.replace(/\/$/,"")!=parser.origin){visitLink="<a href='"+candidateUrl+"' title='Visit Original Link'>"+" →</a>";}$("#domainRedirect").append(" ("+candidateText+createLink+visitLink+")");}varretrievalMethod;$(setup);