forked from githubdulong/Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcosia_x.scriptable
More file actions
12 lines (11 loc) · 4.69 KB
/
Copy pathEcosia_x.scriptable
File metadata and controls
12 lines (11 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
{
"always_run_in_app" : false,
"icon" : {
"color" : "deep-green",
"glyph" : "tree"
},
"name" : "Ecosia_x",
"script" : "\/\/ Scriptable使用的变量;\n\/\/ Ecosia是一个总部位于德国柏林的搜索引擎,它们将至少80%的利润捐赠给非营利组织,用于种植树木。该组件用于显示账户植树数据;\n\/\/ 可搭配快捷搜索脚本设置为默认搜索引擎体验更佳 https:\/\/raw.githubusercontent.com\/githubdulong\/Script\/master\/Surge\/Q-Search_All_in_one.sgmodule;\n\n\/\/ 1.1.4版本 \"修改汉化自 https:\/\/www.scriptables.net\";\n\n\/\/ 第一步:先登陆 https:\/\/www.ecosia.org 再通过Safari共享菜单使用快捷方式获取令牌:https:\/\/www.icloud.com\/shortcuts\/8e3d009ee2a94c2d99d3cdb02f3e16d9;\n\n\/\/ 第二步:将获取到的令牌填入 Scriptable > Parameter;\n\nlet widgetInputRAW = args.widgetParameter;\n\nlet token;\nif (widgetInputRAW !== null) {\n token = widgetInputRAW.toString().trim();\n if (\/^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$\/.test(token) === false) {\t \n throw new Errpr('令牌格式无效')\n }\n} else {\n token = \"40fd8a86-897a-11ec-a2b8-8fc2ec8aa88b\";\n console.log('没有通过小部件参数设置令牌,将使用默认令牌进行展示。\\n')\n \/\/throw new Error('没有通过小部件参数设置令牌!您可以在此处请求令牌:https:\/\/www.ecosia.org\/account\/login')\n}\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n\/\/主题颜色设定\nconst themes = {\n light: {\n background: new Color('#229954', 1),\/\/明亮背景\n textColor: new Color('#fff', 0.9),\/\/明亮文字,蒙层透明度\n color: Color.black()\n },\n dark: {\n background: new Color('#282828', 1),\/\/暗黑背景\n textColor: new Color('#fff', 0.8),\/\/暗黑文字,蒙层透明度\n color: Color.white()\n }\n};\n\n\/\/读取主题\nconst conf = {\n client: 'h5',\n theme: 'system'\n};\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\nasync function getTreeCounter(token) {\n try {\n const url = \"https:\/\/api.ecosia.org\/v1\/accounts\/v2\/personal_counter?token=\" + token\n let req = new Request(url)\n let res = await req.loadJSON()\n return parseInt(res.personal_counter)\n } catch (error) {\n throw new Error('An error occurred when loading data from the Ecosia API. Please check the entered token.')\n }\n}\n\nasync function getLogo() {\n let fm = FileManager.local()\n const pathLogo = fm.joinPath(fm.temporaryDirectory(), 'ecosiaLogo')\n\n if (fm.fileExists(pathLogo)) {\n return fm.readImage(pathLogo)\n } else {\n try {\n let req = new Request('https:\/\/raw.githubusercontent.com\/ThisIsBenny\/iOS-Widgets\/main\/Ecosia\/logo.png') \/\/logo\n let logo = await req.loadImage()\n fm.writeImage(pathLogo, logo)\n return logo\n } catch (e) {\n console.error(e)\n return null\n }\n }\n}\nconsole.log('Load Tree Counter')\nlet treeCounter = await getTreeCounter(token);\nconsole.log('Tree Counter loaded')\nconsole.log(treeCounter)\nconsole.log('Load Logo')\nlet ecosiaLogo = await getLogo()\nconsole.log('Logo loaded')\n\/\/ Create Widget\nlet widget = new ListWidget();\nwidget.url = 'https:\/\/ecosia.org\/'\nwidget.setPadding(10, 10, 10, 10)\n\nif (ecosiaLogo !== null) {\n let titleLogo = widget.addImage(ecosiaLogo)\n titleLogo.imageSize = new Size(50, 50)\n titleLogo.rightAlignImage()\n if (config.widgetFamily === 'large') {\n widget.addSpacer()\n } else {\n widget.addSpacer(5)\n }\n} else {\n let title = widget.addText(\"Ecosia\")\n title.font = Font.mediumSystemFont(12)\n title.textColor = new Color(textColor)\n widget.addSpacer()\n}\n\nlet counterText = widget.addText(`${treeCounter.toLocaleString(Device.locale().replace('_', '-'))} 🌲`) \/\/🌳🌴\ncounterText.font = Font.regularSystemFont(36)\ncounterText.minimumScaleFactor = 0.7;\ncounterText.lineLimit = 1\ncounterText.centerAlignText()\n\n\/\/定义背景颜色\nwidget.backgroundColor = conf.theme === 'system'\n ? Color.dynamic(themes.light.background, themes.dark.background)\n : themes[conf.theme].background;\n\n\/\/定义字体颜色\ncounterText.textColor = conf.theme === 'system'\n ? Color.dynamic(themes.light.textColor, themes.dark.textColor)\n : themes[conf.theme].color;\n\nwidget.addSpacer()\n\nif (!config.runsInWidget) {\n await widget.presentSmall()\n} else {\n\/\/ Tell the system to show the widget.\n Script.setWidget(widget)\n Script.complete()\n}\n",
"share_sheet_inputs" : [
]
}