MediaWiki:Common.js

From greek.doctor
Revision as of 18:06, 22 December 2024 by Nikolas (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

var currentPageTitle = $("h1").text();
var currentPageNumber = parseInt(currentPageTitle);
var currentPageID = [];
var parentCategory = [];
var parentCategorySubpages = [];
var currentPageIndexOfParentCategory = [];
var nextPagePageID = [];
var nextPageURL = [];

/* 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;
        }
});
console.log("currentPageID is " + currentPageID);

/* 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;
        }
});

console.log("currentPageNumber is " + currentPageNumber);
console.log("parentCategory is " + parentCategory);
console.log("parentCategorySubpages is " + parentCategorySubpages);

var currentPageIndexOfParentCategory = parentCategorySubpages.findIndex(function(page3) {
    return page3.pageid == currentPageID;
});
console.log("currentPageIndexOfParentCategory is " + currentPageIndexOfParentCategory);

nextPagePageID = parentCategorySubpages[currentPageIndexOfParentCategory + 1].pageid;
console.log("nextPagePageID is " + nextPagePageID);

/* 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;
        }
});
console.log("nextPageURL is " + nextPageURL);
$(".catlinks").prepend($('<a href="'+nextPageURL+'">'+'Next Page'+'</a>'));