Skip to content

Commit

Permalink
fix: 保存ボタンが表示されない事があったのを修正 (#260)
Browse files Browse the repository at this point in the history
window.onloadではたまにDOMが完全に生成される前に発火してしまう事があった(SPAだから?)
今までと同じようにMutationObserverを使う事で対象のElementが存在するか確認するようにする
  • Loading branch information
mnao305 authored Jul 5, 2022
1 parent 17d9970 commit f61bcd7
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,43 @@ const elementIdTocontentId = (id: string) => {
}

const main = () => {
/**
* ページ読み込み完了後に発火する初期化処理
*/
window.onload = () => {
// 投稿ページ全体一括保存ボタン作成
const postBtnEl = document.getElementsByClassName('post-btns')
if (postBtnEl.length > 0) {
injectPageAllContentsDlBtn((postBtnEl[0] as HTMLElement))
}
const target = document.getElementById('page')
if (!target) return
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
// 対象のElementが見つかるまでループ
if ((mutation.target as HTMLElement).className === 'content-block type-photo-gallery ng-scope') {
// 投稿ページ全体一括保存ボタン作成
const postBtnEl = document.getElementsByClassName('post-btns')
if (postBtnEl.length > 0) {
injectPageAllContentsDlBtn((postBtnEl[0] as HTMLElement))
}

// ギャラリーの一括保存ボタン作成
const galleryElements = document.getElementsByClassName('content-block type-photo-gallery ng-scope')
for (let i = 0; i < galleryElements.length; i++) {
const element = galleryElements[i] as HTMLElement
const elId = element.closest('.post-content-inner')?.id
if (elId) {
const contentId = elementIdTocontentId(elId)
injectGalleryAllDlBtn(element, contentId)
}
}

// ギャラリーの一括保存ボタン作成
const galleryElements = document.getElementsByClassName('content-block type-photo-gallery ng-scope')
for (let i = 0; i < galleryElements.length; i++) {
const element = galleryElements[i] as HTMLElement
const elId = element.closest('.post-content-inner')?.id
if (elId) {
const contentId = elementIdTocontentId(elId)
injectGalleryAllDlBtn(element, contentId)
// 対象Elementが見つかりボタンを作成し終わったら終了
observer.disconnect()
break
}
}
})

const config = {
characterData: false,
childList: true,
subtree: true
}

observer.observe(target, config)
}
main()

Expand Down

0 comments on commit f61bcd7

Please sign in to comment.