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

Content deleted Content added
No edit summary
No edit summary
Line 1:
// Wikipedia QuickSurveys Tracker for common.js
// Fetches active surveys and postsdisplays them toon your userpage
 
(function() {
Line 63:
url: MEDIAWICK_BASE_URL + formatLinkKey(survey.link)
});
});
}
// Generate survey display when viewing userpage
function generateSurveyDisplay() {
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;
}
// Create a display box on the userpage
const $surveyBox = $('<div>')
.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 after the first paragraph or at the top
const $content = $('#mw-content-text');
const $firstPara = $content.find('p').first();
if ($firstPara.length) {
$firstPara.after($surveyBox);
} else {
$content.prepend($surveyBox);
}
})
.catch(error => {
console.error('Error fetching survey data for display:', error);
});
}
Line 146 ⟶ 94:
}
// Display survey information in console/notification instead of updating userpage
// Generate wiki markup for the surveys
function generateSurveyMarkupdisplaySurveyInfo(surveys) {
if (surveys.length === 0) {
return console.log('== Wikipedia QuickSurveys ==\n\nNoNo surveys found in the QuickSurveys module at this time.\n\n');
mw.notify('No surveys found in QuickSurveys module', { type: 'info' });
return;
}
let markup = console.log('=== Wikipedia QuickSurveys ==\n\n=');
markup += console.log(`Last updated:Found ${newsurveys.length} Datesurvey(s).toUTCString(:`)}\n\n`;
let notificationText = `Found ${surveys.length} survey(s): `;
surveys.forEach(survey => {
markup += `=== ${survey.name} ===\n`;
surveys.forEach((survey, index) => {
markup += `* '''Type:''' ${survey.type}\n`;
markupconsole.log(`\n${index += `* '''Coverage:'''1}. ${(survey.coverage * 100).toFixed(1)name}%\n`);
console.log(` Type: ${survey.type}`);
console.log(` Coverage: ${(survey.coverage * 100).toFixed(1)}%`);
if (survey.links.length > 0) {
markupconsole.log(' += `* '''MediaWiki Links:'''\n`);
survey.links.forEach(link => {
markupconsole.log(` += `** [${link.url} - ${link.key}]: (${link.typeurl})\n`);
});
}
markup += '\n';
notificationText += survey.name;
if (index < surveys.length - 1) notificationText += ', ';
});
mw.notify(notificationText, { type: 'success', autoHide: false });
return markup;
console.log('\n=== End of QuickSurveys ===');
}
// Main function to fetch and process surveys (for manual checking)
// Update userpage with survey information
function updateUserpage(content) {
const username = mw.config.get('wgUserName');
if (!username) {
console.error('Not logged in - cannot update userpage');
return;
}
const pageTitle = `User:${username}`;
// Get current page content
new mw.Api().get({
action: 'query',
prop: 'revisions',
titles: pageTitle,
rvprop: 'content',
rvlimit: 1,
formatversion: 2
}).then(data => {
const pages = data.query.pages;
if (pages.length === 0) {
console.error('Could not find userpage');
return;
}
let currentContent = '';
if (pages[0].revisions && pages[0].revisions[0]) {
currentContent = pages[0].revisions[0].content;
}
// Remove existing survey section if it exists
const sectionRegex = /== Wikipedia QuickSurveys ==[\s\S]*?(?=\n== |\n----|\n\[\[Category:|\n\{\{|\n<|\n#|$)/;
let newContent = currentContent.replace(sectionRegex, '').trim();
// Add new survey section
if (newContent) {
newContent += '\n\n' + content;
} else {
newContent = content;
}
// Save the page
return new mw.Api().postWithToken('csrf', {
action: 'edit',
title: pageTitle,
text: newContent,
summary: 'Updated active QuickSurveys information via common.js script',
minor: true
});
}).then(() => {
console.log('Successfully updated userpage with survey information');
mw.notify('QuickSurveys information updated on your userpage!', { type: 'success' });
}).catch(error => {
console.error('Error updating userpage:', error);
mw.notify('Error updating userpage: ' + error.message, { type: 'error' });
});
}
// Main function to fetch and process surveys
function fetchAndProcessSurveys() {
console.log('Fetching QuickSurveys data...');
Line 246 ⟶ 143:
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() {
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;
}
// Create a display box on the userpage
const $surveyBox = $('<div>')
.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 after the first paragraph or at the top
const $content = $('#mw-content-text');
const $firstPara = $content.find('p').first();
if ($firstPara.length) {
$firstPara.after($surveyBox);
} else {
$content.prepend($surveyBox);
}
})
.catch(error => {
console.error('Error fetching survey data for display:', error);
});
}
Line 278 ⟶ 226:
});
console.log('QuickSurveys tracker loaded. Use window.updateQuickSurveys() to manually updatecheck, or visit your userpage andto clicksee thesurveys updatedisplayed linkautomatically.');
})();