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. |
/* Determine whether or not the current article has a redirect from its URL pointing to it */
function setup() {
// If we're not reading an article, do nothing
if (!(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 Wikidata
function getWikidata() {
var wikidataId = mw.config.get( 'wgWikibaseItemId' );
if (wikidataId === null) {
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;
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 infobox
function getInfobox() {
var infoboxUrl;
$(".infobox tr").each(function() {
if ($(this).children("th").text() == "Website") {
infoboxUrl = $(this).children("td").children("a").attr("href");
return false;
}
});
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 links section
function getExternals() {
// First try getting {{official website}}
var officialUrl = $(".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 grab the first link we find
var externalUrl = $("h2").has("#External_links").nextUntil("h2").find("li a.external").first().attr("href");
if (externalUrl) {
console.log("domainRedirect.js: Found Url via External links section: " + externalUrl);
getRedirects(externalUrl);
} else {
console.log("domainRedirect.js: No external links list found");
console.log("domainRedirect.js: No candidates found");
}
}
function getRedirects(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) {
var pageId = mw.config.get("wgArticleId");
var redirects = response.query.pages[pageId].redirects;
checkRedirectExistence(redirects, candidateUrl);
}
});
}
function checkRedirectExistence(redirects, candidateUrl) {
for (var r of redirects) {
var title = r.title;
console.log("domainRedirect.js: Comparing " + candidateUrl + " to " + title);
var titleEscaped = mw.RegExp.escape(title);
var re = new RegExp(titleEscaped, "i");
if (candidateUrl.test(re)) {
console.log("domainRedirect.js: Domain redirect present: " + title);
return;
}
}
// displayCandidate(candidateDomain);
}
$(setup);