Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: リード文に埋め込まれた画像をダウンロードできるようにする #464

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?: {}
}[]
}