-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.svelte
241 lines (205 loc) · 6.87 KB
/
popup.svelte
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<script lang="ts">
import { fetch as crossFetch } from "cross-fetch"
import { default as fetchr } from "fetch-retry"
import type { IChartApi, SingleValueData } from "lightweight-charts"
import { type PopupPrices, getDefaultPopupPrices } from "~model/popup-prices"
import { PopupState } from "~model/popup-state"
import { PriceStatus } from "~model/price-status"
import { convertToChartData } from "~utils/chart"
import { findMedian, findMin, findMinMax } from "~utils/math"
import LoadingView from "~views/loading-view.svelte"
import NoDataView from "~views/no-data-view.svelte"
import PriceDataView from "~views/price-data-view.svelte"
import RequestPermissionsView from "~views/request-permissions-view.svelte"
const allowedHosts = ["shopee.vn", "tiki.vn", "www.lazada.vn"]
const retryableFetch = fetchr(crossFetch)
const fetchRetryOption = {
retries: 5,
retryDelay: 800,
retryOn: [503, 504]
}
let currentTabUrl = ""
let currentPopupState: PopupState = PopupState.Loading
let popupProductName = ""
let priceHistoryRecords = []
let chartApi: IChartApi
let lineSeriesData: SingleValueData[] = []
let popupPrices: PopupPrices = getDefaultPopupPrices()
let priceStatus = PriceStatus.NoChange
const loadCurrentTab = (tabs) => {
currentTabUrl = tabs[0].url
loadCurrentProductUrl()
}
const onError = (error) => {
console.error(`Error: ${error}`)
}
const loadCurrentProductUrl = async () => {
const allowQuery =
currentTabUrl &&
allowedHosts.some((allowedHost) => currentTabUrl.includes(allowedHost))
if (!allowQuery) {
currentPopupState = PopupState.UnsupportedPage
return
}
const currentPermissions = await browser.permissions.getAll()
if (
!currentPermissions.origins ||
currentPermissions.origins.length === 0
) {
currentPopupState = PopupState.NeedPermissions
return
}
await loadProductData()
}
const loadProductData = async () => {
const urlSrc = `https://apiv3.beecost.vn/search/product?product_url=${encodeURIComponent(
currentTabUrl
)}`
const response = await retryableFetch(urlSrc, fetchRetryOption)
if (!response.ok) {
currentPopupState = PopupState.NoData
return
}
var data = await response.json()
loadProductPage(data)
}
const loadProductPage = async (product) => {
if (product.status === "error") {
currentPopupState = PopupState.NoData
return
}
const {
data: {
product_base: {
product_base_id: productId,
price: productPrice,
name: productName
}
}
} = product
if (!productName || !productPrice) {
currentPopupState = PopupState.NoData
return
}
const priceHistoryApi = `https://apiv3.beecost.vn/product/history_price?product_base_id=${productId}&price_current=${productPrice}`
const priceHistoryResponse = await retryableFetch(
priceHistoryApi,
fetchRetryOption
)
const priceHistoryJson = await priceHistoryResponse.json()
const {
data: {
product_history_data: {
item_history: { price: prices, price_ts: timestamps }
}
}
} = priceHistoryJson
createChartElement(prices, timestamps, productPrice)
const timePriceZip = timestamps.map((e, i) => [e, prices[i]])
createLowestPricesTable(prices, timePriceZip, productPrice)
popupProductName = productName
currentPopupState = PopupState.HaveData
}
const createChartElement = (
prices: number[],
timestamps: number[],
productPrice: number
) => {
lineSeriesData = convertToChartData(prices, timestamps)
const { foundMinPrice, foundMaxPrice } = findMinMax(prices)
popupPrices.lowest = foundMinPrice
popupPrices.current = productPrice
popupPrices.highest = foundMaxPrice
chartApi?.timeScale()?.fitContent()
}
const createLowestPricesTable = (prices, timePriceZip, currentPrice) => {
let minimumPrice = findMin(prices)
let medianPrice = findMedian(prices)
if (minimumPrice !== medianPrice) {
const minMedianDiff = medianPrice - minimumPrice
let allowedOffset = minMedianDiff * 0.3
const toPrice = minimumPrice + allowedOffset
const datePriceMap = new Map()
for (let i = 1; i < timePriceZip.length; i++) {
const time = timePriceZip[i][0]
const price = timePriceZip[i][1]
if (price >= minimumPrice && price <= toPrice) {
const date = new Date(time)
const day = date.getDate().toString().padStart(2, "0")
const month = (date.getMonth() + 1).toString().padStart(2, "0")
const year = date.getFullYear()
const key = `${day}-${month}-${year}`
if (datePriceMap.has(key)) {
const keyPrice = datePriceMap.get(key)
if (price < keyPrice) {
datePriceMap.set(key, price)
}
} else {
datePriceMap.set(key, price)
}
}
}
if (datePriceMap.size > 0) {
const reversedMap = new Map(Array.from(datePriceMap).reverse())
const currentDate = new Date()
let cnt = 0
for (const [date, price] of reversedMap) {
if (cnt == 5) {
break
}
const splittedDateParts = date.split("-")
const saleDate = new Date(
Number(splittedDateParts[2]),
Number(splittedDateParts[1] - 1),
Number(splittedDateParts[0])
)
const difference = currentDate.getTime() - saleDate.getTime()
const daysAgo = Math.ceil(difference / (1000 * 3600 * 24))
const discounted = Math.ceil(
((currentPrice - price) / currentPrice) * 100
)
priceHistoryRecords.push({
daysAgo,
date,
price,
discounted
})
cnt += 1
}
}
}
if (currentPrice > medianPrice) {
priceStatus = PriceStatus.NowHigh
} else if (currentPrice < medianPrice) {
priceStatus = PriceStatus.NowLow
}
}
const handlePermissionsResult = (event) => {
const allowedState: boolean = event.detail.state
if (allowedState) {
loadCurrentProductUrl()
}
}
browser.tabs
.query({ currentWindow: true, active: true })
.then(loadCurrentTab, onError)
</script>
{#if currentPopupState === PopupState.Loading}
<LoadingView />
{:else if currentPopupState === PopupState.NeedPermissions}
<RequestPermissionsView on:permissionsResult={handlePermissionsResult} />
{:else if currentPopupState === PopupState.UnsupportedPage || currentPopupState === PopupState.NoData}
<NoDataView popupState={currentPopupState} />
{:else}
<PriceDataView
{popupProductName}
{popupPrices}
{priceStatus}
{lineSeriesData}
{priceHistoryRecords} />
{/if}
<style lang="postcss">
@tailwind base;
@tailwind components;
@tailwind utilities;
</style>