MediaWiki:Common.js: Difference between revisions

From greek.doctor
No edit summary
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 2: Line 2:


var currentPageTitle = $("h1").text();
var currentPageTitle = $("h1").text();
var currentPageNumber = parseInt(currentPageTitle);
var currentPageID = [];
var currentPageID = [];
var parentCategory = [];
var parentCategory = [];
Line 9: Line 8:
var nextPagePageID = [];
var nextPagePageID = [];
var nextPageURL = [];
var nextPageURL = [];
var nextPageTitle = [];
var previousPagePageID = [];
var previousPageURL = [];
var previousPageTitle = []


/* TODO:
/* TODO:
Line 15: Line 18:
- Get the URL (nextPageURL) of nextPagePageID
- Get the URL (nextPageURL) of nextPagePageID
- Create a hyperlink to nextPageURL at the bottom of the page
- Create a hyperlink to nextPageURL at the bottom of the page
*/
*/


Line 35: Line 37:
         }
         }
});
});
console.log("currentPageID is " + currentPageID);


/* Receives a variable "parentCategory" from JSON */
/* Receives a variable "parentCategory" from JSON */
Line 73: Line 74:
         }
         }
});
});
console.log("currentPageNumber is " + currentPageNumber);
console.log("parentCategory is " + parentCategory);
console.log("parentCategorySubpages is " + parentCategorySubpages);


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


nextPagePageID = parentCategorySubpages[currentPageIndexOfParentCategory + 1].pageid;
try {
console.log("nextPagePageID is " + nextPagePageID);
    nextPagePageID = parentCategorySubpages[currentPageIndexOfParentCategory + 1].pageid;
}
 
catch(err) {
    console.log("Impossible to find the next page. Probably because it's the last page in the category. ")
    var lastPageInCategory = 1;
}
 
/* Gets the url and page title of the next page in the category, but only if it's not the last page in the category */


/* Gets a JSON containing the url of the next page in the category */
if (lastPageInCategory != 1) {
$.ajax({
  $.ajax({
         url: mw.util.wikiScript( 'api' ),
         url: mw.util.wikiScript( 'api' ),
         data:
         data:
Line 103: Line 107:
         success: function (json) {
         success: function (json) {
             nextPageURL = json.query.pages[0].fullurl;
             nextPageURL = json.query.pages[0].fullurl;
            nextPageTitle = json.query.pages[0].title;
         }
         }
});
  });
console.log("nextPageURL is " + nextPageURL);
}
$(".catlinks").prepend($('<a href="'+nextPageURL+'">'+'Next Page'+'</a>'));
 
try {
    previousPagePageID = parentCategorySubpages[currentPageIndexOfParentCategory - 1].pageid;
}
 
catch(err) {
    console.log("Impossible to find the previous page. Probably because it's the first page in the category. ")
    var firstPageInCategory = 1;
}
 
/* Gets the url and page title of the previous page in the category */
 
if (firstPageInCategory != 1) {
    $.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;
        }
  });
}
 
/* If it's not the last or first page in the category, add both links */
if ((lastPageInCategory != 1) && (firstPageInCategory != 1)) {
 
  /* Adds the links to the desktop site (vector theme) */
  $(".vector-body").append( ' <br />' );
  $(".vector-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>'));
 
  /* Adds the links to the mobile site (nuerva something theme) */
  $(".content").append( ' <br />' );
  $(".content").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>'));
 
} else if (lastPageInCategory = 1) {
  /* True if this is the last page in the category. Don't evaluate the "Next Page" */ 
  /* Adds the links to the desktop site (vector theme) */
  $(".vector-body").append( ' <br />' );
  $(".vector-body").append($('<p style="text-align: left"><a href="'+previousPageURL+'">'+'Previous page: '+previousPageTitle+'</a></p>'));
 
  /* Adds the links to the mobile site (nuerva something theme) */
  $(".content").append( ' <br />' );
  $(".content").append($('<p style="text-align: left"><a href="'+previousPageURL+'">'+'Previous page: '+previousPageTitle+'</a></p>'));
 
} else if (firstPageInCategory = 1) {
  /* True if this is the first page in the category. Don't evaluate the "Previous Page" */ 
  /* Adds the links to the desktop site (vector theme) */
  $(".vector-body").append( ' <br />' );
  $(".vector-body").append($('<p style="text-align: right"><a href="'+nextPageURL+'">'+'Next page: '+nextPageTitle+'</a></p>'));
 
  /* Adds the links to the mobile site (nuerva something theme) */
  $(".content").append( ' <br />' );
  $(".content").append($('<p style="text-align: right"><a href="'+nextPageURL+'">'+'Next page: '+nextPageTitle+'</a></p>'));
}

Latest revision as of 21:48, 22 December 2024

/* Any JavaScript here will be loaded for all users on every page load. */

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

try {
    nextPagePageID = parentCategorySubpages[currentPageIndexOfParentCategory + 1].pageid;
}

catch(err) {
    console.log("Impossible to find the next page. Probably because it's the last page in the category. ")
    var lastPageInCategory = 1;
}

/* Gets the url and page title of the next page in the category, but only if it's not the last page in the category */

if (lastPageInCategory != 1) {
   $.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;
        }
   });
}

try {
    previousPagePageID = parentCategorySubpages[currentPageIndexOfParentCategory - 1].pageid;
}

catch(err) {
    console.log("Impossible to find the previous page. Probably because it's the first page in the category. ")
    var firstPageInCategory = 1;
}

/* Gets the url and page title of the previous page in the category */

if (firstPageInCategory != 1) {
    $.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;
        }
  });
}

/* If it's not the last or first page in the category, add both links */
if ((lastPageInCategory != 1) && (firstPageInCategory != 1)) {

  /* Adds the links to the desktop site (vector theme) */
  $(".vector-body").append( ' <br />' );
  $(".vector-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>'));

  /* Adds the links to the mobile site (nuerva something theme) */
  $(".content").append( ' <br />' );
  $(".content").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>'));

} else if (lastPageInCategory = 1) {
  /* True if this is the last page in the category. Don't evaluate the "Next Page" */  
  /* Adds the links to the desktop site (vector theme) */
  $(".vector-body").append( ' <br />' );
  $(".vector-body").append($('<p style="text-align: left"><a href="'+previousPageURL+'">'+'Previous page: '+previousPageTitle+'</a></p>'));

  /* Adds the links to the mobile site (nuerva something theme) */
  $(".content").append( ' <br />' );
  $(".content").append($('<p style="text-align: left"><a href="'+previousPageURL+'">'+'Previous page: '+previousPageTitle+'</a></p>'));

} else if (firstPageInCategory = 1) {
  /* True if this is the first page in the category. Don't evaluate the "Previous Page" */  
  /* Adds the links to the desktop site (vector theme) */
  $(".vector-body").append( ' <br />' );
  $(".vector-body").append($('<p style="text-align: right"><a href="'+nextPageURL+'">'+'Next page: '+nextPageTitle+'</a></p>'));

  /* Adds the links to the mobile site (nuerva something theme) */
  $(".content").append( ' <br />' );
  $(".content").append($('<p style="text-align: right"><a href="'+nextPageURL+'">'+'Next page: '+nextPageTitle+'</a></p>'));
}