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

update 写真

上级 dd6ce1d5
...@@ -1040,6 +1040,15 @@ class BaseWidget { ...@@ -1040,6 +1040,15 @@ class BaseWidget {
return index return index
} }
getRandowArrValue(arr) {
const key = parseInt(Math.random() * arr.length)
let item = arr[key]
if (item == undefined) {
item = arr[0]
}
return item
}
keySave = (cacheKey, cache) => { keySave = (cacheKey, cache) => {
if (cache) { if (cache) {
Keychain.set(cacheKey, cache); Keychain.set(cacheKey, cache);
...@@ -1755,9 +1764,9 @@ class BaseWidget { ...@@ -1755,9 +1764,9 @@ class BaseWidget {
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36" "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36"
}; };
const html = await this.httpGet(url, { jsonFromat: false, headers }); const html = await this.httpGet(url, { jsonFromat: false, headers });
let webview = new WebView(); const webview = new WebView();
await webview.loadHTML(html); await webview.loadHTML(html);
var getData = ` const getData = `
function getData() { function getData() {
try { try {
infoLunarText = document.querySelector('div#wnrl_k_you_id_${day}.wnrl_k_you .wnrl_k_you_id_wnrl_nongli').innerText infoLunarText = document.querySelector('div#wnrl_k_you_id_${day}.wnrl_k_you .wnrl_k_you_id_wnrl_nongli').innerText
......
此差异已折叠。
// Variables used by Scriptable. // Variables used by Scriptable.
// These must be at the very top of the file. Do not edit. // These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: user-astronaut; // icon-color: red; icon-glyph: user-astronaut;
/** /**
* Author:LSP * Author:LSP
* Date:2022-11-05 * Date:2022-12-09
*/ */
// @导入引用开发环境 // -------------------------------------------------------
// 是否是开发环境,配合手机端调试使用,正式发布设置为false
const isDev = true;
console.log(`开发环境 👉👉👉👉👉 ${isDev ? 'DEV' : 'RELEASE'}`);
console.log(`----------------------------------------`);
const remoteRoot = 'https://gitcode.net/enoyee/scriptable/-/raw/master/';
// 依赖包目录
const fm = FileManager.local();
const rootDir = fm.documentsDirectory();
const cacheDir = fm.joinPath(rootDir, 'LSP');
const dependencyFileName = isDev ? "_LSP.js" : `${cacheDir}/_LSP.js`;
// 下载依赖包
await downloadLSPDependency();
// -------------------------------------------------------
if (typeof require === 'undefined') require = importModule if (typeof require === 'undefined') require = importModule
const { Base } = require("./lsp环境") // 引入相关方法
const { BaseWidget } = require(dependencyFileName);
const widgetConfigs = {
// 图片等分缓存key
picHalvingCacheKey: '_halving',
}
// @定义小组件 // @定义小组件
class Widget extends Base { class Widget extends BaseWidget {
constructor(scriptName) {
super(scriptName)
// @小组件配置
this.setModuleName(module.filename);
// 初始化设置菜单
this.initWidgetSettingMenu();
}
/** defaultPreference = {
* 初始化设置菜单 photoHalving: '1',
*/ sourceIndex: '0',
async initWidgetSettingMenu() { sourceArr: [
// 组件菜单点击 { name: 'dopaminegirl' },
const onMenuItemClick = async (item) => { { name: 'dounimei' },
switch (item.title) { ]
case '图片等分': };
const picHalvingCache = this.keyGet(widgetConfigs.picHalvingCacheKey, '1');
await this.generateInputAlert('图片等分设置', '根据选择的大中小组件自己划分,最大值为4', '请输入数字进行等分', picHalvingCache, (content) => { getValueByKey = (key) => this.readWidgetSetting()[key] ?? this.defaultPreference[key];
this.keySave(widgetConfigs.picHalvingCacheKey, content); photoHalving = () => this.getValueByKey('photoHalving');
}); sourceIndex = () => Number(this.getValueByKey('sourceIndex'));
break; sourceDesc = (sourceIndex) => this.defaultPreference.sourceArr[sourceIndex].name;
}
}
// 设置菜单
const settingMenu = [
{
icon: { name: 'photo.artframe', color: '#E6AABB', cornerWidth: 40 },
title: '图片等分',
onClick: onMenuItemClick
},
];
////////////////////////////////////////////////////////////////////////
this.configSetting(settingMenu);
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ constructor(scriptName) {
super(scriptName);
}
addStackImg = async (imgRes, imgStack) => { async getAppViewOptions() {
if (imgRes && imgRes.length > 0) { return {
let item = this.getRandowArrValue(imgRes); widgetProvider: {
let imgUrl = item.url; small: true, // 是否提供小号组件
imgStack.url = imgUrl; medium: true, // 是否提供中号组件
let img = await this.getImageByUrl(imgUrl, 'mirror', true); large: true, // 是否提供大号组件
let imgSpan = imgStack.addImage(img); },
imgSpan.applyFillingContentMode(); // 预览界面的组件设置item
settingItems: [
{
name: 'photoHalving',
label: '图片等分',
type: 'cell',
icon: { name: 'photo.artframe', color: '#ffca3a' },
alert: {
title: '图片等分设置',
message: "小组件显示多少张图片,最大值为4",
options: [
{
key: 'photoHalving',
hint: '请输入数字进行等分',
}
]
},
needLoading: false,
default: this.photoHalving(),
},
{
name: 'source',
label: '图源选择',
type: 'cell',
icon: { name: 'tray', color: '#7743DB', },
needLoading: false,
default: this.sourceDesc(this.sourceIndex()),
},
],
// cell类型的item点击回调
onItemClick: async (item) => {
let widgetSetting = this.readWidgetSetting();
let insertDesc;
switch (item.name) {
case 'source':
const ufm = this.useFileManager();
const index = await this.presentSheet({
title: '图源设置',
message: '⊱配置图片获取来源⊰',
options: this.defaultPreference.sourceArr,
});
widgetSetting.sourceIndex = `${index}`
insertDesc = this.sourceDesc(index);
break;
} }
} // 写入更新配置
this.writeWidgetSetting(widgetSetting);
return {
desc: { value: insertDesc },
};
},
};
}
async render({ widgetSetting, family }) {
return await this.provideWidget(family, this.photoHalving());
}
/** async provideWidget(widgetFamily, photoHalving) {
* 小型组件 let name = '大号';
* @returns switch (widgetFamily) {
*/ case 'small':
async provideSmallWidget() { name = '小号';
return await this.provideWidget('小号'); break;
case 'medium':
name = '中号';
break;
} }
//=================================
const widget = new ListWidget();
widget.setPadding(0, 0, 0, 0);
//=================================
let stack = widget.addStack();
const imgRes = await this.loadMirrorPhotoRes(this.sourceIndex());
const widgetSize = this.getWidgetSize(name);
const widgetWidth = widgetSize.width + 8 * Device.screenScale();
// 图片等分
const picHalvingCache = Number(photoHalving);
let imgStack;
switch (picHalvingCache) {
case 2:
stack.layoutHorizontally();
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth / 2, widgetWidth);
await this.addStackImg(imgRes, imgStack);
//
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth / 2, widgetWidth);
await this.addStackImg(imgRes, imgStack);
break;
case 3:
stack.layoutVertically();
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth, widgetWidth / 2);
await this.addStackImg(imgRes, imgStack);
//
imgStack = stack.addStack();
imgStack.layoutHorizontally();
let stack_3_1 = imgStack.addStack();
stack_3_1.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_3_1);
let stack_3_2 = imgStack.addStack();
stack_3_2.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_3_2);
break;
/** case 4:
* 中型组件 stack.layoutVertically();
* @returns imgStack = stack.addStack();
*/ imgStack.layoutHorizontally();
async provideMediumWidget() { let stack_4_1 = imgStack.addStack();
return await this.provideWidget('中号'); stack_4_1.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_4_1);
let stack_4_2 = imgStack.addStack();
stack_4_2.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_4_2);
//
imgStack = stack.addStack();
imgStack.layoutHorizontally();
let stack_4_3 = imgStack.addStack();
stack_4_3.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_4_3);
let stack_4_4 = imgStack.addStack();
stack_4_4.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_4_4);
break;
default:
stack.layoutHorizontally();
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth, widgetWidth);
await this.addStackImg(imgRes, imgStack);
break;
} }
//=================================
stack.addSpacer();
return widget;
}
/** addStackImg = async (imgRes, imgStack) => {
* 大型组件 if (imgRes && imgRes.length > 0) {
* @returns let item = this.getRandowArrValue(imgRes);
*/ let imgUrl = item.url;
async provideLargeWidget() { imgStack.url = imgUrl;
return await this.provideWidget('大号'); let img = await this.getImageByUrl(imgUrl, 'mirror', true);
let imgSpan = imgStack.addImage(img);
imgSpan.applyFillingContentMode();
} }
}
async provideWidget(type = '大号') { // --------------------------NET START--------------------------
let name = type;
const widgetFamily = config.widgetFamily; loadMirrorPhotoRes = async (source = 0) => {
switch (widgetFamily) { const imgResArr = [];
case 'small': switch (source) {
name = '小号'; case 0:
break;
case 'medium':
name = '中号';
break;
case 'large':
name = '大号';
break;
}
//=================================
const widget = new ListWidget();
//=================================
let stack = widget.addStack();
const imgRes = await this.httpGet(`https://dopaminegirl.com/api/posts/ranking?offset=${parseInt(Math.random() * 1000) + 100}`); const imgRes = await this.httpGet(`https://dopaminegirl.com/api/posts/ranking?offset=${parseInt(Math.random() * 1000) + 100}`);
const widgetWidth = this.getWidgetWidthSize(name, false) + 20; imgRes.forEach(element => {
// 图片等分 imgResArr.push({ url: element.url });
const picHalvingCache = Number(this.keyGet(widgetConfigs.picHalvingCacheKey, '1')); });
let imgStack; break;
switch (picHalvingCache) {
case 2: case 1:
stack.layoutHorizontally(); // 抖你妹数据
imgStack = stack.addStack(); const url = `https://www.dounimei.co/page/${parseInt(Math.random() * 80) + 1}?orderby=hot`;
imgStack.size = new Size(widgetWidth / 2, widgetWidth); const headers = {
await this.addStackImg(imgRes, imgStack); "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36"
// };
imgStack = stack.addStack(); const webview = new WebView();
imgStack.size = new Size(widgetWidth / 2, widgetWidth); const html = await this.httpGet(url, { jsonFromat: false, headers });
await this.addStackImg(imgRes, imgStack); await webview.loadHTML(html);
break; const getData = `
function getData() {
case 3: let imgArr = [];
stack.layoutVertically(); try {
imgStack = stack.addStack(); let imgData = document.getElementsByTagName('img');
imgStack.size = new Size(widgetWidth, widgetWidth / 2); for (let index = 0; index < imgData.length; index++) {
await this.addStackImg(imgRes, imgStack); let elementSrc = imgData[index].getAttribute("data-src");
// if(elementSrc != null && elementSrc != undefined) {
imgStack = stack.addStack(); let start = elementSrc.indexOf('?src=');
imgStack.layoutHorizontally(); let end = elementSrc.lastIndexOf('&h');
let stack_3_1 = imgStack.addStack(); imgArr.push({url : elementSrc.slice(start + 5, end)});
stack_3_1.size = new Size(widgetWidth / 2, widgetWidth / 2); }
await this.addStackImg(imgRes, stack_3_1); }
let stack_3_2 = imgStack.addStack(); } catch(e) {
stack_3_2.size = new Size(widgetWidth / 2, widgetWidth / 2); console.log('抖你妹资源获取出错->' + e);
await this.addStackImg(imgRes, stack_3_2); }
break; return imgArr;
}
case 4: getData();
stack.layoutVertically(); `
imgStack = stack.addStack(); const dounimeiRes = await webview.evaluateJavaScript(getData, false);
imgStack.layoutHorizontally(); dounimeiRes.forEach(element => {
let stack_4_1 = imgStack.addStack(); imgResArr.push({ url: element.url });
stack_4_1.size = new Size(widgetWidth / 2, widgetWidth / 2); });
await this.addStackImg(imgRes, stack_4_1); break;
let stack_4_2 = imgStack.addStack();
stack_4_2.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_4_2);
//
imgStack = stack.addStack();
imgStack.layoutHorizontally();
let stack_4_3 = imgStack.addStack();
stack_4_3.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_4_3);
let stack_4_4 = imgStack.addStack();
stack_4_4.size = new Size(widgetWidth / 2, widgetWidth / 2);
await this.addStackImg(imgRes, stack_4_4);
break;
default:
stack.layoutHorizontally();
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth, widgetWidth);
await this.addStackImg(imgRes, imgStack);
break;
}
//=================================
stack.addSpacer();
return widget;
} }
return imgResArr;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ // --------------------------NET END--------------------------
} }
// @运行测试 await new Widget(Script.name()).run();
const { Running } = require("./lsp环境")
const newConfig = {
visualMode: true, // =================================================================================
small: true, // =================================================================================
medium: true, async function downloadLSPDependency() {
large: true, let fm = FileManager.local();
accessoryCircular: false, const dependencyURL = `${remoteRoot}_LSP.js`;
accessoryRectangular: false, if (isDev) {
accessoryInline: false const iCloudPath = FileManager.iCloud().documentsDirectory();
const localIcloudDependencyExit = fm.isFileStoredIniCloud(`${iCloudPath}/_LSP.js`);
const localDependencyExit = fm.fileExists(`${rootDir}/_LSP.js`);
const fileExist = localIcloudDependencyExit || localDependencyExit;
console.log(`🚀 DEV开发依赖文件${fileExist ? '已存在 ✅' : '不存在 🚫'}`);
if (!fileExist) {
console.log(`🤖 DEV 开始下载依赖~`);
await downloadFile2Scriptable('_LSP', dependencyURL);
}
return
}
//////////////////////////////////////////////////////////
console.log(`----------------------------------------`);
const remoteDependencyExit = fm.fileExists(`${cacheDir}/_LSP.js`);
console.log(`🚀 RELEASE依赖文件${remoteDependencyExit ? '已存在 ✅' : '不存在 🚫'}`);
console.log(`----------------------------------------`);
// ------------------------------
if (!remoteDependencyExit) { // 下载依赖
// 创建根目录
if (!fm.fileExists(cacheDir)) {
fm.createDirectory(cacheDir, true);
}
// 下载
console.log('🤖 RELEASE开始下载依赖~');
console.log(`----------------------------------------`);
const req = new Request(dependencyURL);
const moduleJs = await req.load();
if (moduleJs) {
fm.write(fm.joinPath(cacheDir, '/_LSP.js'), moduleJs);
console.log('✅ LSP远程依赖环境下载成功!');
console.log(`----------------------------------------`);
} else {
console.error('🚫 获取依赖环境脚本失败,请重试!');
console.log(`----------------------------------------`);
}
}
}
/**
* 获取保存的文件名
* @param {*} fileName
* @returns
*/
function getSaveFileName(fileName) {
const hasSuffix = fileName.lastIndexOf(".") + 1;
return !hasSuffix ? `${fileName}.js` : fileName;
}; };
await Running(Widget, Script.name(), true, newConfig)
\ No newline at end of file /**
* 保存文件到Scriptable软件目录,app可看到
* @param {*} fileName
* @param {*} content
* @returns
*/
function saveFile2Scriptable(fileName, content) {
try {
const fm = FileManager.iCloud();
let fileSimpleName = getSaveFileName(fileName);
const filePath = fm.joinPath(fm.documentsDirectory(), fileSimpleName);
fm.writeString(filePath, content);
return true;
} catch (error) {
return false;
}
};
/**
* 下载js文件到Scriptable软件目录
* @param {*} moduleName 名称
* @param {*} url 在线地址
* @returns
*/
async function downloadFile2Scriptable(moduleName, url) {
const req = new Request(url);
const content = await req.loadString();
return saveFile2Scriptable(`${moduleName}`, content);
};
// =================================================================================
// =================================================================================
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册