-
Notifications
You must be signed in to change notification settings - Fork 0
/
priceReminder.js
193 lines (184 loc) · 10 KB
/
priceReminder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: orange; icon-glyph: bell;
const _info = {
name: 'priceReminder',
version: '1.1',
updated_at: '2023-12-04 17:45:00',
author: 'raoyc',
description: 'a widget to show XAUUSD/XAGUSD/Au(T+D)/Ag(T+D) realtime price',
repo_file_url: 'https://github.com/ycrao/scripts-for-scriptable/blob/main/app/priceReminder.js',
raw_file_url: 'https://raw.githubusercontent.com/ycrao/scripts-for-scriptable/main/app/priceReminder.js'
}
async function getPriceArr() {
const jsStr = `
function loadScript(url, callback) {
const script = document.createElement("script")
script.type = "application/javascript"
script.charset = "gb18030"
script.src = url
document.getElementsByTagName('head')[0].appendChild(script);
script.onload = function() {
let xauArr = hq_str_hf_XAU.split(','), xagArr = hq_str_hf_XAG.split(','), autdArr = hq_str_gds_AUTD.split(','), agtdArr = hq_str_gds_AGTD.split(',')
let priceArr = [xauArr[0], xagArr[0], autdArr[0], agtdArr[0]]
callback(priceArr)
}
}
let url = "https://hq.sinajs.cn/?rn=" + (new Date()).getTime() + "&list=hf_XAU,hf_XAG,gds_AUTD,gds_AGTD"
loadScript(url, (priceArr) => {
completion(priceArr)
})
`
const webView = new WebView()
await webView.loadHTML(``, "https://finance.sina.com.cn/nmetal/")
// webView.present()
const priceArr = await webView.evaluateJavaScript(jsStr, true)
return priceArr
}
async function getFrontCanvas(prices) {
const htmlStr = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>metal price</title>
<!--<script src="https://cdn.bootcdn.net/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>-->
</head>
<style>
@font-face {
font-family: 'ProggyTiny';
src: url('data:font/woff2;charset=utf-8;base64,d09GMgABAAAAAAOgAA4AAAAAEqwAAANLAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGhYGVgCBNAgCEQgKmziXCgs4AAE2AiQDOAQgBYJrB3YMCBtOD8iuBhxjih6pcBURt7tuuD6kYcPxibBhPDyt0d7f2TORZukODYWQTZvEHVRLo9FUYqQy8A8/7j33ix4dymuDKiNYV9qqffhzfR5wFNiAut7WF5V8AHAN4yYXq/8B9Nm/bWNP1xUURBC3uDwijhvUy+r3U5sL2sgI81SMAXY2KimTAyPiypf9fO+VQFbXKBxf4avIqCpfhRKF4zPd3ipqQE1uJUw2cWkCugEqIhmiRK+0oPpSPcFL/e7jr0lkqDvor+jxq/cbUVAe24GaQABUkGCYlWCYzSeWldjYeJS0dQ1tMdgKtbmVnZObh28RnUpaHSiPzCLH0Od+xH3PdazRUnBGCUawgG8rSqkCpoB9xDbqd7ScQkANTANAh3WcX0hYkFcwIW8dJ/187V4o9FWu5aPaooskulrLYCAJ01MhJgY9xSjEDJ5i6az/2FniyWYfZMjrOSO8L/IPXmHBWc2aIeF96Zz2Sybmx/favd8L0YFoOfcLOUNOaifAY6BqwbvXvDnocE6ZVzIwwhCwP7cgjWV+1p1jDOdAP4TQWskuCee9cnaOeA8hLHizuf1ccVfn94zsgih3cqn33ZTgf1MghHhmPOWmdiVP020q7Sln2I4/JMijfBxeOWs+ESp2ooMK0n4fD4msLtDouAunePe20ZQhWGfZw2LFfj+DmRnoQdvWYncHrNY0z1tW1wJIGnk5O2jKTH2ZcnDwS4A32fndtn8XHtfXPitwoWwUpsBOgHCN8tT5HfrA5aOZgoT7+s6b5lJTaekGYukgfA6tnfR/Gjz41+5tB95fuPWDr2/e7qCU2EA7CxBIP/4XEpAQAACRAABZZlZlL3vAlDs0UHHZRMFNBNDrXwAMQgDBJ0JAxalAQBJSJ6BuVi085vpbq86zUt0ByzYSJSmXyiLgSVmVfHhtRNnFfPU5SNPpD2/afFo/zR/0Py+D178gGVt93gWaBH5qewioedb2AqZ3aF9wPMeFD55xtXFzN3HMRg4eZgcHC1eJq5OVlQ/YOPoAyCysPOxNXAHU+I6LZSMjM0OvaWyOhVlrWTEIVzkFwRJVJFZePgERNU0d8xYsWrHP5omkMqVab9B74/rmeTklje4JGKfnni+ef4hyHwAAAA==') format('woff2');
font-weight: normal;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: 'LcdD';
src: url('data:font/woff2;charset=utf-8;base64,d09GMgABAAAAAAoUAA8AAAAAFAwAAAm+AAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGhYbgVwcKgZWAEQIMgqcSJkBCyAAATYCJAM8BCAFgwwHTgwiGxoRo6KK8wTZPw9jY1rTYTqjmOjLRU5uUHJifZPmCTOtbfW17Y/+k833pCHa5vGI1D+YsSgbG7RJgwn9VIgV6FIX0Uaz0mZdef3c526yeSF4AfwAND+HOWBpa0RlLY4/tidMhauxdcDzxNXe7j1+chhoukAd4CdIpoFEKx5m1I/ZqbVWd/F7xL205xokS+Vuf/b0DVUt6YmEJq4hEzKZloiQK5VheFUNRGtQ6x6gP4ABAIYgHYKcaBmPAUDSEQVauXwpoDyN1WWAggAANxVdHhZrGSAAvRARRKiIOdEyRCt0TAVE/7XmW2mlUv26UWeFRmcwFytnUYCCiq3gHMLfOB1w6YPPZoTsRswBpGxCTg8k3SgZGmC5D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M9wzFK/FzhPuEyW78AksDRf0SS4aHdsHZf7zXq1HNGgbF88srnnXs0GkTEa5EJCIjOCe9LlXpdpedHWK9rPKfe0hDjeLAtadXzvddt5JsUr1Om930kjXfLOLepXQ0gTwRNgBN3fflOxx+Dh/vbm4vysKnfbzfpShw61oNVsZMYk9vWt/rx/aRjXuUS7teAt72bXvtellsZ5Ot2w1eT2pqeGD/cX1Ug6Ezq8bTfDAXXPYF6l7dZKgGbDkFvdbm9RqGsErRtjqTj4UecbknJPyCxiEoN/cXCPYmOZ4XMAo8vOG3M90CWxh89NklXMnNdyAFa7ehAgACDe7DaPn/T20R+lXtEDAAB0fm8p2sj9b/oD4qHtAPAoaGUAMICw/usGiBMGVC2SW8azi6glgEdm2DoRZoN51SeLS5o/2D2AVkWAE/HHYfuB6rqlVbSHOH5pJvZtaGOqryhuEQlowsqcYiFAGAWFANnZQ4HMG4O0vSGyvjeWaTbWAe+NYzsUHZs4rETUxGyuLM4bKT57CyxfvEWp9WOJ85bUHBAYhx5zMt821OexPfqfrDSumG/OZf8sMV0QEgQhoQ2/HXFzGi0ynEaLBTJiqEEkyuWpNo6RlUVoFbLSK0wolhfKV4AJP0gkBoYY3tSoGDS0Pp9ZLnbGW4gyxiiZADerEiRVwsCV5SysqhQqV8al09C4hHBReSwIkKaDf7O5Qe13ogJMicECkcuVG3cePHnx5sOXX5tfMqGiIDK8elQaGYuFIAwGAKAVOzBz1qLCHXCuGYaAAQAA') format('woff2');
font-weight: normal;
font-style: italic;
font-display: swap;
}
body {
background-color: #1e1e1e;
}
.lcdDText {
font-family: 'LcdD';
font-weight: normal;
font-style: italic;
font-size: 24px;
color: #ff0;
}
.proggyTinyText {
font-family: 'ProggyTiny' !important;
font-weight: normal;
font-style: normal;
color: #fff;
}
rt {
font-size: 16px;
}
sub {
font-size: 14px;
}
#container {
width: 300px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-content: center;
justify-content: center;
align-items: center;
background-color: #1e1e1e;
}
.item {
width: 120px;
height: 48px;
}
</style>
<body>
<div id="container">
<div class="item">
<ruby class="lcdDText">
${prices[0]}<rt class="symbol proggyTinyText">XAUUSD</rt>
</ruby>
<sub class="unit proggyTinyText">usd/oz</sub>
</div>
<div class="item">
<ruby class="lcdDText">
${prices[1]}<rt class="symbol proggyTinyText">XAGUSD</rt>
</ruby>
<sub class="unit proggyTinyText">usd/oz</sub>
</div>
<div class="item">
<ruby class="lcdDText">
${prices[2]}<rt class="symbol proggyTinyText">Au(T+D)</rt>
</ruby>
<sub class="unit proggyTinyText">cny/g</sub>
</div>
<div class="item">
<ruby class="lcdDText">
${prices[3].replace('.00', '')}<rt class="symbol proggyTinyText">Ag(T+D)</rt>
</ruby>
<sub class="unit proggyTinyText">cny/kg</sub>
</div>
</div>
<!--
<script type="text/javascript">
html2canvas(document.querySelector("#container")).then(canvas => {
document.body.appendChild(canvas)
const dataUrl = canvas.toDataURL("image/png")
log(dataUrl)
})
</script>
-->
</body>
</html>
`
const webView = new WebView()
await webView.loadHTML(htmlStr, "https://cdn.bootcdn.net")
// await webView.present(false)
let jsStr = `
function loadScript(url, callback) {
const script = document.createElement("script")
script.type = "application/javascript"
script.src = url
document.getElementsByTagName('head')[0].appendChild(script);
script.onload = function() {
html2canvas(document.querySelector("#container")).then(canvas => {
// document.body.appendChild(canvas)
const dataUrl = canvas.toDataURL("image/png")
callback(dataUrl)
})
}
}
const scriptUrl = "https://cdn.bootcdn.net/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"
loadScript(scriptUrl, (dataUrl) => {
completion(dataUrl)
})
`
const url = await webView.evaluateJavaScript(jsStr, true)
console.log(url)
return url
}
const priceArr = await getPriceArr()
const imageStr = await getFrontCanvas(priceArr)
const imageBase64Str = imageStr.replace("data:image/png;base64,", "")
const widget = new ListWidget()
widget.backgroundColor = new Color('#1e1e1e', 1)
widget.setPadding(10, 10, 10, 10)
const title = widget.addText("PriceReminder")
title.font = Font.ultraLightRoundedSystemFont(20)
title.textColor = new Color("#f7e01e", 1)
title.centerAlignText()
widget.addSpacer(20)
const pricePic = Image.fromData(Data.fromBase64String(imageBase64Str))
const img = widget.addImage(pricePic)
img.imageSize = new Size(300, 96)
if (config.runsInApp) {
widget.presentLarge()
}
Script.setWidget(widget)
Script.complete()