提交 a81b891e 编写于 作者: AndroidLeaves's avatar AndroidLeaves

update

上级 26799c50
......@@ -27,7 +27,7 @@ const { BaseWidget } = require(dependencyFileName);
class Widget extends BaseWidget {
defaultPreference = {
version: 20221213_2,
version: 20221213_3,
domain: 'https://tophub.today',
hotban: [{ title: '微博 · 热搜榜', link: 'https://tophub.today/n/KqndgxeLl9' }],
weiboOpenOptions: [
......@@ -36,15 +36,6 @@ class Widget extends BaseWidget {
{ name: '浏览器' },
],
// --------------------------
// 网络请求头
phoneHeaders: [
{
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.17(0x17001126) NetType/WIFI Language/zh_CN",
"referer": "https://tophub.today/c/news",
},
],
// --------------------------
titleFontSize: 16,
titleFontColor: '#FFFFFF',
contentFontSize: 13,
......@@ -62,8 +53,7 @@ class Widget extends BaseWidget {
async getCookie() {
let cookie = '';
const { hotbanCK } = this.readWidgetSetting();
console.log(`???${hotbanCK}`);
if (hotbanCK) {
if (hotbanCK?.length > 0) {
const hotbanCKArr = hotbanCK.split(";");
if (hotbanCKArr.length !== 3) {
await this.generateAlert('TopHub添加cookie', `添加的cookie不符合要求,需要登录后的cookie`, ['确定']);
......@@ -73,22 +63,21 @@ class Widget extends BaseWidget {
let Hm_lpvt;
//
hotbanCKArr.forEach(element => {
if (element.startsWith('Hm_lvt')) {
if (element.indexOf('Hm_lvt') != -1) {
let index = element.lastIndexOf("=");
Hm_lvt = element.slice(0, index)
Hm_lvt = element.slice(0, index - 1)
Hm_lvt += `=${Math.round(new Date() / 1000)}`
} else if (element.startsWith('Hm_lpvt')) {
} else if (element.indexOf('Hm_lpvt') != -1) {
let index = element.lastIndexOf("=");
Hm_lpvt = element.slice(0, index)
Hm_lpvt = element.slice(0, index - 1)
Hm_lpvt += `=${Math.round(new Date() / 1000) - 86400 * Math.ceil(Math.random() * 6)},${Math.round(new Date() / 1000) - 86400 * Math.ceil(Math.random() * 3)}`
} else {
} else if (element.startsWith('itc_center_user')) {
itc_center_user = element;
}
});
//
cookie = (itc_center_user + ";" + Hm_lvt + ";" + Hm_lpvt);
}
cookie = (itc_center_user + Hm_lvt + Hm_lpvt).replace('\n', '');
console.log(`cookie=>${cookie}`);
}
return cookie;
}
......@@ -471,9 +460,9 @@ class Widget extends BaseWidget {
let response = undefined;
try {
//////
const headers = { ...this.getRandowArrValue(this.defaultPreference.phoneHeaders), cookie: this.getCookie() };
const html = await this.httpGet(`${this.defaultPreference.domain}/search?q=${encodeURIComponent(keyword)}`, { jsonFormat: false, useCache: false, headers });
let webview = new WebView();
const webview = new WebView();
await webview.loadURL(`${this.defaultPreference.domain}/search?q=${encodeURIComponent(keyword)}`);
const html = await webview.getHTML();
await webview.loadHTML(html);
// 通过dom操作把HTML里面的热榜内容提取出来
const getData = `
......@@ -638,7 +627,6 @@ class Widget extends BaseWidget {
let hotbanArr = cacheHotStr ? JSON.parse(cacheHotStr) : defaultHotArr;
hotbanArr = hotbanArr.length === 0 ? defaultHotArr : hotbanArr;
const index = this.carouselIndex(`HotbanRandom${this.defaultPreference.version}`, hotbanArr.length);
console.log(`????>>>index=${index}, hotbanArr=${JSON.stringify(hotbanArr)}`);
const response = await this.loadHotBanRES(hotbanArr[index].link);
// 数据
const { hotTitle = '', logoUrl = '', linkArr = [], titleArr = [] } = response;
......@@ -697,7 +685,7 @@ class Widget extends BaseWidget {
textSpan.lineLimit = 1;
//
let linkElement = link;
if (!link.startsWith('http')) {
if (!link?.startsWith('http')) {
linkElement = `https://tophub.today${link}`;
}
if (hotTitle.search("微博") != -1) {
......@@ -722,9 +710,26 @@ class Widget extends BaseWidget {
// 热榜数据
let response = undefined;
try {
const headers = { ...this.getRandowArrValue(this.defaultPreference.phoneHeaders), cookie: this.getCookie() };
const html = await this.httpGet(link, { jsonFormat: false, headers });
let webview = new WebView();
const ufm = this.useFileManager();
const webview = new WebView();
const cacheFileName = this.md5(link);
const lastCacheTime = ufm.getCacheModifyDate(cacheFileName);
const timeInterval = Math.floor((this.getCurrentTimeStamp() - lastCacheTime) / 60);
// 读取本地缓存
const localCache = ufm.readStringCache(cacheFileName);
const canUseCache = localCache != null && localCache.length > 0;
// 过时且有本地缓存则直接返回本地缓存数据
const { refreshInterval = '0' } = this.readWidgetSetting();
const shouldLoadCache = timeInterval <= Number(refreshInterval) && canUseCache;
let html = ufm.readStringCache(cacheFileName);
if (!shouldLoadCache) {
console.log(`-->>在线加载网页数据`);
await webview.loadURL(link);
html = await webview.getHTML();
ufm.writeStringCache(cacheFileName, html);
} else {
console.log(`-->>加载缓存网页数据`);
}
await webview.loadHTML(html);
// 通过dom操作把HTML里面的热榜内容提取出来
const getData =
......@@ -735,6 +740,9 @@ class Widget extends BaseWidget {
// 榜单标题
hotTitle = '--'
branLogoArr = document.getElementsByClassName('brand logo')
if(JSON.stringify(branLogoArr) == '{}') {
return getPCData()
}
if(branLogoArr.length > 0) {
branLogo = branLogoArr[0]
logoUrl = branLogo.style['background-image'].slice(5).slice(0, -2)
......@@ -763,6 +771,31 @@ class Widget extends BaseWidget {
}
return { hotTitle, logoUrl, linkArr, titleArr };
}
function getPCData() {
// 获取榜单标题
hotTitle = document.querySelector('.Xc-ec-L').innerText
// 获取logo链接
logoImgHtml = document.querySelector('#tabbed-header-panel div').innerHTML;
// 提取src的正则表达式
logoPattern = /<img.*?src{1}=['"]([^'"]+)['"]+/g;
// 提取logo链接
logoUrl = logoPattern.exec(logoImgHtml)[1]
// 链接
linkArr = []
// 标题
titleArr = []
// 所有节点
allItemNodeList = document.querySelectorAll('.al')
for(let node of allItemNodeList) {
// 链接
node.innerHTML.replace(/<a [^>]*href=['"]([^'"]+)[^>]*/gi, function(match, link){
linkArr.push(link)
});
// 标题
titleArr.push(node.innerText)
}
return { hotTitle, logoUrl, linkArr, titleArr };
}
getData()
`
// 热榜数据
......@@ -771,8 +804,8 @@ class Widget extends BaseWidget {
this.useFileManager().writeStringCache('hot', JSON.stringify(response));
}
} catch (error) {
response = JSON.parse(this.useFileManager().readStringCache('hot'));
console.error(`🚫 请求热板数据出错了=>${error}`);
response = JSON.parse(this.useFileManager().readStringCache('hot'));
}
return response;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册