You should change your url to also inclide categories
in your prop
, and then set the cllimit
to the maximum of 500:
tempscript.src = "http://en.wikipedia.org/w/api.php" + "?action=query&generator=random&prop=categories|extracts&cllimit=500&exchars=" + maxchars + "&format=json&callback=onComplete&requestid=" + Math.floor(Math.random() * 999999).toString();
Then, if the page has categories, it will list them in the returned JSON object.
In your callback function, you will then need the following:
var badArticles = ['Poop', 'Pee', 'Underpants'],
badCategories = ['Images of poop', 'Images of pee', 'Images of underpants'],
page = response.query.pages;
for (var i in page) {
page = page[i]; // `i` will be the pageid in this loop
break; // you don't want the loop to continue within the new `page` object
}
//exit callback when pagename is in bad articles list
if (badArticles.indexOf(page.title) !== -1) return false;
if (page.categories) {
for (var i=0;i<page.categories.length;i++) {
//exit callback when pagename has a category in bad categories list
if (badCategories.indexOf(page.categories[i].title)) return false;
}
}
That should work. I did not personally test it, but I would expect that to work, based on the format of the MediaWiki API's response. If that doesn't work, please leave a comment.