459 lines
16 KiB
JavaScript
459 lines
16 KiB
JavaScript
// import { storageObj } from '../huiyuan/public/localstorage.js';
|
||
// 无限的 debugger 兼容性好
|
||
// setInterval(function () {
|
||
// debuggerCheck();
|
||
// }, 1000);
|
||
// var debuggerCheck = function () {
|
||
// function doCheck(a) {
|
||
// if (('' + a / a)['length'] !== 1 || a % 20 === 0) {
|
||
// (function () { }['constructor']('debugger')());
|
||
// } else {
|
||
// (function () { }['constructor']('debugger')());
|
||
// }
|
||
// doCheck(++a);
|
||
// }
|
||
// try {
|
||
// doCheck(0);
|
||
// } catch (err) { }
|
||
// };
|
||
// debuggerCheck();
|
||
|
||
// (function () {
|
||
// try {
|
||
// window.history.pushState(null, '');
|
||
// window.addEventListener(
|
||
// 'popstate',
|
||
// function () {
|
||
// window.location.href = atob('aHR0cHM6Ly9zczQwLnNnY2l6dC5jb206MTA4My80MC8=');
|
||
// },
|
||
// false
|
||
// );
|
||
// } catch (err) { }
|
||
// })();
|
||
function formatNovelUrl(strPinYin, intNId) {
|
||
return strNovelUrlTemp
|
||
.replace('{strPinYin}', encodeURIComponent(strPinYin))
|
||
.replace('{intNId}', intNId);
|
||
}
|
||
|
||
function formatNovelChapterUrl(strPinYin, intNId, intSort,intPage=0) {
|
||
return strNovelChapterUrlTemp
|
||
.replace('{strPinYin}', encodeURIComponent(strPinYin))
|
||
.replace('{intNId}', intNId)
|
||
.replace('{intChapterPage}', intPage)
|
||
.replace('{intChapterSort}', intSort);
|
||
}
|
||
|
||
function formatSearchUrl(strKey,intPage=1) {
|
||
return strSearchUrlTemp
|
||
.replace('{strSearchKeywords}', strKey)
|
||
.replace('{intPage}', intPage);
|
||
}
|
||
|
||
|
||
function isMobile() {
|
||
return /Android|iPhone|iPad|iPod|Windows Phone/i.test(navigator.userAgent);
|
||
}
|
||
|
||
// 懒加载
|
||
document.addEventListener("DOMContentLoaded", function () {
|
||
// 通用懒加载函数
|
||
function lazyLoad(elements, callback) {
|
||
const observer = new IntersectionObserver((entries, observer) => {
|
||
entries.forEach(entry => {
|
||
if (entry.isIntersecting) {
|
||
const element = entry.target;
|
||
callback(element); // 执行懒加载逻辑
|
||
observer.unobserve(element); // 停止观察
|
||
}
|
||
});
|
||
});
|
||
|
||
elements.forEach(element => observer.observe(element));
|
||
}
|
||
|
||
// 背景封面懒加载
|
||
const lazyBackgrounds = document.querySelectorAll(".lazyload-bg");
|
||
lazyLoad(lazyBackgrounds, (element) => {
|
||
const src = element.getAttribute("data-src");
|
||
if (src) {
|
||
element.style.backgroundImage = `url(${src})`;
|
||
element.classList.remove("lazyload-bg");
|
||
}
|
||
});
|
||
|
||
// 章节图片懒加载
|
||
const lazyImages = document.querySelectorAll(".lazyload-img");
|
||
lazyLoad(lazyImages, (element) => {
|
||
const src = element.getAttribute("data-src");
|
||
if (src) {
|
||
element.setAttribute("src", src);
|
||
element.classList.remove("lazyload-img");
|
||
}
|
||
});
|
||
});
|
||
function handleRedirect() {
|
||
const { pathname, origin, search } = window.location;
|
||
|
||
// 规范化路径名,将多个斜杠替换为单个斜杠
|
||
const normalizedPath = pathname.replace(/\/+/g, '/');
|
||
const strCurrentPath = normalizedPath || '/index.html';
|
||
|
||
// 构建URL,确保斜杠正确
|
||
const buildUrl = (path) => {
|
||
// 确保路径以单个斜杠开头,并移除多余斜杠
|
||
const cleanPath = `/${path.replace(/\/+/g, '/')}`.replace(/^\/+/, '/');
|
||
return `${origin}${cleanPath}${search}`;
|
||
};
|
||
|
||
if (isMobile()) {
|
||
// 移动端逻辑:将PC路径重定向到移动端路径
|
||
if (strCurrentPath.startsWith(strPcPath)) {
|
||
const newPath = strCurrentPath.replace(strPcPath, '') || '/';
|
||
window.location.href = buildUrl(newPath);
|
||
}
|
||
} else {
|
||
// PC端逻辑:将非PC路径重定向到PC路径
|
||
if (!strCurrentPath.startsWith(strPcPath)) {
|
||
// 确保 PC 首页以斜杠结尾
|
||
const newPath = strCurrentPath === '/' ? `${strPcPath}/` : `${strPcPath}${strCurrentPath}`;
|
||
window.location.href = buildUrl(newPath);
|
||
}
|
||
}
|
||
}
|
||
handleRedirect();
|
||
|
||
|
||
function layerPopup(strContent, intTime) {
|
||
layer.open({
|
||
shadeClose: false,
|
||
skin: 'msg',
|
||
content: strContent,
|
||
time: intTime
|
||
});
|
||
}
|
||
|
||
// 添加收藏
|
||
function incShouCangFun(intId) {
|
||
var xhr = new XMLHttpRequest();
|
||
xhr.open('GET', "/sc?intId=" + intId, true);
|
||
xhr.setRequestHeader('accept', 'application/x.hubserver.admin+json');
|
||
xhr.send();
|
||
xhr.onreadystatechange = function () {
|
||
|
||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||
layerPopup('收藏成功', 3)
|
||
}
|
||
if (xhr.readyState == 4 && xhr.status == 0) {
|
||
layerPopup('操作太频繁,稍后再试', 3)
|
||
}
|
||
}
|
||
}
|
||
|
||
const strBookshelfKey = window.location.hostname + 'strBookshelfKey';
|
||
const strHistoryKey = window.location.hostname + 'strHistoryKey';
|
||
|
||
// 加入书架 addShuJiaFun
|
||
function addBookshelfFun(NovelInfo) {
|
||
|
||
if (!NovelInfo) {
|
||
NovelInfo = arrNovel
|
||
}
|
||
|
||
let arrBookshelf = JSON.parse(localStorage.getItem(strBookshelfKey)) || [];
|
||
|
||
// 检查书籍是否已存在
|
||
const index = arrBookshelf.findIndex(item => item.n_id === NovelInfo.n_id);
|
||
|
||
if (index !== -1) {
|
||
// 如果存在,更新该书籍的信息
|
||
arrBookshelf[index] = { ...arrBookshelf[index], ...NovelInfo };
|
||
layerPopup('已存在书架中!', 3)
|
||
} else {
|
||
// 如果不存在,添加到历史记录
|
||
arrBookshelf.push(NovelInfo);
|
||
layerPopup('已成功加入书架!', 3)
|
||
}
|
||
|
||
// 更新本地存储
|
||
localStorage.setItem(strBookshelfKey, JSON.stringify(arrBookshelf));
|
||
|
||
}
|
||
|
||
// 删除书架
|
||
function delBookshelfFun(intNId) {
|
||
// 获取书架数据
|
||
const arrHistory = JSON.parse(localStorage.getItem(strBookshelfKey)) || [];
|
||
|
||
// 检查 arrHistory 是否为数组且不为空
|
||
if (Array.isArray(arrHistory) && arrHistory.length > 0) {
|
||
// 找到匹配 intNId 的索引
|
||
const index = arrHistory.findIndex(item => item.n_id == intNId);
|
||
|
||
// 如果找到匹配项(index !== -1),则删除
|
||
if (index !== -1) {
|
||
arrHistory.splice(index, 1); // 删除该项
|
||
// 更新 localStorage
|
||
localStorage.setItem('strBookshelfKey', JSON.stringify(arrHistory));
|
||
} else {
|
||
console.log(`未找到 ID 为 ${intNId} 的记录`);
|
||
}
|
||
} else {
|
||
console.log('为空,无需删除');
|
||
}
|
||
removeElementsByClass('book-shelf')
|
||
|
||
displayBookshelf()
|
||
}
|
||
|
||
// 展示 书架
|
||
function displayBookshelf() {
|
||
|
||
// 获取书架数据
|
||
const arrBookshelf = JSON.parse(localStorage.getItem(strBookshelfKey)) || [];
|
||
|
||
if (arrBookshelf.length === 0) {
|
||
console.log('书架为空');
|
||
return;
|
||
}
|
||
|
||
// 渲染书籍列表
|
||
const DomBookshelfContainer = document.getElementById('bookshelfMod-head');
|
||
const DomBookshelfContainerH5 = document.getElementById('list');
|
||
arrBookshelf.forEach(book => {
|
||
let strNovelUrl = formatNovelUrl(book.og_novel_pin_yin, book.n_id)
|
||
let strNovelChapterUrl = formatNovelChapterUrl(book.og_novel_pin_yin, book.n_id, book.c_sort_num)
|
||
let strSearchUrl = formatSearchUrl(book.n_author, 1)
|
||
if (isMobile()) {
|
||
const bookElement = document.createElement('li');
|
||
bookElement.classList.add('book-shelf'); // 添加类名
|
||
bookElement.classList.add('li2'); // 添加类名
|
||
bookElement.innerHTML = `
|
||
<a href='${strNovelUrl}'>
|
||
<div class="leftImage2 ">
|
||
<img class="lazyload-img" src="${book.n_cover_path}" >
|
||
</div>
|
||
<div class="rightBlock">
|
||
<span class="rightBlock_title">${book.n_name}</span>
|
||
<span class="rightBlock_name">${book.n_author}</span>
|
||
<span class="rightBlock_con rightBlock_con2">
|
||
<span class="con">
|
||
<span class="spanLi">${book.c_name}</span>
|
||
</span>
|
||
<span class="spanLi Time">${book.n_update_time ?? ''}</span>
|
||
<span class="rightJustTime" onclick="javascript:window.location.href='${strNovelChapterUrl}';return false;">
|
||
继续阅读
|
||
</span>
|
||
</span>
|
||
</div>
|
||
</a>
|
||
<span class="deleteBlock" onclick="delBookshelfFun(${book.n_id})"></span>
|
||
|
||
`;
|
||
DomBookshelfContainerH5.appendChild(bookElement);
|
||
} else {
|
||
const bookElement = document.createElement('tr');
|
||
bookElement.classList.add('book-shelf'); // 添加类名
|
||
bookElement.innerHTML = `
|
||
<td>
|
||
<a href="${strPcPath}${strNovelUrl}" >
|
||
<img src="${book.n_cover_path}" >
|
||
</a>
|
||
</td>
|
||
<td class="t2">
|
||
<a href="${strPcPath}${strNovelUrl}" >${book.n_name}</a>
|
||
</td>
|
||
<td class="t3">
|
||
<a href="${strPcPath}${strNovelChapterUrl}" >${book.c_name}</a>
|
||
</td>
|
||
<td class="t4">
|
||
<a href="${strPcPath}${strSearchUrl}" >${book.n_author}</a>
|
||
</td>
|
||
<td class="t5">${book.n_update_time}</td>
|
||
|
||
<td class="t6">${book.n_status}</td>
|
||
<td class="t7">
|
||
<a href="${strPcPath}${strNovelChapterUrl}" class="goon" >阅读</a>
|
||
<i><span class="Collection" ajax-get="" onclick="delBookshelfFun(${book.n_id})">删除</span></i>
|
||
</td>
|
||
|
||
`;
|
||
DomBookshelfContainer.after(bookElement);
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
// 加入阅读记录
|
||
function addHistoryFun(NovelInfo) {
|
||
// 如果没有传入 NovelInfo,则使用 arrNovel 并补充字段
|
||
if (!NovelInfo) {
|
||
NovelInfo = arrNovel
|
||
|
||
}
|
||
|
||
// 获取本地存储中的历史记录
|
||
let arrBookshelf = JSON.parse(localStorage.getItem(strHistoryKey)) || [];
|
||
// 检查书籍是否已存在
|
||
const index = arrBookshelf.findIndex(item => item.n_id == NovelInfo.n_id);
|
||
|
||
if (index !== -1) {
|
||
// 如果存在,更新该书籍的信息
|
||
arrBookshelf[index] = { ...arrBookshelf[index], ...NovelInfo };
|
||
} else {
|
||
// 如果不存在,添加到历史记录
|
||
arrBookshelf.push(NovelInfo);
|
||
}
|
||
|
||
// 更新本地存储
|
||
localStorage.setItem(strHistoryKey, JSON.stringify(arrBookshelf));
|
||
}
|
||
|
||
// 删除历史记录
|
||
function delHistoryFun(intNId) {
|
||
// 获取书架数据
|
||
const arrHistory = JSON.parse(localStorage.getItem(strHistoryKey)) || [];
|
||
|
||
// 检查 arrHistory 是否为数组且不为空
|
||
if (Array.isArray(arrHistory) && arrHistory.length > 0) {
|
||
// 找到匹配 intNId 的索引
|
||
const index = arrHistory.findIndex(item => item.n_id == intNId);
|
||
|
||
// 如果找到匹配项(index !== -1),则删除
|
||
if (index !== -1) {
|
||
arrHistory.splice(index, 1); // 删除该项
|
||
// 更新 localStorage
|
||
localStorage.setItem(strHistoryKey, JSON.stringify(arrHistory));
|
||
} else {
|
||
console.log(`未找到 ID 为 ${intNId} 的历史记录`);
|
||
}
|
||
} else {
|
||
console.log('历史记录为空,无需删除');
|
||
}
|
||
removeElementsByClass('book-history')
|
||
displayHistory()
|
||
}
|
||
|
||
// 展示 历史记录
|
||
function displayHistory() {
|
||
|
||
// 获取书架数据
|
||
const arrHistory = JSON.parse(localStorage.getItem(strHistoryKey)) || [];
|
||
|
||
if (arrHistory.length === 0) {
|
||
console.log('书架为空');
|
||
return;
|
||
}
|
||
|
||
// 渲染书籍列表
|
||
const DomHistoryContainer = document.getElementById('bookshelfMod-head');
|
||
const DomBookshelfContainerH5 = document.getElementById('list');
|
||
arrHistory.forEach(book => {
|
||
let strNovelUrl = formatNovelUrl(book.og_novel_pin_yin, book.n_id)
|
||
let strNovelChapterUrl = formatNovelChapterUrl(book.og_novel_pin_yin, book.n_id, book.c_sort_num)
|
||
let strSearchUrl = formatSearchUrl(book.n_author, 1)
|
||
if (isMobile()) {
|
||
const bookElement = document.createElement('li');
|
||
bookElement.classList.add('book-history'); // 添加类名
|
||
bookElement.classList.add('li2'); // 添加类名
|
||
bookElement.innerHTML = `
|
||
<a href='${strNovelUrl}'>
|
||
<div class="leftImage2 ">
|
||
<img class="lazyload-img" src="${book.n_cover_path}" >
|
||
</div>
|
||
<div class="rightBlock">
|
||
<span class="rightBlock_title">${book.n_name}</span>
|
||
<span class="rightBlock_name">${book.n_author}</span>
|
||
<span class="rightBlock_con rightBlock_con2">
|
||
<span class="con">
|
||
<span class="spanLi">${book.c_name}</span>
|
||
</span>
|
||
<span class="spanLi Time">${book.n_update_time ?? ''}</span>
|
||
<span class="rightJustTime" onclick="javascript:window.location.href='${strNovelChapterUrl}';return false;"
|
||
>继续阅读</span>
|
||
</span>
|
||
</div>
|
||
</a>
|
||
<span class="deleteBlock" onclick="delHistoryFun(${book.n_id})"></span>
|
||
`;
|
||
DomBookshelfContainerH5.appendChild(bookElement);
|
||
} else {
|
||
const bookElement = document.createElement('tr');
|
||
bookElement.classList.add('book-history'); // 添加类名
|
||
bookElement.innerHTML = `
|
||
<td>
|
||
<a href="${strPcPath}${strNovelUrl}" >
|
||
<img src="${book.n_cover_path}" >
|
||
</a>
|
||
</td>
|
||
<td class="t2">
|
||
<a href="${strPcPath}${strNovelUrl}" >${book.n_name}</a>
|
||
</td>
|
||
<td class="t3">
|
||
<a href="${strPcPath}${strNovelChapterUrl}" >${book.c_name}</a>
|
||
</td>
|
||
<td class="t4">
|
||
<a href="${strPcPath}${strSearchUrl}" >${book.n_author}</a>
|
||
</td>
|
||
<td class="t5">${book.n_update_time ?? ''}</td>
|
||
|
||
<td class="t6">${book.n_status}</td>
|
||
<td class="t7">
|
||
<a href="${strNovelChapterUrl}" class="goon" >阅读</a>
|
||
<i><span class="Collection" ajax-get="" onclick="delHistoryFun(${book.n_id})">删除</span></i>
|
||
</td>
|
||
|
||
`;
|
||
DomHistoryContainer.after(bookElement);
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
|
||
// 方法1:使用 fetch API
|
||
async function reportStats(data) {
|
||
|
||
try {
|
||
const response = await fetch(strPushApi+'/publish', {
|
||
method: 'POST',
|
||
// mode: 'no-cors', // 添加这行
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
// 如果需要认证可以添加
|
||
// 'Authorization': 'Bearer your-token'
|
||
},
|
||
body: JSON.stringify(data)
|
||
});
|
||
|
||
if (!response.ok) {
|
||
throw new Error('上报失败');
|
||
}
|
||
|
||
// const result = await response.json();
|
||
// console.log('上报成功:', result);
|
||
// return result;
|
||
} catch (error) {
|
||
console.error('上报错误:', error);
|
||
}
|
||
}
|
||
|
||
function removeElementsByClass(className) {
|
||
const elements = document.querySelectorAll(`.${className}`);
|
||
elements.forEach(element => element.remove());
|
||
}
|
||
|
||
document.addEventListener("DOMContentLoaded", function () {
|
||
|
||
if (typeof strNovelId !== "undefined") {
|
||
|
||
// 使用示例
|
||
const statsData = {
|
||
n_id: strNovelId,
|
||
};
|
||
// 调用方法1
|
||
reportStats(statsData);
|
||
|
||
addHistoryFun()
|
||
}
|
||
}) |