MediaWiki:Mobile.js: Difference between revisions

From greek.doctor
Created page with "→‎All JavaScript here will be loaded for users of the mobile site: var currentPageTitle = $("h1").text(); var currentPageID = []; var parentCategory = []; var parentCategorySubpages = []; var currentPageIndexOfParentCategory = []; var nextPagePageID = []; var nextPageURL = []; var nextPageTitle = []; var previousPagePageID = []; var previousPageURL = []; var previousPageTitle = [] /* TODO: - Find the index (currentPageCategoryIndex) of parentCategorySubpages whose p..."
 
No edit summary
Line 127: Line 127:
$(".mw-body").append( ' <br />' );
$(".mw-body").append( ' <br />' );
$(".mw-body").append($('<p style="text-align: left"><a href="'+previousPageURL+'">'+'Previous page: '+previousPageTitle+'</a><span style="float:right;"><a href="'+nextPageURL+'">'+'Next page: ' + nextPageTitle+'</a></span></p>'));
$(".mw-body").append($('<p style="text-align: left"><a href="'+previousPageURL+'">'+'Previous page: '+previousPageTitle+'</a><span style="float:right;"><a href="'+nextPageURL+'">'+'Next page: ' + nextPageTitle+'</a></span></p>'));
console.log(nextPageURL);

Revision as of 20:46, 22 December 2024

/* All JavaScript here will be loaded for users of the mobile site */

var currentPageTitle = $("h1").text();
var currentPageID = [];
var parentCategory = [];
var parentCategorySubpages = [];
var currentPageIndexOfParentCategory = [];
var nextPagePageID = [];
var nextPageURL = [];
var nextPageTitle = [];
var previousPagePageID = [];
var previousPageURL = [];
var previousPageTitle = []

/* TODO:
- Find the index (currentPageCategoryIndex) of parentCategorySubpages whose pageid corresponds to currentPageID
- Create a variable (nextPagePageID) containing the pageID of currentPageCategoryIndex + 1 (and -1)
- Get the URL (nextPageURL) of nextPagePageID
- Create a hyperlink to nextPageURL at the bottom of the page
*/

/* Receives the pageID of the current page */
$.ajax({
        url: mw.util.wikiScript( 'api' ),
        data:
            {
                'action': 'query',
                'format': 'json',
                'prop': 'pageprops',
                'titles': currentPageTitle,
                 'formatversion': '2'
            },
        async: false,
        datatype: 'json',
        success: function (json) {
            currentPageID = json.query.pages[0].pageid;
        }
});

/* Receives a variable "parentCategory" from JSON */
$.ajax({
        url: mw.util.wikiScript( 'api' ),
        data:
            {
                'action': 'query',
                'format': 'json',
                'prop': 'categories',
                'titles': currentPageTitle,
                 'formatversion': '2'
            },
        async: false,
        datatype: 'json',
        success: function (json) {
            parentCategory = json.query.pages[0].categories[0].title;
        }
});

/* Gets a JSON containing the subpages of the category of the current page */
$.ajax({
        url: mw.util.wikiScript( 'api' ),
        data:
            {
                'action': 'query',
                'format': 'json',
                "list": "categorymembers",
                "cmtitle": parentCategory,
                'formatversion': '2',
                "cmlimit": "max"
            },
        async: false,
        datatype: 'json',
        success: function (json) {
            parentCategorySubpages = json.query.categorymembers;
        }
});

var currentPageIndexOfParentCategory = parentCategorySubpages.findIndex(function(page3) {
    return page3.pageid == currentPageID;
});

nextPagePageID = parentCategorySubpages[currentPageIndexOfParentCategory + 1].pageid;

/* Gets a JSON containing the url of the next page in the category */
$.ajax({
        url: mw.util.wikiScript( 'api' ),
        data:
            {
                'action': 'query',
                'format': 'json',
                "prop": "info",
                "iwurl": 1,
                "pageids": nextPagePageID,
                'formatversion': '2',
                "inprop": "url"
            },
        async: false,
        datatype: 'json',
        success: function (json) {
            nextPageURL = json.query.pages[0].fullurl;
            nextPageTitle = json.query.pages[0].title;
        }
});

previousPagePageID = parentCategorySubpages[currentPageIndexOfParentCategory - 1].pageid;

/* Gets a JSON containing the url of the previous page in the category */
$.ajax({
        url: mw.util.wikiScript( 'api' ),
        data:
            {
                'action': 'query',
                'format': 'json',
                "prop": "info",
                "iwurl": 1,
                "pageids": previousPagePageID,
                'formatversion': '2',
                "inprop": "url"
            },
        async: false,
        datatype: 'json',
        success: function (json) {
            previousPageURL = json.query.pages[0].fullurl;
            previousPageTitle = json.query.pages[0].title;
        }
});

$(".mw-body").append( ' <br />' );
$(".mw-body").append($('<p style="text-align: left"><a href="'+previousPageURL+'">'+'Previous page: '+previousPageTitle+'</a><span style="float:right;"><a href="'+nextPageURL+'">'+'Next page: ' + nextPageTitle+'</a></span></p>'));

console.log(nextPageURL);