MediaWiki:Gadget-find-archived-section.js
Appearance
![]() | This page is loaded as a part of the find-archived-section gadget, used by 5,258 users. |
$.ready.then(function() {
var addsection = document.getElementById('ca-addsection');
var correctNs = mw.config.get('wgNamespaceNumber') % 2 === 1 || mw.config.get('wgNamespaceNumber') === 4;
var minerva = mw.config.get('skin') === 'minerva';
// Show only on discussion pages (pages with "add section" button)
// On minerva skin (which doesn't use have add section button) show on all talk & project space pages
if (!addsection && (!correctNs || !minerva)) {
return;
}
var sectionName = decodeURIComponent(window.location.hash.slice(1)).replace(/_/g, ' ');
if (!sectionName || // no section name in URL
sectionName.indexOf('/media/') === 0 || // URLs used by MediaViewer
document.getElementById(sectionName.replace(/ /g, '_')) !== null) { // section exists on page
return;
}
$('#mw-content-text').before(
$('<div>')
.html('Looks like the discussion "' + sectionName + '" has been archived. Finding archived discussion...')
.addClass('archived-section-prompt')
.css({
'font-size': '90%',
'padding-left': '20px'
})
);
var prefix = mw.config.get('wgPageName').replace(/_/g, ' ');
// For admin noticeboards, archive pages titles are unusual
if (prefix === "Wikipedia:Administrators' noticeboard/Incidents") {
prefix = "Wikipedia:Administrators' noticeboard/IncidentArchive";
} else if (prefix === "Wikipedia:Administrators' noticeboard/Edit warring") {
prefix = "Wikipedia:Administrators' noticeboard/3RRArchive";
} else if (prefix === "Wikipedia:Administrators' noticeboard") {
prefix = "Wikipedia:Administrators' noticeboard/Archive";
}
var searchString = '~"' + sectionName + '"';
var searchLink = mw.config.get('wgScript')
+ '?title=Special:Search'
+ '&search=' + encodeURIComponent(searchString)
+ '&prefix=' + encodeURIComponent(prefix)
+ '&sort=create_timestamp_desc';
// Fetch and parse search results page
$.ajax(searchLink).then(function(html) {
var divHtml;
if ($(html).find('.mw-search-nonefound').length) {
divHtml = 'No search results found for archived discussion "' + sectionName + '"';
} else {
divHtml = 'Looks like the discussion "' + sectionName + '" has been archived. ';
var discussionLink;
// obtain the the first exact section title match (which would be from the most recent archive)
// this loop iterates over just one item in the vast majority of cases
$(html).find('ul.mw-search-results .searchalttitle a').each(function() {
if (this.textContent === sectionName) {
discussionLink = this.getAttribute('href');
return false; // break
}
});
if (discussionLink) {
// escape double quotes in link, so that they aren't treated as end of href
// encodeURI() doesn't work here as it tends to escape characters such as the single quote which
// have already been escaped by mediawiki, resulting in a doubly-escaped malformed url
divHtml += '<b><a href="' + discussionLink.replace(/"/g, '%22') + '">Click to see archived discussion</a></b> ';
divHtml += '<small><a href="' + searchLink + '">(or search in archives)</a></small>. ';
} else {
divHtml += '<a href="' + searchLink + '">Click to search in archives</a>. ';
}
}
$('.archived-section-prompt').html(divHtml);
}).fail(function(err) {
console.error('[find-archived-section]: ', JSON.stringify(err));
});
});