Content deleted Content added
Polygnotus (talk | contribs) No edit summary |
Polygnotus (talk | contribs) No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1:
// Wikipedia QuickSurveys Tracker for common.js
// Fetches
(function() {
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 89 ⟶ 92:
}
// Format link key for MediaWiki URL (capitalize only the first letter
function formatLinkKey(key) {
return key.
}
// Display survey information in console/notification instead of updating userpage
function
if (surveys.length === 0) {
mw.notify('No surveys found in QuickSurveys module', { type: 'info' });
return;
}
let notificationText = `Found ${surveys.length} survey(s): `;
surveys.forEach((survey, index) => {
if (survey.links.length > 0) {
survey.links.forEach(link => {
});
}
notificationText += survey.name;
if (index < surveys.length - 1) notificationText += ', ';
});
mw.notify(notificationText, { type: 'success', autoHide: false });
console.log('\n=== End of QuickSurveys ===');
}
// Main function to fetch and process surveys (for manual checking)
function fetchAndProcessSurveys() {
console.log('Fetching QuickSurveys data...');
Line 191 ⟶ 141:
console.log('Extracted surveys:', surveys);
})
.catch(error => {
console.error('Error fetching survey data:', error);
mw.notify('Error fetching QuickSurveys data: ' + error.message, { type: 'error' });
});
}
// Generate survey display when viewing userpage
function generateSurveyDisplay() {
const currentPage = mw.config.get('wgPageName');
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));
}
console.log('Generating survey display for userpage...');
fetch(QUICKSURVEYS_URL)
.then(response => response.text())
.then(moduleText => {
const surveys = extractSurveyData(moduleText);
if (surveys.length === 0) {
return;
}
// Double-check that the element doesn't exist (race condition protection)
if ($('#quicksurveys-display-box').length > 0) {
console.log('Survey display was created while fetching, skipping...');
return;
}
// Create a display box on the userpage
const $surveyBox = $('<div>')
.attr('id', 'quicksurveys-display-box')
.css({
'border': '1px solid #a2a9b1',
'background-color': '#f8f9fa',
'padding': '10px',
'margin': '10px 0',
'border-radius': '3px'
})
.html('<strong>Wikipedia QuickSurveys</strong><br>');
surveys.forEach(survey => {
$surveyBox.append(`<div style="margin: 5px 0;">
<strong>${survey.name}</strong> (${survey.type}, ${(survey.coverage * 100).toFixed(1)}% coverage)
</div>`);
if (survey.links.length > 0) {
survey.links.forEach(link => {
$surveyBox.append(`<div style="margin-left: 15px; font-size: 0.9em;">
→ <a href="${link.url}" target="_blank">${link.key}</a>
</div>`);
});
}
});
// Insert at the top of mw-content-text
const $content = $('#mw-content-text');
$content.prepend($surveyBox);
})
.catch(error => {
console.error('Error fetching survey data for display:', error);
});
}
Line 211 ⟶ 242:
if (username && currentPage === `User:${username}`) {
//
//
generateSurveyDisplay();
}, 100); // Small delay to ensure DOM is ready
}
});
console.log('QuickSurveys tracker loaded. Use window.updateQuickSurveys() to manually
})();
|