MediaWiki:Gadget-find-archived-section.js: Difference between revisions
Content deleted Content added
try to find section even if anchor is dot-encoded, load dependencies only when needed |
move all strings into an object for internationalization |
||
Line 26:
return;
}
var msg = {
"starter-text": "Looks like the discussion \"$1\" has been archived. ",
"finding": "Finding archived discussion...",
"click-link": "Click to see archived discussion",
"search-link-found": "(or search in archives)",
"search-link-notfound": "Click to search in archives"
};
$('#mw-content-text').before(
$('<div>')
.text(msg['
.addClass('archived-section-prompt')
.css({
Line 48 ⟶ 57:
}
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function() {▼
▲ var searchQuery = sectionNameDotDecoded === sectionName ?
'"' + sectionName + '" prefix:"' + prefix + '"' :
'"' + sectionName + '" OR "' + sectionNameDotDecoded + '" prefix:"' + prefix + '"';
new mw.Api({▼
▲ return new mw.Api({
ajax: { headers: { 'Api-User-Agent': 'w:en:User:SD0001/find-archived-section.js' } }
}).get({
Line 63 ⟶ 72:
srsort: 'create_timestamp_desc', // list more recent archives first
srlimit: '20'
});▼
}).then(function(json) {▼
if (!json || !json.query || !json.query.search) {▼
return;▼
}▼
var divHtml;▼
if (results.length === 0) {▼
}
▲ divHtml = 'No search results found for archived discussion "' + mw.html.escape(sectionName) + '"';
divHtml = msg['no-search-results'].replace('$1', mw.html.escape(sectionName));
} else {
// 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▼
var sectionNameDotDecoded_ = sectionNameDotDecoded.replace(/ /g, '_');▼
for (var i in results) {▼
var result = results[i];▼
// sectiontitle in API output has spaces encoded as underscores▼
if (result.sectiontitle) {▼
if (result.sectiontitle === sectionName_ ||▼
result.sectiontitle === sectionNameDotDecoded_) {▼
pageTitle = result.title;▼
sectionNameFound = result.sectiontitle.replace(/_/g, ' ');▼
var sectionName_ = sectionName.replace(/ /g, '_');
break;▼
▲ }
}
}
▲ }
}▼
}
}).fail(function(err) {▼
console.error('[find-archived-section]: ', JSON.stringify(err));▼
▲ });
});
|