From 2f4901167d8fe39150f4b9559ca7231f7a8790ff Mon Sep 17 00:00:00 2001 From: mnao305 Date: Mon, 16 Oct 2023 01:08:37 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=AA=E3=83=BC=E3=83=89=E6=96=87?= =?UTF-8?q?=E3=81=AB=E5=9F=8B=E3=82=81=E8=BE=BC=E3=81=BE=E3=82=8C=E3=81=9F?= =?UTF-8?q?=E7=94=BB=E5=83=8F=E3=82=92=E3=83=80=E3=82=A6=E3=83=B3=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B=20(#464)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: リード文に埋め込まれた画像をダウンロードできるようにする #432 * style: fix lint --- src/content/modules/download.ts | 4 ++-- src/content/modules/postPage.ts | 26 ++++++++++++++++++++++++-- src/types/index.d.ts | 15 ++++++++++++--- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/content/modules/download.ts b/src/content/modules/download.ts index 313dd51d..0f5f7c09 100644 --- a/src/content/modules/download.ts +++ b/src/content/modules/download.ts @@ -19,7 +19,7 @@ import { browser } from 'webextension-polyfill-ts' /** * backgroundのdownloads.downloadを呼び出します。 */ -export const fileDownload = (url: string, filepath: string, filename: string): void => { +export const fileDownload = (url: string, filepath: string, filename: string): Promise => { const sendData = { msg: 'download', url: url, @@ -27,5 +27,5 @@ export const fileDownload = (url: string, filepath: string, filename: string): v filepath: filepath.replaceAll('\u200d', ''), filename: filename } - browser.runtime.sendMessage(sendData) + return browser.runtime.sendMessage(sendData) } diff --git a/src/content/modules/postPage.ts b/src/content/modules/postPage.ts index d6c2d94c..c8ec7f51 100644 --- a/src/content/modules/postPage.ts +++ b/src/content/modules/postPage.ts @@ -1,5 +1,5 @@ import { idAndTitlePath, contentIdToTitle, urlToExt } from '../index' -import { PostData, PostDataResponse } from '../../types/index' +import { BlogComment, PostData, PostDataResponse } from '../../types/index' import { getImgList } from './img' import { fileDownload } from './download' import { blogDL } from './blog' @@ -27,11 +27,33 @@ export const downloadEverythingFromPost = async (): Promise => { if (data.comment) { // リード文がある場合 - // TODO: リード文に埋め込まれた画像をダウンロードできるようにblog_commentをパースしてダウンロードできるようにする const text = `data:text/plain;charset=UTF-8,${data.comment}` fileDownload(text, baseFilepath, 'text.txt') } + // リード文に存在する画像のダウンロード + if (data.blog_comment) { + try { + const blogComment = JSON.parse(data.blog_comment) as BlogComment + const ops = blogComment.ops + const imgList = ops.flatMap(v => + typeof v.insert === 'object' && v.insert?.image ? v.insert.image : [] + ) + + // NOTE: 外部画像の可能性あり + for (let i = 0; i < imgList.length; i++) { + if (i % 10 === 0) await sleep(500) + const url = imgList[i] + const filename = `image${i}${urlToExt(url)}` + fileDownload(url, baseFilepath, filename).catch(e => { + console.error(e) + }) + } + } catch (e) { + console.error(e) + } + } + // post_contents内をループしてダウンロードしていく for (const postContent of data.post_contents) { // 表示できるもの以外はスキップします diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 2c55cf22..56cf783e 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -98,7 +98,7 @@ export interface PostData { 'title': string 'comment': string | null 'rating': string - 'thumb'?: { + 'thumb': { thumb: string medium: string large: string @@ -106,7 +106,7 @@ export interface PostData { ogp: string micro: string original: string - } + } | null 'thumb_micro': string 'show_adult_thumb': boolean 'posted_at': string @@ -130,7 +130,7 @@ export interface PostData { 'deadline': string 'publish_reserved_at': null 'comments': unknown - 'blog_comment': '' + 'blog_comment': string | null 'comments_reactions': unknown 'reactions': unknown 'reaction_types_url': string @@ -153,3 +153,12 @@ export interface ImgContents { name: string; content: Blob; } + +export interface BlogComment { + ops: { + insert?: string | { + image?: string + } + attributes?: {} + }[] +}