class NeoDB { constructor(config) { this.container = config.container; this.types = config.types ?? ["book", "movie", "tv", "music", "game", "podcast"]; this.baseAPI = config.baseAPI; this.ver = "1.0.6"; this.type = "movie"; this.status = "complete"; this.finished = false; this.paged = 1; this.subjects = []; this._create(); } on(event, element, callback) { const nodeList = document.querySelectorAll(element); nodeList.forEach((item) => { item.addEventListener(event, callback); }); } _handleTypeClick() { this.on("click", ".neodb-navItem", (t) => { const self = t.currentTarget; if (self.classList.contains("current")) return; this.type = self.dataset.type; document.querySelector(".neodb-list").innerHTML = ""; document.querySelector(".lds-ripple").classList.remove("u-hide"); document.querySelector(".neodb-navItem.current").classList.remove("current"); self.classList.add("current"); this.paged = 1; this.finished = false; this.subjects = []; this._fetchData(); }); } _renderTypes() { document.querySelector(".neodb-nav").innerHTML = this.types .map((item) => { return `${item}`; }) .join(""); this._handleTypeClick(); } _fetchData() { const params = new URLSearchParams({ type: "complete", category: this.type, page: this.paged.toString(), }); return fetch(this.baseAPI + "?" + params.toString()) .then((response) => response.json()) .then((data) => { if (data.length) { // 过滤重复项 data = data.filter(item => !this.subjects.some(existing => existing.item.id === item.item.id)); if (data.length) { this.subjects = [...this.subjects, ...data]; this._renderListTemplate(); } document.querySelector(".lds-ripple").classList.add("u-hide"); } else { this.finished = true; // 没有更多数据 document.querySelector(".lds-ripple").classList.add("u-hide"); } }); } _renderListTemplate() { document.querySelector(".neodb-list").innerHTML = this.subjects .map((item) => { const coverImage = item.item.cover_image_url; const title = item.item.title; const rating = item.item.rating; const link = item.item.id; return `
${rating ? `${rating}` : ""}
${title}
`; }) .join(""); } _handleScroll() { let isLoading = false; // 标志位,表示是否正在加载数据 let lastScrollTop = 0; // 上一次的滚动位置 window.addEventListener("scroll", () => { const scrollY = window.scrollY || window.pageYOffset; const moreElement = document.querySelector(".block-more"); // 检查滚动到底部的条件 if ( moreElement.offsetTop + moreElement.clientHeight <= scrollY + window.innerHeight && document.querySelector(".lds-ripple").classList.contains("u-hide") && !this.finished && !isLoading // 确保没有正在加载数据 ) { isLoading = true; // 设置标志位为 true,表示正在加载数据 document.querySelector(".lds-ripple").classList.remove("u-hide"); this.paged++; this._fetchData().finally(() => { isLoading = false; // 数据加载完成后,重置标志位 }); } // 更新上一次的滚动位置 lastScrollTop = scrollY; }); } _create() { if (document.querySelector(".neodb-container")) { const container = document.querySelector(this.container); if (!container) return; container.innerHTML = `
`; this._renderTypes(); this._fetchData(); this._handleScroll(); } } }