MediaWiki:Gadget-find-archived-section.js: Difference between revisions

Content deleted Content added
update: add tilde (~) in search string to correctly handle section names where an article/redirect of the same name exists; remove hashColonFix as no longer necessary; also correctly handles section names beginning with a word then a colon (like "User:")
Version 2: shows a link pointing directly to the archived discussion if it can be found
Line 1:
$.when($.ready).then(function() {
 
var addsection = document.getElementById('ca-addsection');
var correctNs = mw.config.get('wgNamespaceNumber') % 2 === 1 || mw.config.get('wgNamespaceNumber') === 4;
ifvar (!correctNsminerva &&= mw.user.optionsconfig.get('skin') === 'minerva') return;
 
// Show only on discussion pages (pages with "add section" button)
if (!addsection && mw.user.options.get('skin') !== 'minerva') return;
// On minerva skin (which doesn't use have add section button) show on all talk & project space pages
if (!correctNs && mw.user.options.get('skin') === 'minerva') return;
if (!addsection && (!correctNs || !minerva)) {
return;
}
 
var hashsectionName = decodeURIComponent(window.___location.hash.slice(1)).replace(/_/g, ' ');
if (hash === ''!sectionName || document.getElementById(hashsectionName.replace(/ /g, '_')) !== null) {
return;
}
 
$('#mw-content-text').before(div);
$('<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";
Line 20 ⟶ 33:
prefix = "Wikipedia:Administrators' noticeboard/Archive";
}
prefix = encodeURIComponent(prefix);
 
var searchsearchString = encodeURIComponent('~"' + hash.replace(/_/g, ' ')sectionName + '"');
 
var searchLink = mw.config.get('wgScript')
+ '?title=Special:Search'
+ '&search=' + searchencodeURIComponent(searchString)
+ '&prefix=' + encodeURIComponent(prefix);
 
// Fetch and parse search results page
$.ajax(searchLink).then(function(html) {
if ($(html).find('.mw-search-nonefound').length) {
var divHtml = 'No search results found for archived discussion "' + sectionName + '"';
 
} else {
div.innerHTML var divHtml = 'Looks like the discussion "' + hash.replace(/_/g,sectionName + '" has been archived. ') +;
var discussionLink = $(html).find('ul.mw-search-results .searchalttitle a').attr('href');
if (discussionLink) {
divHtml += '<b><a href="' + encodeURI(discussionLink) + '">Click to see archived discussion</a></b> ';
'" divHtml has been archived.+= '<small><a href="' + searchLink + '">Click to(or search in archives)</a></small>. ';
} else {
divHtml += '<a href="' + searchLink + '">Click to search in archives</a>. ';
}
}
 
$('.archived-section-prompt').html(divHtml);
var div = document.createElement('div');
div.innerHTML = 'Looks like the discussion "' + hash.replace(/_/g, ' ') +
'" has been archived. <a href="' + searchLink + '">Click to search in archives</a>.';
div.style.fontSize = '90%';
div.style.paddingLeft = '20px';
 
}).fail(function(err) {
$('#mw-content-text').before(div);
console.error('[find-archived-section]: ', JSON.stringify(err));
});
 
});