|
const SESSION_STORAGE_KEY = 'gist.github.com/sounisi5011/0cdf426d24128176d0acbbeda1b77a2e' |
|
const inputElem = document.getElementById('input') |
|
const outputElem = document.getElementById('output') |
|
|
|
inputElem.addEventListener('input', inputListener) |
|
init() |
|
|
|
// ----- ----- ----- ----- ----- // |
|
|
|
function last (array) { |
|
return array[array.length - 1] |
|
} |
|
|
|
function sec2time (sec) { |
|
if (!Number.isFinite(sec)) return String(sec) |
|
|
|
const sign = sec < 0 ? '-' : '' |
|
const absSec = Math.abs(sec) |
|
const s = absSec % 60 |
|
const m = Math.trunc(absSec / 60) % 60 |
|
const h = Math.trunc(absSec / 60 ** 2) |
|
return `${sign}${h}:${String(m).padStart(2, 0)}:${String(s).padStart(2, 0)}` |
|
} |
|
|
|
function init () { |
|
if (inputElem.value === '') { |
|
const savedData = sessionStorage.getItem(SESSION_STORAGE_KEY) |
|
if (typeof savedData === 'string') { |
|
inputElem.value = savedData |
|
} |
|
} |
|
inputListener() |
|
} |
|
|
|
function inputListener () { |
|
const inputText = inputElem.value |
|
const chapterList = inputText |
|
.split(/(?:\r\n?|\n)+/) |
|
.map(line => { |
|
const match = /^\s*(?:(\d+):)?(\d+):(\d+)\s+(.+)$/s.exec(line) |
|
if (!match) return null |
|
|
|
const [, hour = '0', min, sec, title] = match |
|
const time = (Number(hour) * 60 + Number(min)) * 60 + Number(sec) |
|
return { time, title } |
|
}) |
|
.filter(Boolean) |
|
.reduce( |
|
(chapterList, chapterItem) => |
|
(chapterList.length < 1 && chapterItem.time === 0) || |
|
(chapterList.length >= 1 && last(chapterList).time < chapterItem.time) |
|
? [...chapterList, chapterItem] |
|
: chapterList, |
|
[] |
|
) |
|
|
|
const outputHTML = chapterList |
|
.map(({ time, title }, index) => { |
|
const nextTime = chapterList[index + 1]?.time ?? Infinity |
|
const diffTime = nextTime - time |
|
|
|
return [ |
|
`<li class="${index >= 100 ? 'warn' : ''}">`, |
|
`<span class=time>${sec2time(time)}</span>`, |
|
`<span class=${diffTime < 10 ? '"diff warn"' : 'diff'}>${sec2time(diffTime)}</span>`, |
|
`<span class=title>${html.escape(title)}</span>`, |
|
'</li>' |
|
].join('') |
|
}) |
|
.join('') |
|
outputElem.innerHTML = outputHTML |
|
|
|
try { |
|
sessionStorage.setItem(SESSION_STORAGE_KEY, inputText) |
|
} finally {} |
|
} |