{"size": 1072, "ext": "js", "max_stars_repo_path": "src/js/modules/initialize.js", "max_stars_repo_name": "tee-talog/youtube-chat-highlighter", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/js/modules/initialize.js", "max_issues_repo_name": "tee-talog/youtube-chat-highlighter", "max_issues_repo_issues_event_min_datetime": "2020-07-16T17:39:03.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T03:41:53.000Z", "max_forks_repo_path": "src/js/modules/initialize.js", "max_forks_repo_name": "tee-talog/youtube-chat-highlighter", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 25.5238095238, "max_line_length": 75, "alphanum_fraction": 0.7192164179}
import paint from "./paint"
import state from "../state"
import paintAllChats from "./paintAllChats"
import addConfigChangeListener from "./configListener"
import fetchConfigColors from "./fetchConfigColors"

// ページアクセス時：チャット欄がロードされるまで待って、監視を開始する
const initialize = (configKeys, isUsername) => {
	const initLoadHandler = setInterval(() => {
		const chatList = state.chatListFunc()
		if (!chatList) {
			return
		}

		// チャット背景初期値
		state.initialBackgroundColor = getComputedStyle(chatList).backgroundColor

		// state 更新
		fetchConfigColors(configKeys).then(() => {
			// チャット欄監視開始
			const observer = new MutationObserver((records) => {
				// 更新されたチャットに対して色付け
				const chats = records.flatMap((e) => [...e.addedNodes])
				paint(chats, isUsername)
			})
			observer.observe(chatList, { childList: true })

			// 設定変更時にチャット欄をすべて再読み込みする
			addConfigChangeListener(configKeys, () => paintAllChats(isUsername))

			// アクセス時に色付け
			paintAllChats(isUsername)
		})

		// ロード待ちを解除
		clearInterval(initLoadHandler)
		state.loadedChatList()
	}, 500)
}

export default initialize
