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

update

上级 fa6f4704
......@@ -28,7 +28,7 @@ class Widget extends BaseWidget {
defaultPreference = {
domain: 'https://tophub.today',
hotban: { title: '微博 · 热搜榜', link: 'https://tophub.today/n/KqndgxeLl9' },
hotban: [{ title: '微博 · 热搜榜', link: 'https://tophub.today/n/KqndgxeLl9' }],
weiboOpenOptions: [
{ name: '国内版' },
{ name: '国际版' },
......@@ -57,7 +57,7 @@ class Widget extends BaseWidget {
largeItemCount: 13
};
getHotban = () => JSON.parse(this.useFileManager().readStringCache('hotban') ?? JSON.stringify(this.defaultPreference.hotban));
getHotban = (defaultValue) => JSON.parse(this.useFileManager().readStringCache('hotban') ?? (defaultValue ? defaultValue : JSON.stringify(this.defaultPreference.hotban)));
getWeiboOpenType = () => this.getSettingValueByKey('weiboOpenType', this.defaultPreference.weiboOpenOptions[2].name);
getTitleFontSize = () => Number(this.getSettingValueByKey('titleFontSize', `${this.defaultPreference.titleFontSize}`));
getTitleFontColor = () => this.getSettingValueByKey('titleFontColor', `${this.defaultPreference.titleFontColor}`);
......@@ -261,9 +261,6 @@ class Widget extends BaseWidget {
}
});
////////////////////////////////////
const { title, link } = event;
await this.generateAlert(`热榜内容`, `已成功添加«${title}»\n关闭窗口点击预览查看效果`, ['确定']);
this.useFileManager().writeStringCache('hotban', JSON.stringify({ title, link: `${this.defaultPreference.domain}${link}` }));
onItemClick?.(event);
}
......@@ -285,11 +282,11 @@ class Widget extends BaseWidget {
settingItems: [
{
name: 'hotban',
label: '热搜榜搜索',
label: '热搜榜设置',
type: 'cell',
icon: { name: 'flame', color: '#EB3323', },
needLoading: true,
default: this.getHotban().title,
showDesc: false,
},
{
name: 'weiboOpenType',
......@@ -403,19 +400,29 @@ class Widget extends BaseWidget {
let insertDesc;
switch (item.name) {
case 'hotban':
await this.generateInputAlert({
title: '热榜搜索',
options: [{ hint: '请输入关键字', value: '' }]
}, async (inputArr) => {
const keyword = inputArr[0].value;
let response = undefined;
try {
//////
const html = await this.httpGet(`${this.defaultPreference.domain}/search?q=${encodeURIComponent(keyword)}`, { jsonFormat: false, headers: this.defaultPreference.phoneHeaders });
let webview = new WebView();
await webview.loadHTML(html);
// 通过dom操作把HTML里面的热榜内容提取出来
const getData = `
const hotSelectIndex = await this.presentSheet({
title: '热榜设置',
message: '⊱配置热榜显示榜单内容⊰',
options: [{ name: '查看已添加榜单' }, { name: '搜索添加榜单' }, { name: '重置榜单' }],
});
if (hotSelectIndex == 0) {
const hotbanArr = this.getHotban('[]');
const hotbanTitleArr = hotbanArr.map(hotban => hotban.title);
await this.generateAlert('已添加榜单', `${hotbanTitleArr.length > 0 ? hotbanTitleArr.join('') : '暂无添加,默认微博热搜'}`, ['确定']);
} else if (hotSelectIndex == 1) {
await this.generateInputAlert({
title: '热榜搜索',
options: [{ hint: '请输入关键字', value: '' }]
}, async (inputArr) => {
const keyword = inputArr[0].value;
let response = undefined;
try {
//////
const html = await this.httpGet(`${this.defaultPreference.domain}/search?q=${encodeURIComponent(keyword)}`, { jsonFormat: false, headers: this.defaultPreference.phoneHeaders });
let webview = new WebView();
await webview.loadHTML(html);
// 通过dom操作把HTML里面的热榜内容提取出来
const getData = `
function getData() {
// 图片封面
coverArr = []
......@@ -449,22 +456,31 @@ class Widget extends BaseWidget {
}
getData()
`
// 热榜数据
response = await webview.evaluateJavaScript(getData, false);
const { linkArr = [] } = response;
if (linkArr.length === 0) {
await this.generateAlert('热榜搜索', '搜索结果为空', ['确定']);
} else {
await this.renderSearchResultView(response, () => {
this.rerunWidget();
});
}
//////
} catch (error) {
console.error(`🚫 热榜搜索出错===>${error}`);
await this.generateAlert('🚫 热榜搜索出错', `${error}`, ['确定']);
};
});
// 热榜数据
response = await webview.evaluateJavaScript(getData, false);
const { linkArr = [] } = response;
if (linkArr.length === 0) {
await this.generateAlert('热榜搜索', '搜索结果为空', ['确定']);
} else {
await this.renderSearchResultView(response, async (event) => {
const { title, link } = event;
await this.generateAlert(`热榜内容`, `已成功添加«${title}»\n关闭窗口重新运行`, ['确定']);
const hotbanArr = this.getHotban('[]');
hotbanArr.push({ title, link: `${this.defaultPreference.domain}${link}` });
this.useFileManager().writeStringCache('hotban', JSON.stringify(hotbanArr));
this.rerunWidget();
});
}
//////
} catch (error) {
console.error(`🚫 热榜搜索出错==>${error}`);
await this.generateAlert('🚫 热榜搜索出错', `${error}`, ['确定']);
};
});
} else {
this.useFileManager().writeStringCache('hotban', JSON.stringify([]));
await this.generateAlert('热榜设置', '热榜已重置成功', ['确定']);
}
break;
case 'weiboOpenType':
......@@ -530,7 +546,11 @@ class Widget extends BaseWidget {
}
async provideWidget(itemCount, small = false) {
const response = await this.loadHotBanRES(this.getHotban().link);
const defaultHotArr = this.defaultPreference.hotban;
const cacheHotStr = this.useFileManager().readStringCache('hotban');
const hotbanArr = cacheHotStr ? JSON.parse(cacheHotStr) : defaultHotArr;
const index = this.carouselIndex('hotbanRandom', hotbanArr.length);
const response = await this.loadHotBanRES(hotbanArr[index].link);
// 数据
const { hotTitle = '', logoUrl = '', linkArr = [], titleArr = [] } = response;
//=================================
......
......@@ -1042,16 +1042,15 @@ class BaseWidget {
}
}
carouselIndex = (cacheKey, size, root = false) => {
carouselIndex = (cacheKey, size) => {
let index = -1;
const fullName = this.useFileManager().fullFileName(cacheKey, root);
if (Keychain.contains(fullName)) {
let cacheString = this.keyGet(fullName);
if (Keychain.contains(cacheKey)) {
let cacheString = this.keyGet(cacheKey);
index = parseInt(cacheString);
}
index = index + 1;
index = index % size;
this.keySave(fullName, `${index}`)
this.keySave(cacheKey, `${index}`)
return index
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册