Skip to content

Commit

Permalink
feat: 全てのファイルを1つのフォルダーにダウンロードできるようにする (#497)
Browse files Browse the repository at this point in the history
* feat: 全てのファイルを1つのフォルダに保存するためのオプション設定を追加

* feat: 全てのファイルを1つのフォルダーにダウンロードできるようにする
  • Loading branch information
mnao305 authored Dec 26, 2023
1 parent 6a659a5 commit a548c74
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 9 deletions.
11 changes: 10 additions & 1 deletion src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
"message": "Download All"
},
"download_everything_from_a_post": {
"message": "download everything from a post(β)"
"message": "download everything from a post"
},
"all_file_one_folder" : {
"message": "全てのファイルを1つのフォルダに保存する"
},
"all_file_one_folder_description" : {
"message": "このオプションを有効にすると、クリエイター・記事毎にフォルダー分けがされず、1つのフォルダに全てのファイルが保存されます。"
},
"save": {
"message": "Save"
}
}
11 changes: 10 additions & 1 deletion src/_locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
"message": "一括DL"
},
"download_everything_from_a_post": {
"message": "この投稿の全てをダウンロード(β)"
"message": "この投稿の全てをダウンロード"
},
"all_file_one_folder" : {
"message": "全てのファイルを1つのフォルダに保存する"
},
"all_file_one_folder_description" : {
"message": "このオプションを有効にすると、クリエイター・記事毎にフォルダー分けがされず、1つのフォルダに全てのファイルが保存されます。"
},
"save": {
"message": "保存"
}
}
27 changes: 25 additions & 2 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { browser, Downloads } from 'webextension-polyfill-ts'
import { Settings } from '../options'

/**
* 与えられた文字列からunicode制御文字を取り除く
Expand Down Expand Up @@ -26,10 +27,32 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
.catch(err => { console.error(err) })
})

export const download = (url: string, filename: string, filepath: string): void => {
/**
* 1つのフォルダに全てのファイルを保存する場合のファイル名を生成する
*/
const generateOneFolderFileName = (filepath: string, filename: string) => {
const splitFilepath = filepath.split('/')
const fanclubName = splitFilepath[0].split('_')[1]
const titleName = splitFilepath[1].split('_')[1]
return `${fanclubName}_${titleName}_${removeControlCharacters(filename)}`
}

export const download = async (url: string, filename: string, filepath: string): Promise<void> => {
const { allFileOneFolder } = (await browser.storage.local.get({
allFileOneFolder: false
})) as Settings
// 保存先の設定
const downloadFilename = allFileOneFolder
// 1つのフォルダーに全てのファイルを保存する場合
? generateOneFolderFileName(filepath, filename)
//
: `${removeControlCharacters(filepath)}/${removeControlCharacters(
filename
)}`

const options: Downloads.DownloadOptionsType = {
url,
filename: `fantia/${removeControlCharacters(filepath)}/${removeControlCharacters(filename)}`,
filename: `fantia/${downloadFilename}`,
saveAs: false,
conflictAction: 'overwrite'
}
Expand Down
4 changes: 2 additions & 2 deletions src/content/modules/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import { browser } from 'webextension-polyfill-ts'
export const fileDownload = (url: string, filepath: string, filename: string): Promise<void> => {
const sendData = {
msg: 'download',
url: url,
url,
// ガチャコン絵文字が入るとエラーになるので分割します
filepath: filepath.replaceAll('\u200d', ''),
filename: filename
filename
}
return browser.runtime.sendMessage(sendData)
}
8 changes: 7 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
],
"persistent": false
},
"options_ui": {
"page": "options/index.html",
"chrome_style": true,
"browser_style": true
},
"permissions": [
"downloads",
"contextMenus"
"contextMenus",
"storage"
]
}
15 changes: 13 additions & 2 deletions src/options/index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Option</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="app">

<body>
<div id="setting">
<ul>
<li>
<div>
<label title="all_file_one_folder_description"><input type="checkbox" name="allFileOneFolder"><span data-locale="all_file_one_folder"></span></label>
</div>
</li>
</ul>
<button id="save">保存</button><span id="saveMsg">saved...</span>
</div>

<script src="index.js"></script>
</body>

</html>
63 changes: 63 additions & 0 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { browser } from 'webextension-polyfill-ts'

export type Settings = {
allFileOneFolder: boolean;
};

/**
* オプション画面が開かれたときに呼ばれる
*/
const setup = async () => {
// 翻訳文言を入れる
document.querySelectorAll<HTMLElement>('[data-locale]').forEach((elem) => {
elem.innerText = elem.dataset.locale
? browser.i18n.getMessage(elem.dataset.locale)
: ''
})
// title属性がある場合はそれも入れる
document.querySelectorAll<HTMLElement>('[title]').forEach((elem) => {
elem.title = elem.title ? browser.i18n.getMessage(elem.title) : ''
})

// 保存済みの設定を表示に反映
const settings = (await browser.storage.local.get({
allFileOneFolder: false
})) as Settings

for (const key in settings) {
const setting = settings[key as keyof Settings]
const inputEl = document.querySelector<HTMLInputElement>(`[name="${key}"]`)
if (!inputEl) continue

if (typeof setting === 'boolean') {
inputEl.checked = setting
}
}
}

setup()

/**
* オプション画面で設定を変更したときに呼ばれる
*/
const save = async () => {
console.log('save')
const settings: Settings = {
allFileOneFolder:
document.querySelector<HTMLInputElement>('[name="allFileOneFolder"]')
?.checked ?? false
}

await browser.storage.local.set(settings)

// 保存完了を表示
const saveMsg = document.querySelector<HTMLDivElement>('#saveMsg')
saveMsg!.style.display = 'inline-block'
setTimeout(() => {
saveMsg!.style.display = 'none'
}, 2000)
}

document
.querySelector<HTMLButtonElement>('#save')
?.addEventListener('click', save)
13 changes: 13 additions & 0 deletions src/options/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ul {
list-style: none;
padding-left: 0;
}

input {
margin-right: 6px;
}

#saveMsg {
display: none;
margin-left: 6px;
}

0 comments on commit a548c74

Please sign in to comment.