User:Polygnotus/Scripts/Surveys.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 8:
const QUICKSURVEYS_URL = 'https://en.wikipedia.org/w/load.php?modules=ext.quicksurveys.lib&debug=true';
const MEDIAWICK_BASE_URL = 'https://en.wikipedia.org/wiki/MediaWiki:';
// Track processed pages to prevent duplicates
let processedPages = new Set();
// Function to extract survey data from the loaded module
Line 148 ⟶ 151:
// Generate survey display when viewing userpage
function generateSurveyDisplay() {
const currentPage = mw.config.get('wgPageName');
// Prevent duplicate execution
const username = mw.config.get('wgUserName');
const pageKey = `${currentPage}-${Date.now()}`;
);
// Check if we've already processed this page recently (within 1 second)
const now = Date.now();
const recentProcessing = Array.from(processedPages).find(entry => {
const [page, timestamp] = entry.split('-');
return page === currentPage && (now - parseInt(timestamp)) < 1000;
});
if (recentProcessing) {
console.log('Survey display recently processed, skipping...');
return;
}
// Prevent duplicate execution by checking for existing elements
if ($('#quicksurveys-display-box').length > 0) {
console.log('Survey display already exists, skipping...');
return;
}
// Add to processed pages
processedPages.add(`${currentPage}-${now}`);
// Clean up old entries (keep only last 10)
if (processedPages.size > 10) {
const sortedEntries = Array.from(processedPages).sort();
processedPages = new Set(sortedEntries.slice(-10));
}
Line 161 ⟶ 189:
const surveys = extractSurveyData(moduleText);
if (surveys.length === 0) {
return;
}
// Double-check that the element doesn't exist (race condition protection)
if ($('#update-quicksurveys-display-box').length ===> 0) {
console.log('Survey display was created while fetching, skipping...');
return;
}
Line 213 ⟶ 247:
if (username && currentPage === `User:${username}`) {
// Auto-generateUse surveysetTimeout displayto onensure userpageDOM is ready and avoid race conditions
generateSurveyDisplaysetTimeout((); => {
// Auto-generate survey display on userpage
// Only add link if it doesn't already existgenerateSurveyDisplay();
if ($('#update-quicksurveys').length === 0) {
// Add a small link to manually update
const $link = $('<span style="font-size: 0.8em; margin-left: 1em;">').html(
'[<a href="#" id="update-quicksurveys">Check QuickSurveys</a>]'
);
$(// Only add link if it doesn'#firstHeading').append($link);t already exist
if ($('#update-quicksurveys').length === 0) {
// Add a small link to manually update
$('#update-quicksurveys').on('click', function(e) {
e.preventDefaultconst $link = $()'<span style="font-size: 0.8em; margin-left: 1em;">').html(
fetchAndProcessSurveys(); '[<a href="#" id="update-quicksurveys">Check QuickSurveys</a>]'
} );
}
$('#firstHeading').append($link);
$('#update-quicksurveys').on('click', function(e) {
e.preventDefault();
fetchAndProcessSurveys();
});
}
}, 100); // Small delay to ensure DOM is ready
}
});