MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary Tags: Mobile edit Mobile web edit |
No edit summary |
||
| Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
/ | // Custom Random Page with Minimum Word Count | ||
$(document).ready(function() { | |||
// Override the default random page link behavior | |||
$('a[href*="Special:Random"]').on('click', function(e) { | |||
e.preventDefault(); // Prevent default link behavior | |||
(function() { | |||
// | |||
// Function to count words in text | |||
// | function countWords(text) { | ||
return text.replace(/[\s\n]+/g, ' ').trim().split(' ').length; | |||
} | } | ||
if ($content. | // Function to fetch a random page and check word count | ||
function getRandomPageWithMinWords(minWords) { | |||
$.ajax({ | |||
url: mw.util.wikiScript('api'), | |||
data: { | |||
action: 'query', | |||
format: 'json', | |||
list: 'random', | |||
rnnamespace: 0, // Main namespace only | |||
rnlimit: 1 | |||
}, | |||
dataType: 'json', | |||
success: function(data) { | |||
if (data.query && data.query.random && data.query.random.length > 0) { | |||
var pageId = data.query.random[0].id; | |||
// Fetch page content | |||
$.ajax({ | |||
url: mw.util.wikiScript('api'), | |||
data: { | |||
action: 'query', | |||
format: 'json', | |||
prop: 'revisions', | |||
pageids: pageId, | |||
rvprop: 'content', | |||
rvslots: 'main' | |||
}, | |||
dataType: 'json', | |||
success: function(contentData) { | |||
var pageContent = contentData.query.pages[pageId].revisions[0].slots.main['*']; | |||
var wordCount = countWords(pageContent); | |||
if (wordCount >= minWords) { | |||
// Redirect to the page if it meets the word count | |||
window.location = mw.util.getUrl(contentData.query.pages[pageId].title); | |||
} else { | |||
// Retry if word count is too low | |||
getRandomPageWithMinWords(minWords); | |||
} | |||
}, | |||
error: function() { | |||
// Retry on error | |||
getRandomPageWithMinWords(minWords); | |||
} | |||
}); | |||
} else { | |||
// Retry if no page is returned | |||
getRandomPageWithMinWords(minWords); | |||
} | |||
}, | |||
error: function() { | |||
// Retry on error | |||
getRandomPageWithMinWords(minWords); | |||
} | |||
}); | |||
} | } | ||
// | // Start the process with minimum 50 words | ||
getRandomPageWithMinWords(50); | |||
}); | |||
}); | |||
Revision as of 22:53, 27 October 2025
/* Any JavaScript here will be loaded for all users on every page load. */
// Custom Random Page with Minimum Word Count
$(document).ready(function() {
// Override the default random page link behavior
$('a[href*="Special:Random"]').on('click', function(e) {
e.preventDefault(); // Prevent default link behavior
// Function to count words in text
function countWords(text) {
return text.replace(/[\s\n]+/g, ' ').trim().split(' ').length;
}
// Function to fetch a random page and check word count
function getRandomPageWithMinWords(minWords) {
$.ajax({
url: mw.util.wikiScript('api'),
data: {
action: 'query',
format: 'json',
list: 'random',
rnnamespace: 0, // Main namespace only
rnlimit: 1
},
dataType: 'json',
success: function(data) {
if (data.query && data.query.random && data.query.random.length > 0) {
var pageId = data.query.random[0].id;
// Fetch page content
$.ajax({
url: mw.util.wikiScript('api'),
data: {
action: 'query',
format: 'json',
prop: 'revisions',
pageids: pageId,
rvprop: 'content',
rvslots: 'main'
},
dataType: 'json',
success: function(contentData) {
var pageContent = contentData.query.pages[pageId].revisions[0].slots.main['*'];
var wordCount = countWords(pageContent);
if (wordCount >= minWords) {
// Redirect to the page if it meets the word count
window.location = mw.util.getUrl(contentData.query.pages[pageId].title);
} else {
// Retry if word count is too low
getRandomPageWithMinWords(minWords);
}
},
error: function() {
// Retry on error
getRandomPageWithMinWords(minWords);
}
});
} else {
// Retry if no page is returned
getRandomPageWithMinWords(minWords);
}
},
error: function() {
// Retry on error
getRandomPageWithMinWords(minWords);
}
});
}
// Start the process with minimum 50 words
getRandomPageWithMinWords(50);
});
});