MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
$("#start-edit-test").click(function(){ | $("#start-edit-test").click(function(){ | ||
extractPageInfo(" | extractPageInfo("Smart Chest"); | ||
}); | }); | ||
function extractPageInfo( | function extractPageInfo(pageTitle) { | ||
$.ajax({ | $.ajax({ | ||
url: 'https://wiki.factorio.com/api.php', | url: 'https://wiki.factorio.com/api.php', | ||
Line 17: | Line 17: | ||
format: 'json', | format: 'json', | ||
action: 'query', | action: 'query', | ||
titles: | titles: pageTitle, | ||
prop: 'revisions', | prop: 'revisions', | ||
rvprop: 'content' | rvprop: 'content' | ||
Line 45: | Line 45: | ||
function createNewInfoboxPage(page, contentOfMainPage) { | function createNewInfoboxPage(page, contentOfMainPage) { | ||
var infoboxText = getInfoboxFromFullPageContent(contentOfMainPage); | |||
console.log(infoboxText); | |||
var newPageTitle = page + "/infobox"; | |||
var convertedInfoboxText = convertInfobox(infoboxTest); | |||
console.log(convertedInfoboxText); | |||
createPage(newPageTitle, convertedInfoboxText); | |||
} | } | ||
function | var getInfoboxFromFullPageContent(contentOfPage) { | ||
return contentOfPage.match(/{{\bMachinery\b(\s+\|.+)+\s}}/g); | |||
} | |||
function convertInfobox(text) { | |||
text = text.replace(/{{(\w+)/g, function (piece, $1) { | |||
var returnText = "{{Infobox\n| category = "; | |||
$1 = $1.toLowerCase(); | |||
switch ($1) { | |||
case "item": | |||
returnText = returnText.concat("Items"); | |||
break; | |||
case "machinery": | |||
returnText = returnText.concat("Machinery"); | |||
break; | |||
case "combat": | |||
returnText = returnText.concat("Combat"); | |||
break; | |||
case "technology": | |||
returnText = returnText.concat("Technology"); | |||
} | |||
switch ($1) { | |||
case "machinery": | |||
returnText = returnText.concat("\n| category-name = Machine"); | |||
break; | |||
case "item": | |||
returnText = returnText.concat("\n| category-name = Item"); | |||
break; | |||
} | |||
return returnText; | |||
}); | |||
text = text.replace("stack_size", "stack-size"); | |||
text = text.replace("poweroutput", "power-output"); | |||
text = text.replace("input", "recipe"); | |||
text = text.replace("raw", "total-raw"); | |||
text = text.replace("technologies", "required-technologies"); | |||
text = text.replace("costmultiplier", "cost-multiplier"); | |||
text = text.replace("requirements", "required-technologies"); | |||
text = text.replace("walkingspeed", "walking-speed"); | |||
text = text.replace("storagesize", "storage-size"); | |||
text = text.replace("gridsize", "grid-size"); | |||
text = text.replace("shootingspeed", "shooting-speed"); | |||
text = text.replace("damagebonus", "damage-bonus"); | |||
text = text.replace("clustersize", "cluster-size"); | |||
text = text.replace("aoesize", "area-of-effect-size"); | |||
text = text.replace("magazinesize", "magazine-size"); | |||
text = text.replace("recharge", "robot-recharge-rate"); | |||
text = text.replace("rechargebuffer", "internal-buffer-recharge-rate"); | |||
text = text.replace("wirereach", "wire-reach"); | |||
text = text.replace("craftingspeed", "crafting-speed"); | |||
text = text.replace("smeltingspeed", "smelting-speed"); | |||
text = text.replace("miningpower", "mining-power"); | |||
text = text.replace("miningspeed", "mining-speed"); | |||
text = text.replace("miningarea", "mining-area"); | |||
text = text.replace("supplyarea", "supply-area"); | |||
text = text.replace("constructionarea", "construction-area"); | |||
text = text.replace("lifetime", "lifespan"); | |||
text = text.replace("inventorysizebonus", "inventory-size-bonus"); | |||
text = text.replace("gridsize", "grid-size"); | |||
text = text.replace("boosttechs", "boosting-technologies"); | |||
text = text.replace("storage", "storage-size"); | |||
text = text.replace(/\|\s*\brecipe\b\s*=\s*(.+)\n\|\s*\boutput\b\s*=\s*(.+)/g, function (piece, $1, $2) { | |||
return "| recipe = " + $1 + " = " + $2; | |||
}); | |||
text = text.concat("<noinclude>[[Category:Infobox page]]</noinclude>"); | |||
return text; | |||
} | |||
function createPage(pageTitle, content) { | |||
$.ajax({ | $.ajax({ | ||
url: 'https://wiki.factorio.com/api.php', | url: 'https://wiki.factorio.com/api.php', | ||
data: { | data: { | ||
format: 'json', | format: 'json', | ||
action: ' | action: 'edit', | ||
titles: | text: content, | ||
token: | titles: pageTitle, | ||
bot: true, | |||
token: 159d15f2706c7140ac229e91474d142e57ed8df4+\\ | |||
}, | }, | ||
dataType: 'json', | dataType: 'json', | ||
type: 'GET', | type: 'GET', | ||
success: function( data ) { | success: function( data ) { | ||
if ( data && data.edit && data.edit.result == 'Success' ) { | if ( data && data.edit && data.edit.result == 'Success' ) { | ||
window.location.reload(); // reload page if edit was successful | window.location.reload(); // reload page if edit was successful |
Revision as of 23:13, 29 September 2016
/* Any JavaScript here will be loaded for all users on every page load. */
/*$(".navbox").css("background", "red");*/
//Spoiler template JavaScript
$(".spoiler-container .button").click(function() {
$(this).siblings(".text").toggle("slow");
});
$("#start-edit-test").click(function(){
extractPageInfo("Smart Chest");
});
function extractPageInfo(pageTitle) {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: pageTitle,
prop: 'revisions',
rvprop: 'content'
},
dataType: 'json',
type: 'GET',
success: function( data ) {
console.log(data);
var pages = data.query.pages;
var revisions = pages[Object.keys(pages)[0]].revisions[0];
var content = revisions[Object.keys(revisions)[2]]
var title = pages[Object.keys(pages)[0]].title;
createNewInfoboxPage(title, content);
if ( data && data.query && data.query.result == 'Success' ) {
window.location.reload(); // reload page if edit was successful
} else if ( data && data.error ) {
alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
} else {
alert( 'Error: Unknown result from API.' );
}
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
}
function createNewInfoboxPage(page, contentOfMainPage) {
var infoboxText = getInfoboxFromFullPageContent(contentOfMainPage);
console.log(infoboxText);
var newPageTitle = page + "/infobox";
var convertedInfoboxText = convertInfobox(infoboxTest);
console.log(convertedInfoboxText);
createPage(newPageTitle, convertedInfoboxText);
}
var getInfoboxFromFullPageContent(contentOfPage) {
return contentOfPage.match(/{{\bMachinery\b(\s+\|.+)+\s}}/g);
}
function convertInfobox(text) {
text = text.replace(/{{(\w+)/g, function (piece, $1) {
var returnText = "{{Infobox\n| category = ";
$1 = $1.toLowerCase();
switch ($1) {
case "item":
returnText = returnText.concat("Items");
break;
case "machinery":
returnText = returnText.concat("Machinery");
break;
case "combat":
returnText = returnText.concat("Combat");
break;
case "technology":
returnText = returnText.concat("Technology");
}
switch ($1) {
case "machinery":
returnText = returnText.concat("\n| category-name = Machine");
break;
case "item":
returnText = returnText.concat("\n| category-name = Item");
break;
}
return returnText;
});
text = text.replace("stack_size", "stack-size");
text = text.replace("poweroutput", "power-output");
text = text.replace("input", "recipe");
text = text.replace("raw", "total-raw");
text = text.replace("technologies", "required-technologies");
text = text.replace("costmultiplier", "cost-multiplier");
text = text.replace("requirements", "required-technologies");
text = text.replace("walkingspeed", "walking-speed");
text = text.replace("storagesize", "storage-size");
text = text.replace("gridsize", "grid-size");
text = text.replace("shootingspeed", "shooting-speed");
text = text.replace("damagebonus", "damage-bonus");
text = text.replace("clustersize", "cluster-size");
text = text.replace("aoesize", "area-of-effect-size");
text = text.replace("magazinesize", "magazine-size");
text = text.replace("recharge", "robot-recharge-rate");
text = text.replace("rechargebuffer", "internal-buffer-recharge-rate");
text = text.replace("wirereach", "wire-reach");
text = text.replace("craftingspeed", "crafting-speed");
text = text.replace("smeltingspeed", "smelting-speed");
text = text.replace("miningpower", "mining-power");
text = text.replace("miningspeed", "mining-speed");
text = text.replace("miningarea", "mining-area");
text = text.replace("supplyarea", "supply-area");
text = text.replace("constructionarea", "construction-area");
text = text.replace("lifetime", "lifespan");
text = text.replace("inventorysizebonus", "inventory-size-bonus");
text = text.replace("gridsize", "grid-size");
text = text.replace("boosttechs", "boosting-technologies");
text = text.replace("storage", "storage-size");
text = text.replace(/\|\s*\brecipe\b\s*=\s*(.+)\n\|\s*\boutput\b\s*=\s*(.+)/g, function (piece, $1, $2) {
return "| recipe = " + $1 + " = " + $2;
});
text = text.concat("<noinclude>[[Category:Infobox page]]</noinclude>");
return text;
}
function createPage(pageTitle, content) {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'edit',
text: content,
titles: pageTitle,
bot: true,
token: 159d15f2706c7140ac229e91474d142e57ed8df4+\\
},
dataType: 'json',
type: 'GET',
success: function( data ) {
if ( data && data.edit && data.edit.result == 'Success' ) {
window.location.reload(); // reload page if edit was successful
} else if ( data && data.error ) {
alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
} else {
alert( 'Error: Unknown result from API.' );
}
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
}