User:Opencooper/domainRedirect.js
Appearance
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. |
This user script seems to have a documentation page at User:Opencooper/domainRedirect. |
// 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.uk
function setup() {
// If we're not reading an article, do nothing
if (!(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 result
if (!$("#contentSub").length) {
return;
}
getWikidata();
}
// First try to get the official site off Wikidata
function getWikidata() {
var wikidataId = mw.config.get( 'wgWikibaseItemId' );
if (wikidataId === null) {
getInfobox();
return;
}
// API docs: https://www.wikidata.org/w/api.php?action=help&modules=wbgetclaims
var url;
$.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 (typeof response.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 infobox
function getInfobox() {
var infoboxUrl;
// 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");
return false;
}
});
if (infoboxUrl) {
retrievalMethod = "website=";
checkRedirect(infoboxUrl);
} else {
// console.log("domainRedirect.js: Infobox website= failed");
getExternals();
}
}
// Finally, try to get it from the external links
function getExternals() {
// First try getting {{official website}}
var officialUrl = $(".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 official
var externalUrl = $("h2").has("#External_links").nextUntil("h2")
.find("li a.external").first();
if (externalUrl && /Official/i.test(externalUrl.text())) {
var externalHref = 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");
}
}
function checkRedirect(candidateUrl) {
var parser = new URL(candidateUrl);
var candidateDomain = parser.hostname;
candidateDomain = candidateDomain.replace(/^w+\d*\./, ""); // get rid of www
// API docs: https://www.mediawiki.org/wiki/API:Query
var apiUrl = 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;
}
}
});
}
function displayCandidate(candidateUrl) {
var pageTitle = mw.config.get("wgTitle");
pageTitle = encodeURIComponent(pageTitle).replace(/'/g, "%27");
var parser = new URL(candidateUrl);
var candidateDomain = parser.hostname;
candidateDomain = candidateDomain.replace(/^w+\d*\./, ""); // get rid of www
$("#contentSub").append("<span class='mw-redirectedfrom' id='domainRedirect'></span>");
var domainType = "domain";
if (candidateDomain.split(".").length-1 >= 2) {
domainType = "subdomain";
} else if (parser.pathname != "/") {
domainType = "subpage";
}
if (domainType != "domain") {
console.info("domainRedirect.js: Ignoring " + domainType + " redirect: "
+ candidateDomain);
$("#domainRedirect").hide();
}
var createUrl = "https://en.wikipedia.org/w/index.php?title="
+ candidateDomain + "&action=edit";
var editSummary = "Create redirect (using "
+ "[[User:Opencooper/domainRedirect|domainRedirect.js]])";
var editSummaryEncoded = encodeURIComponent(editSummary);
createUrl += "&preload=User:Opencooper/domainRedirectPreloads.js&preloadparams[]="
+ pageTitle + "&summary=" + editSummaryEncoded;
var domainMarkup = "<a title='via " + retrievalMethod + "' href='"
+ parser.origin + "'>" + candidateDomain + "</a>";
var candidateText = "Possible " + domainType + " redirect: " + domainMarkup;
var createLink = " <a href='" + createUrl + "' title='Create Redirect'>"
+ "+</a>";
var visitLink = "";
if (candidateUrl.replace(/\/$/, "") != parser.origin) {
visitLink = " <a href='" + candidateUrl + "' title='Visit Original Link'>"
+ "→</a>";
}
$("#domainRedirect").append(" (" + candidateText + createLink
+ visitLink + ")");
}
var retrievalMethod;
$(setup);