User:CFA/scripts/attributetranslation.js
Appearance
< User:CFA
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:CFA/scripts/attributetranslation. |
(function() {
'use strict';
function createInputGUI() {
var gui = document.createElement('div');
gui.style.position = 'fixed';
gui.style.top = '50%';
gui.style.left = '50%';
gui.style.transform = 'translate(-50%, -50%)';
gui.style.backgroundColor = '#f8f9fa';
gui.style.border = '1px solid #a2a9b1';
gui.style.padding = '20px';
gui.style.zIndex = 10000;
gui.innerHTML = `
<label for="langCode">Language Code:</label>
<input type="text" id="langCode" placeholder="e.g. zh, es"><br><br>
<label for="articleName">Article Name:</label>
<input type="text" id="articleName" placeholder="Exact name of article"><br><br>
<button id="submitTranslation">Submit</button>
<button id="closeGUI">Close</button>
`;
document.body.appendChild(gui);
document.getElementById('submitTranslation').addEventListener('click', function() {
var langCode = document.getElementById('langCode').value.trim();
var articleName = document.getElementById('articleName').value.trim();
if (langCode && articleName) {
processTranslation(langCode, articleName);
document.body.removeChild(gui);
} else {
alert('Please fill in both fields.');
}
});
document.getElementById('closeGUI').addEventListener('click', function() {
document.body.removeChild(gui);
});
}
function processTranslation(langCode, articleName) {
var pageTitle = mw.config.get('wgPageName').replace(/_/g, ' ');
var editSummary = `Content in this edit is translated from the existing ${langCode} Wikipedia article at ${langCode}:${articleName}; see its history for attribution.`;
$.ajax({
url: mw.util.wikiScript('api'),
type: 'GET',
data: {
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'content',
titles: pageTitle
},
success: function(data) {
var pages = data.query.pages;
var pageContent = '';
for (var pageId in pages) {
if (pages.hasOwnProperty(pageId)) {
pageContent = pages[pageId].revisions[0]['*'];
}
}
pageContent += ' ';
$.ajax({
url: mw.util.wikiScript('api'),
type: 'POST',
data: {
format: 'json',
action: 'edit',
title: pageTitle,
text: pageContent,
summary: editSummary,
token: mw.user.tokens.get('csrfToken')
},
success: function() {
console.log('Dummy edit successful');
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Error performing dummy edit:', textStatus, errorThrown);
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Error retrieving page content:', textStatus, errorThrown);
}
});
var talkPageTitle = 'Talk:' + pageTitle;
$.ajax({
url: mw.util.wikiScript('api'),
type: 'POST',
data: {
format: 'json',
action: 'edit',
title: talkPageTitle,
section: 'new',
sectiontitle: 'Translation attribution',
text: `{{Translated page|${langCode}|${articleName}}}`,
token: mw.user.tokens.get('csrfToken')
},
success: function() {
console.log('Article talk page updated');
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Error updating article talk page:', textStatus, errorThrown);
}
});
notifyContributor(creator, langCode, articleName);
}
function notifyContributor(contributor, langCode, articleName) {
var subst = "subst:";
var notificationMessage = `{{${subst}uw-translation|1=${langCode}|2=${articleName}}}`;
$.ajax({
url: mw.util.wikiScript('api'),
type: 'POST',
data: {
format: 'json',
action: 'edit',
title: 'User talk:' + contributor,
section: 'new',
sectiontitle: 'Notification: Translation Attribution',
text: notificationMessage,
token: mw.user.tokens.get('csrfToken')
},
success: function() {
console.log('Notification sent to user talk page');
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Error sending notification:', textStatus, errorThrown);
if (confirm("Could not notify contributor. Retry?")) {
notifyContributor(contributor, langCode, articleName);
}
}
});
}
var button = document.createElement('button');
button.innerHTML = 'Translation Attribution';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = 1000;
button.style.backgroundColor = '#f8f9fa';
button.style.border = '1px solid #a2a9b1';
button.style.padding = '5px 10px';
button.style.cursor = 'pointer';
document.body.appendChild(button);
button.addEventListener('click', createInputGUI);
})();