User:Nihiltres/assesslinks.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. |
Documentation for this user script can be added at User:Nihiltres/assesslinks. |
/**
* Optional component for metadata script ([[MediaWiki:Gadget-metadata.js]]).
* This script adds a link to the toolbox. When clicked, the script finds the
* assessment of all articles linked from the current article and colors the
* links accordingly.
* @author Pyrospirit - Created the original script
* @author Nihiltres - Rewrote it to match his update to the gadget
*/
mw.loader.using("ext.gadget.metadata", function () {
assessment.links = (function () {
var assessmentLinksObj = {},
//internal shortcuts
al = assessmentLinksObj,
am = assessment.methods;
/**
* Assesses all the links on the page, making an AJAX query for each.
*/
al.assessLinks = function () {
var unassessedClass = "assess-wikilink " + am.renderAssessment({rating: "none"}).newClass,
rawArticlePath = mw.config.get("wgArticlePath").replace("$1", "");
$("#bodyContent a[href^=\"" + rawArticlePath + "\"]").each(function (i, e) {
var url = $(e).attr("href").replace(/#.*/, ""); //Bypass section links
if (url === "" || //empty string as the result of stripping out an internal section link
url === mw.config.get("wgPageName").replace(/(.*)/, mw.config.get("wgArticlePath")) || //Link to the current page
mw.Title.newFromText(decodeURIComponent(url.replace(rawArticlePath, ""))).namespace !== 0 //Link to non-mainspace page
) {
return true; //go back to the loop
}
$.ajax({
url: url.replace(
rawArticlePath,
mw.config.get("wgScript") + "?title=Talk:"
) + "&action=raw§ion=0",
cache: true,
async: true,
dataType: "text",
success: function (responseText) {
$(e).addClass("assess-wikilink " + am.renderAssessment(am.getAssessment(responseText)).newClass);
},
error: function (jqxhr) {
if (jqxhr.status === 404) {
$(e).addClass(unassessedClass);
}
}
});
});
};
/**
* Adds a link to activate the main assessLinks function.
*/
al.addToolboxLink = function () {
mw.util.addPortletLink(
"p-tb",
"#",
"Assess links",
"t-assess-article-links",
"Find the assessment of all articles linked and colour the links"
);
$("#t-assess-article-links").click(assessment.links.assessLinks);
};
/**
* Runs the part that adds the tab, and anything else necessary.
* Slightly redundant, but consistent form is nice.
*/
al.init = function () {
al.addToolboxLink();
};
return assessmentLinksObj;
}());
//Initializes the script on page load.
$(assessment.links.init);
mw.loader.load("//en.wikipedia.org/w/index.php?title=User:Pyrospirit/metadata/assesslinks.css&action=raw&ctype=text/css", "text/css");
});