抽空优化了一下,你再试试
[JavaScript] 纯文本查看 复制代码 const days = 2
const today = new Date(new Date().setHours(0, 0, 0, 0))
const oneDay = 86400 * 1000
const daysMap = {
"今": today,
"昨": today - oneDay,
}
let list = []
const activationList = document.querySelectorAll('.activation-list .activation')
async function getList() {
const activationList = setNewList(document.querySelectorAll('.activation-list .activation'))
list = [...filterData(activationList,days)]
console.log(list)
if (!list.length){
return alert('没有数据')
} else {
const result = list.map(item => {
return item.content
}).join('\n')
alert(result)
}
}
const setNewList = (data) => {
return Array.prototype.map.call(data, item => {
const createTime = daysMap[item.children[0].innerText.split('天')[0]] ? daysMap[item.children[0].innerText.split('天')[0]] : today - oneDay * item.children[0].innerText.split('天')[0]
console.log(createTime);
return {
createTime,
content: item.children[1].innerText
}
})
}
// 过滤掉days之前的数据
const filterData = (data, days) => {
const dates = new Date(new Date().setHours(0, 0, 0, 0))
return data.filter(item => {
console.log(dates - new Date(item.createTime).getTime() < days * oneDay);
return dates - new Date(item.createTime).getTime() < days * oneDay
})
}
createObserver =() => {
// 创建观察器
const observer = new MutationObserver(mutations => {
getList()
})
observer.observe(document.querySelector('.activation-list'), {
childList: true
})
}
window.onload = createObserver()
|