From a4328eae24fee0da1688704df02a6d102d1a130b Mon Sep 17 00:00:00 2001 From: KoloMl Date: Tue, 9 Apr 2024 23:24:15 +0400 Subject: [PATCH] Re-fetching the detail image page to catch new tags and aliases list --- src/lib/booru/scraped/ScrapedAPI.js | 17 +++++++++++------ src/lib/booru/scraped/parsing/PostParser.js | 11 +++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/lib/booru/scraped/ScrapedAPI.js b/src/lib/booru/scraped/ScrapedAPI.js index b77ca4a..919c3ca 100644 --- a/src/lib/booru/scraped/ScrapedAPI.js +++ b/src/lib/booru/scraped/ScrapedAPI.js @@ -8,8 +8,8 @@ export default class ScrapedAPI { * @return {Promise|null>} Updated tags and aliases list for updating internal cached state. */ async updateImageTags(imageId, callback) { - const formData = await new PostParser(imageId) - .resolveTagEditorFormData(); + const postParser = new PostParser(imageId); + const formData = await postParser.resolveTagEditorFormData(); const tagsList = new Set( formData @@ -29,13 +29,18 @@ export default class ScrapedAPI { Array.from(updateTagsList).join(', ') ); - const tagsSubmittedResponse = await fetch(`/images/${imageId}/tags`, { + await fetch(`/images/${imageId}/tags`, { method: 'POST', body: formData, }); - return PostParser.resolveTagsAndAliasesFromPost( - await PostParser.resolveFragmentFromResponse(tagsSubmittedResponse) - ); + // We need to remove stored version of the document to request an updated version. + postParser.clear(); + + // Additional request to re-fetch the new list of tags and aliases. I couldn't find the way to request this list + // using official API. + // TODO Maybe it will be better to resolve aliases on the extension side somehow, maybe by requesting and caching + // aliases in storage. + return await postParser.resolveTagsAndAliases(); } } diff --git a/src/lib/booru/scraped/parsing/PostParser.js b/src/lib/booru/scraped/parsing/PostParser.js index 33bfdbc..dcef003 100644 --- a/src/lib/booru/scraped/parsing/PostParser.js +++ b/src/lib/booru/scraped/parsing/PostParser.js @@ -35,6 +35,17 @@ export default class PostParser extends PageParser { ); } + /** + * Resolve the tags and aliases mapping from the post page. + * + * @return {Promise|null>} + */ + async resolveTagsAndAliases() { + return PostParser.resolveTagsAndAliasesFromPost( + await this.resolveFragment() + ); + } + /** * Resolve the list of tags and aliases from the post content. *