Jump to content

Wikipedia:WikiProject User scripts/Scripts/Page exists

From Wikipedia, the free encyclopedia
/*
 * Returns a boolean: whether or not the page with the specified title exists or not.
 *
 * Keywords for search: check page existence, page is missing.
 *
 * Reference documentation:
 *   - for JS class Api: https://doc.wikimedia.org/mediawiki-core/REL1_41/js/#!/api/mw.Api
 *   - for action=query requests:
 *     - https://www.mediawiki.org/wiki/API:Query
 *     - https://en.wikipedia.org/w/api.php?action=help&modules=query
 */
async function pageExists(title) {
	const api = new mw.Api();
	const response = await api.get({
		"action": "query",
		"format": "json",
		"titles": title
	});
	const missing = "missing" in Object.values(response.query.pages)[0];
	return !missing;
}