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

Content deleted Content added
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
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');
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 ($('#updatequicksurveys-quicksurveysdisplay-box').length ===> 0) {
console.log('Survey display already exists, skipping...');
} else {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...');
Line 155 ⟶ 189:
const surveys = extractSurveyData(moduleText);
if (surveys.length === 0) {
e.preventDefault()return;
}
// Double-check that the element doesn't exist (race condition protection)
if ($firstPara('#quicksurveys-display-box').length > 0) {
console.log('Survey display was created while fetching, skipping...');
return;
}
Line 160 ⟶ 200:
// Create a display box on the userpage
const $surveyBox = $('<div>')
.attr('id', 'quicksurveys-display-box')
.css({
'border': '1px solid #a2a9b1',
Line 183 ⟶ 224:
});
// Insert afterat the firsttop paragraphof or at the topmw-content-text
const $content = $('#mw-content-text');
const $firstPara = $content.find('p').firstprepend($surveyBox);
if ($firstPara.length) {
$firstPara.after($surveyBox);
} else {
$content.prepend($surveyBox);
}
})
.catch(error => {
Line 206 ⟶ 242:
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();
}, 100); // Small delay to ensure DOM is ready
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>]'
);
$('#firstHeading').append($link);
$('#update-quicksurveys').on('click', function(e) {
e.preventDefault();
fetchAndProcessSurveys();
});
}
}
});