Skip to content

Commit

Permalink
feat: リード文に埋め込まれた画像をダウンロードできるようにする (#464)
Browse files Browse the repository at this point in the history
* feat: リード文に埋め込まれた画像をダウンロードできるようにする

#432

* style: fix lint
  • Loading branch information
mnao305 authored Oct 15, 2023
1 parent ac3a0a3 commit 2f49011
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/content/modules/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ 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<void> => {
const sendData = {
msg: 'download',
url: url,
// ガチャコン絵文字が入るとエラーになるので分割します
filepath: filepath.replaceAll('\u200d', ''),
filename: filename
}
browser.runtime.sendMessage(sendData)
return browser.runtime.sendMessage(sendData)
}
26 changes: 24 additions & 2 deletions src/content/modules/postPage.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -27,11 +27,33 @@ export const downloadEverythingFromPost = async (): Promise<void> => {

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) {
// 表示できるもの以外はスキップします
Expand Down
15 changes: 12 additions & 3 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ export interface PostData {
'title': string
'comment': string | null
'rating': string
'thumb'?: {
'thumb': {
thumb: string
medium: string
large: string
main: string
ogp: string
micro: string
original: string
}
} | null
'thumb_micro': string
'show_adult_thumb': boolean
'posted_at': string
Expand All @@ -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
Expand All @@ -153,3 +153,12 @@ export interface ImgContents {
name: string;
content: Blob;
}

export interface BlogComment {
ops: {
insert?: string | {
image?: string
}
attributes?: {}
}[]
}

0 comments on commit 2f49011

Please sign in to comment.