From 98d8672c55210b8dee9bb7882220674a551b27ef Mon Sep 17 00:00:00 2001 From: Jayden Chiu <57676066+Jayden-Chiu@users.noreply.github.com> Date: Tue, 3 Oct 2023 04:54:49 -0400 Subject: [PATCH] add -random option to xkcd parser (#238) --- Parsers/Find a relevant XKCD comic.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Parsers/Find a relevant XKCD comic.js b/Parsers/Find a relevant XKCD comic.js index e136f8f..a36cc56 100644 --- a/Parsers/Find a relevant XKCD comic.js +++ b/Parsers/Find a relevant XKCD comic.js @@ -37,11 +37,18 @@ function buildComicOutput(xkcdPayload) { return block; } -var search = gs.urlEncode(current.text.replace(/!xkcd/g, '').trim()); +var terms = current.text.replace(/!xkcd/g, '').trim(); +var search = gs.urlEncode(terms); +var endpoint; +if (terms === '-random' || terms === '') { + endpoint = 'https://www.explainxkcd.com/wiki/index.php/Special:Random'; +} else { + endpoint = 'https://www.explainxkcd.com/wiki/index.php?&title=Special%3ASearch&go=Go&fulltext=1&search=' + search; +} var rm = new sn_ws.RESTMessageV2(); rm.setHttpMethod('GET'); -rm.setEndpoint('https://www.explainxkcd.com/wiki/index.php?&title=Special%3ASearch&go=Go&fulltext=1&search=' + search); +rm.setEndpoint(endpoint); rm.setRequestHeader('User-Agent', 'servicenow'); var response = rm.execute(); var body = response.getBody(); @@ -51,7 +58,7 @@ var msg; // Check if we got an empty search result, as the result regex will match the page even if no result var checkResponse = body.match(/

/g); if (checkResponse === null){ - var result = body.match(/(?: