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

update 写真

上级 dd6ce1d5
......@@ -1040,6 +1040,15 @@ class BaseWidget {
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) => {
if (cache) {
Keychain.set(cacheKey, cache);
......@@ -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"
};
const html = await this.httpGet(url, { jsonFromat: false, headers });
let webview = new WebView();
const webview = new WebView();
await webview.loadHTML(html);
var getData = `
const getData = `
function getData() {
try {
infoLunarText = document.querySelector('div#wnrl_k_you_id_${day}.wnrl_k_you .wnrl_k_you_id_wnrl_nongli').innerText
......
此差异已折叠。
// Variables used by Scriptable.
// 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
* 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
const { Base } = require("./lsp环境")
const widgetConfigs = {
// 图片等分缓存key
picHalvingCacheKey: '_halving',
}
// 引入相关方法
const { BaseWidget } = require(dependencyFileName);
// @定义小组件
class Widget extends Base {
constructor(scriptName) {
super(scriptName)
// @小组件配置
this.setModuleName(module.filename);
// 初始化设置菜单
this.initWidgetSettingMenu();
}
class Widget extends BaseWidget {
/**
* 初始化设置菜单
*/
async initWidgetSettingMenu() {
// 组件菜单点击
const onMenuItemClick = async (item) => {
switch (item.title) {
case '图片等分':
const picHalvingCache = this.keyGet(widgetConfigs.picHalvingCacheKey, '1');
await this.generateInputAlert('图片等分设置', '根据选择的大中小组件自己划分,最大值为4', '请输入数字进行等分', picHalvingCache, (content) => {
this.keySave(widgetConfigs.picHalvingCacheKey, content);
});
break;
}
}
// 设置菜单
const settingMenu = [
{
icon: { name: 'photo.artframe', color: '#E6AABB', cornerWidth: 40 },
title: '图片等分',
onClick: onMenuItemClick
},
];
////////////////////////////////////////////////////////////////////////
this.configSetting(settingMenu);
}
defaultPreference = {
photoHalving: '1',
sourceIndex: '0',
sourceArr: [
{ name: 'dopaminegirl' },
{ name: 'dounimei' },
]
};
getValueByKey = (key) => this.readWidgetSetting()[key] ?? this.defaultPreference[key];
photoHalving = () => this.getValueByKey('photoHalving');
sourceIndex = () => Number(this.getValueByKey('sourceIndex'));
sourceDesc = (sourceIndex) => this.defaultPreference.sourceArr[sourceIndex].name;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
constructor(scriptName) {
super(scriptName);
}
addStackImg = async (imgRes, imgStack) => {
if (imgRes && imgRes.length > 0) {
let item = this.getRandowArrValue(imgRes);
let imgUrl = item.url;
imgStack.url = imgUrl;
let img = await this.getImageByUrl(imgUrl, 'mirror', true);
let imgSpan = imgStack.addImage(img);
imgSpan.applyFillingContentMode();
async getAppViewOptions() {
return {
widgetProvider: {
small: true, // 是否提供小号组件
medium: true, // 是否提供中号组件
large: true, // 是否提供大号组件
},
// 预览界面的组件设置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());
}
/**
* 小型组件
* @returns
*/
async provideSmallWidget() {
return await this.provideWidget('小号');
async provideWidget(widgetFamily, photoHalving) {
let name = '大号';
switch (widgetFamily) {
case 'small':
name = '小号';
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;
/**
* 中型组件
* @returns
*/
async provideMediumWidget() {
return await this.provideWidget('中号');
case 4:
stack.layoutVertically();
imgStack = stack.addStack();
imgStack.layoutHorizontally();
let stack_4_1 = imgStack.addStack();
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;
}
/**
* 大型组件
* @returns
*/
async provideLargeWidget() {
return await this.provideWidget('大号');
addStackImg = async (imgRes, imgStack) => {
if (imgRes && imgRes.length > 0) {
let item = this.getRandowArrValue(imgRes);
let imgUrl = item.url;
imgStack.url = imgUrl;
let img = await this.getImageByUrl(imgUrl, 'mirror', true);
let imgSpan = imgStack.addImage(img);
imgSpan.applyFillingContentMode();
}
}
async provideWidget(type = '大号') {
let name = type;
const widgetFamily = config.widgetFamily;
switch (widgetFamily) {
case 'small':
name = '小号';
break;
case 'medium':
name = '中号';
break;
case 'large':
name = '大号';
break;
}
//=================================
const widget = new ListWidget();
//=================================
let stack = widget.addStack();
// --------------------------NET START--------------------------
loadMirrorPhotoRes = async (source = 0) => {
const imgResArr = [];
switch (source) {
case 0:
const imgRes = await this.httpGet(`https://dopaminegirl.com/api/posts/ranking?offset=${parseInt(Math.random() * 1000) + 100}`);
const widgetWidth = this.getWidgetWidthSize(name, false) + 20;
// 图片等分
const picHalvingCache = Number(this.keyGet(widgetConfigs.picHalvingCacheKey, '1'));
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();
imgStack = stack.addStack();
imgStack.layoutHorizontally();
let stack_4_1 = imgStack.addStack();
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;
imgRes.forEach(element => {
imgResArr.push({ url: element.url });
});
break;
case 1:
// 抖你妹数据
const url = `https://www.dounimei.co/page/${parseInt(Math.random() * 80) + 1}?orderby=hot`;
const headers = {
"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 webview = new WebView();
const html = await this.httpGet(url, { jsonFromat: false, headers });
await webview.loadHTML(html);
const getData = `
function getData() {
let imgArr = [];
try {
let imgData = document.getElementsByTagName('img');
for (let index = 0; index < imgData.length; index++) {
let elementSrc = imgData[index].getAttribute("data-src");
if(elementSrc != null && elementSrc != undefined) {
let start = elementSrc.indexOf('?src=');
let end = elementSrc.lastIndexOf('&h');
imgArr.push({url : elementSrc.slice(start + 5, end)});
}
}
} catch(e) {
console.log('抖你妹资源获取出错->' + e);
}
return imgArr;
}
getData();
`
const dounimeiRes = await webview.evaluateJavaScript(getData, false);
dounimeiRes.forEach(element => {
imgResArr.push({ url: element.url });
});
break;
}
return imgResArr;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// --------------------------NET END--------------------------
}
// @运行测试
const { Running } = require("./lsp环境")
const newConfig = {
visualMode: true,
small: true,
medium: true,
large: true,
accessoryCircular: false,
accessoryRectangular: false,
accessoryInline: false
await new Widget(Script.name()).run();
// =================================================================================
// =================================================================================
async function downloadLSPDependency() {
let fm = FileManager.local();
const dependencyURL = `${remoteRoot}_LSP.js`;
if (isDev) {
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.
先完成此消息的编辑!
想要评论请 注册