Extension:TopicTags/JS-Common.js
Appearance
// Get Tag name from query-string
var urlParams = new URLSearchParams(window.location.search);
var sTag = (urlParams.get('Tag'));
// Get Description
// http://scraping.pro/web-scraping-with-javascript-load-html-page/
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", "/index.php/Template:TagDescription?action=render&Tag=" + sTag, false);
xmlhttp.send();
var sDescription = xmlhttp.responseText;
// sDescription = $(sDescription).text(); // strip HTML, else it gets into storage page
// NOT WORKING, WOULD BE PREFERABLE
/*
var apix = new mediaWiki.Api();
var mwTitle = new mediaWiki.Title( 'Portal:TagDescriptions', 3004); // this would have to be changed to determine the correct namespace number
// before calling mediawiki.Title(), since Portal is a custom namespace
// and many wikis may not have it as namespace number 3004
alert (mwTitle.getName());
apix.parse(mwTitle,
{section:1}
)
.done(function(result,jqXHR)
{alert("done");}
)
*/
// NOT WORKING, BUT PREFERABLE CUZ SIMPLER
// https://www.tutorialspoint.com/jquery/ajax-jquery-get.htm
/*
$.get("/index.php/Template:TagDescription?action=render&Tag=CommonGround",function(html){
var sDescription = $(html);
});
*/
// Draw Form
var sExtraHTML = '<div contenteditable="true" style="width:95%; min-height:20px; border:1px solid; border-radius:20px; outline-offset:-10px; padding:10px">';
$('#txtBox').html(sExtraHTML + sDescription + '</div>');
$('#btnSave').html('<button>Save</button>');
// Handle events
// https://www.mediawiki.org/wiki/API:Edit#Parameters
// https://www.mediawiki.org/w/index.php?title=Topic:S8dvml33u43d2joy&topic_showPostId=s8dvmk8eivd9su01#flow-post-s8dvmk8eivd9su01
$(document).on('click', '#btnSave', function () {
// get new description
var sNewDescription = $('#txtBox').html();
// remove extra HTML before comparing
sNewDescription = sNewDescription.substring(sExtraHTML.length, sNewDescription.length-6);
// if no change, exit
if (String(sNewDescription) == String(sDescription)) {
return;}
// if get here, we have new description
// if tag-section not found, add new section to Descriptions page
// Get Section ID, to confirm existence
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", "/index.php/Template:SectionIDUrl?action=render&Page=Portal:TagDescriptions&Section=" + sTag, false);
xmlhttp.send();
var sSectionID = xmlhttp.responseText;
sSectionID = $(sSectionID).text();
var iSectionID = parseInt(sSectionID);
var bExists = (iSectionID>0);
if (!bExists) {
// create new section.
var api = new mediaWiki.Api();
api.postWithToken( "edit", {
action: "edit",
title: "Portal:TagDescriptions",
section: "new",
summary: "",
text: sNewDescription,
sectiontitle: sTag
} ).done( function( result, jqXHR ) {
alert( "Saved successfully" );
// remember new description
sDescription = sNewDescription;
} ).fail( function( code, result ) {
if ( code === "http" ) {
alert( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object
} else if ( code === "ok-but-empty" ) {
alert( "Got an empty response from the server" );
} else {
alert( "API error: " + code );
}
})
}
else { // edit existing section
var api = new mediaWiki.Api();
api.postWithToken( "edit",
{
action: "edit",
title: "Portal:TagDescriptions",
section: iSectionID,
text: "== " + sTag + " == \n" + sNewDescription
}
)
.done(function( result, jqXHR )
{
alert( "Saved successfully" );
// remember new description
sDescription = sNewDescription;
}
)
.fail( function( code, result )
{
if ( code === "http" )
{
alert( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object
}
else if ( code === "ok-but-empty" )
{
alert( "Got an empty response from the server" );
}
else
{
alert( "API error: " + code );
}
}
)
}
});