forked from githubdulong/Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeibo.scriptable
More file actions
12 lines (11 loc) · 11.3 KB
/
Copy pathWeibo.scriptable
File metadata and controls
12 lines (11 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
{
"always_run_in_app" : false,
"icon" : {
"color" : "red",
"glyph" : "at"
},
"name" : "Weibo",
"script" : "\/\/ Scriptable使用的变量。\n\/\/ 这些必须在文件的顶部。不要编辑。\n\/\/ 图标字形:@;图标颜色:粉红色;\n\/**\n * 微博上的热门搜索\n *\n * @版本 1.0.3\n * @作者 Honye\n *\/\n\n\/**\n * @Param {number} [高度] 以像素为单位的屏幕高度\n *\/\nconst phoneSize = (height) => {\n const phones = {\n \/** 12 Pro Max *\/\n 2778: {\n small: 510,\n medium: 1092,\n large: 1146,\n left: 96,\n right: 678,\n top: 246,\n middle: 882,\n bottom: 1518\n },\n \/** 12 and 12 Pro *\/\n 2532: {\n small: 474,\n medium: 1014,\n large: 1062,\n left: 78,\n right: 618,\n top: 231,\n middle: 819,\n bottom: 1407\n },\n \/** 11 Pro Max, XS Max *\/\n 2688: {\n small: 507,\n medium: 1080,\n large: 1137,\n left: 81,\n right: 654,\n top: 228,\n middle: 858,\n bottom: 1488\n },\n \/** 11, XR *\/\n 1792: {\n small: 338,\n medium: 720,\n large: 758,\n left: 55,\n right: 437,\n top: 159,\n middle: 579,\n bottom: 999\n },\n \/** 11 Pro, XS, X, 12 mini *\/\n 2436: {\n small: 465,\n medium: 987,\n large: 1035,\n x: {\n left: 69,\n right: 591,\n top: 213,\n middle: 783,\n bottom: 1353\n },\n mini: {\n left: 69,\n right: 591,\n top: 231,\n middle: 801,\n bottom: 1371\n }\n },\n \/** Plus phones *\/\n 2208: {\n small: 471,\n medium: 1044,\n large: 1071,\n left: 99,\n right: 672,\n top: 114,\n middle: 696,\n bottom: 1278\n },\n \/** SE2 and 6\/6S\/7\/8 *\/\n 1334: {\n small: 296,\n medium: 642,\n large: 648,\n left: 54,\n right: 400,\n top: 60,\n middle: 412,\n bottom: 764\n },\n \/** SE1 *\/\n 1136: {\n small: 282,\n medium: 584,\n large: 622,\n left: 30,\n right: 332,\n top: 59,\n middle: 399,\n bottom: 399\n },\n \/** 11 and XR in Display Zoom mode *\/\n 1624: {\n small: 310,\n medium: 658,\n large: 690,\n left: 46,\n right: 394,\n top: 142,\n middle: 522,\n bottom: 902\n },\n \/** Plus in Display Zoom mode *\/\n 2001: {\n small: 444,\n medium: 963,\n large: 972,\n left: 81,\n right: 600,\n top: 90,\n middle: 618,\n bottom: 1146\n }\n };\n height = height || Device.screenResolution().height;\n const scale = Device.screenScale();\n\n if (config.runsInWidget) {\n const phone = phones[height];\n if (phone) {\n return phone\n }\n\n const pc = {\n small: 164 * scale,\n medium: 344 * scale,\n large: 354 * scale\n };\n return pc\n }\n\n \/\/ in app screen fixed 375x812 pt\n return {\n small: 155 * scale,\n medium: 329 * scale,\n large: 345 * scale\n }\n};\n\nconst fontSize = 13;\/\/文字大小\nconst gap = 7;\/\/行距\nconst logoSize = 25;\/\/logo大小\nconst paddingVertical = 10;\/\/顶边距\nconst themes = {\n light: {\n background: new Color('#FFFFFF'),\/\/明亮背景\n color: Color.black()\n },\n dark: {\n background: new Color('#282828', 1),\/\/暗黑背景\n color: Color.white()\n }\n};\n\n\/** Scoped Keychain *\/\nconst KeyStorage = {\n set: (key, value) => {\n return Keychain.set(`$${Script.name()}.${key}`, JSON.stringify(value))\n },\n get: (key) => {\n const _key = `$${Script.name()}.${key}`;\n if (Keychain.contains(_key)) {\n return JSON.parse(Keychain.get(_key))\n }\n }\n};\n\n\/**\n * @param {object} options\n * @param {string} [options.title]\n * @param {string} [options.message]\n * @param {Array<{ title: string; [key: string]: any }>} options.options\n * @param {string} [options.cancelText = 'Cancel']\n *\/\nconst presentSheet = async (options) => {\n options = {\n showCancel: true,\n cancelText: '取消操作',\n ...options\n };\n const alert = new Alert();\n if (options.title) {\n alert.title = options.title;\n }\n if (options.message) {\n alert.message = options.message;\n }\n if (!options.options) {\n throw new Error('参数的\"选项\"属性不能为空')\n }\n for (const option of options.options) {\n alert.addAction(option.title);\n }\n if (options.showCancel) {\n alert.addCancelAction(options.cancelText);\n }\n const value = await alert.presentSheet();\n return { value, option: options.options[value] }\n};\n\n\/** 微博国际版页面 *\/\nconst InternationalScheme = {\n hotSearch: () => 'weibointernational:\/\/hotsearch',\n search: (keyword) => `weibointernational:\/\/search?keyword=${encodeURIComponent(keyword)}`\n};\n\n\/** 微博 H5 应用页面 *\/\nconst H5Page = {\n hotSearch: () => `https:\/\/m.weibo.cn\/p\/index?containerid=${encodeURIComponent('106003&filter_type=realtimehot')}`,\n search: (keyword) => `https:\/\/m.weibo.cn\/search?containerid=${encodeURIComponent('100103type=1&t=10&q=' + keyword)}`\n};\n\nconst conf = {\n client: 'h5',\n theme: 'system'\n};\nconst screen = Device.screenResolution();\nconst scale = Device.screenScale();\nconst phone = phoneSize(screen.height);\nlet widgetFamily = 'medium';\nif (config.runsInWidget) {\n widgetFamily = config.widgetFamily;\n const [client, theme] = (args.widgetParameter || '').split(',').map(text => text.trim());\n conf.client = client === '2' ? 'international' : conf.client;\n conf.theme = theme || conf.theme;\n}\nconst height = (widgetFamily === 'medium' ? phone.small : phone[widgetFamily]) \/ scale;\nconf.count = Math.floor((height - paddingVertical * 2 + gap) \/ (fontSize + gap));\nconst storedClient = KeyStorage.get('client');\nif (storedClient) {\n conf.client = storedClient;\n}\n\nconst Pages = (() => {\n switch (conf.client) {\n case 'international':\n return InternationalScheme\n case 'h5':\n return H5Page\n }\n})();\n\nconst dateFormat = new DateFormatter();\ndateFormat.dateFormat = 'HH:mm';\nconst timeString = dateFormat.string(new Date());\n\nconst main = async () => {\n const url = 'https:\/\/weibointl.api.weibo.cn\/portal.php?ct=feed&a=search_topic';\n const request = new Request(url);\n const data = await request.loadJSON();\n const widget = await createWidget(data);\n return widget\n};\n\nlet stackBottom;\nlet widgetBottom;\nconst createWidget = async (data) => {\n const widget = new ListWidget();\n widget.backgroundColor = conf.theme === 'system'\n ? Color.dynamic(themes.light.background, themes.dark.background)\n : themes[conf.theme].background;\n widget.url = Pages.hotSearch();\n const paddingY = paddingVertical - (gap \/ 2);\n widget.setPadding(paddingY, 12, paddingY, 14);\n const max = conf.count;\n const logoLines = Math.ceil((logoSize + gap) \/ (fontSize + gap));\n for (let i = 0; i < max; ++i) {\n const item = data.data[i];\n if (i === 0) {\n const stack = widget.addStack();\n await addItem(stack, item);\n stack.addSpacer();\n const textTime = stack.addText(`更新于 ${timeString}`);\n textTime.font = Font.systemFont(10);\n textTime.textColor = new Color('#888888');\/\/更新文字颜色\n } else if (i < max - logoLines) {\n await addItem(widget, item);\n } else {\n if (!widgetBottom) {\n stackBottom = widget.addStack();\n stackBottom.bottomAlignContent();\n widgetBottom = stackBottom.addStack();\n widgetBottom.layoutVertically();\n addItem(widgetBottom, item);\n } else {\n await addItem(widgetBottom, item);\n }\n widgetBottom.length = (widgetBottom.length || 0) + 1;\n if (widgetBottom.length === logoLines) {\n stackBottom.addSpacer();\n const imageLogo = stackBottom.addImage(await getImage('https:\/\/www.sinaimg.cn\/blog\/developer\/wiki\/LOGO_64x64.png'));\n imageLogo.imageSize = new Size(logoSize, logoSize);\n }\n }\n }\n return widget\n};\n\nconst addItem = async (widget, item) => {\n const stack = widget.addStack();\n const [, queryString] = item.scheme.split('?');\n const query = {};\n queryString.split('&').forEach((item) => {\n const [key, value] = item.split('=');\n query[key] = value;\n });\n stack.url = Pages.search(query.keyword);\n stack.centerAlignContent();\n stack.size = new Size(-1, fontSize + gap);\n const stackIndex = stack.addStack();\n stackIndex.size = new Size(fontSize * 1.4, -1);\n const textIndex = stackIndex.addText(String(item.pic_id));\n textIndex.rightAlignText();\n textIndex.textColor = item.pic_id > 3 ? new Color('#FF9400', 0.7) : new Color('#ED402E', 0.7);\/\/排序文字颜色 后+前\n textIndex.font = Font.boldSystemFont(fontSize);\n stack.addSpacer(4);\n const textTitle = stack.addText(item.title);\n textTitle.font = Font.systemFont(fontSize);\n textTitle.textColor = conf.theme === 'system'\n ? Color.dynamic(new Color('#000000', 0.7), new Color('#FFFFFF', 0.8))\/\/文字颜色蒙版\n : themes[conf.theme].color;\n textTitle.lineLimit = 1;\n textTitle.shadowColor = new Color('#333333', 0.2);\n textTitle.shadowOffset = new Point(0.5, 0.5);\n textTitle.shadowRadius = 0;\n if (item.icon) {\n stack.addSpacer(4);\n const imageIcon = stack.addImage(await getImage(item.icon));\n imageIcon.imageSize = new Size(12, 12);\n }\n stack.addSpacer();\n};\n\nconst getImage = async (url) => {\n const request = new Request(url);\n const image = await request.loadImage();\n return image\n};\n\n\/** 更新脚本 *\/\nconst update = async () => {\n let fm = FileManager.local();\n if (fm.isFileStoredIniCloud(module.filename)) {\n fm = FileManager.iCloud();\n }\n const url = 'https:\/\/raw.githubusercontent.com\/Honye\/scriptable-scripts\/master\/dist\/Weibo.js';\n const request = new Request(url);\n try {\n const code = await request.loadString();\n fm.writeString(module.filename, code);\n const alert = new Alert();\n alert.message = '代码已更新。如果脚本处于打开状态,请关闭它以使更改生效。';\n alert.addAction('确定');\n alert.presentAlert();\n } catch (e) {\n console.error(e);\n }\n};\n\n\/** Settings *\/\nconst settings = async () => {\n const res = await presentSheet({\n title: '设置',\n message: '使用哪个客户端查看详细信息',\n options: [\n { title: '微博国际版', value: 'international' },\n { title: '微博客户端', value: 'h5' }\n ]\n });\n const client = res.option?.value || conf.client;\n KeyStorage.set('client', client);\n conf.client = client;\n};\n\nif (config.runsInApp) {\n const res = await presentSheet({\n message: '预览小组件或更新脚本,更新将覆盖整个脚本;',\n options: [\n { title: '预览组件', value: 'Preview' },\n { title: '设置查看', value: 'Settings' },\n { title: '更新脚本', value: 'Update' }\n ]\n });\n const value = res.option?.value;\n switch (value) {\n case 'Preview':\n break\n case 'Settings':\n await settings();\n break\n case 'Update':\n update();\n break\n }\n}\n\nconst widget = await main();\nif (config.runsInApp) {\n widget.presentMedium();\n}\nScript.setWidget(widget);\nScript.complete();\n",
"share_sheet_inputs" : [
]
}