Skip to content

Commit

Permalink
feat: use fixed server ip
Browse files Browse the repository at this point in the history
  • Loading branch information
me1ting committed Apr 11, 2024
1 parent 7f67f66 commit 0dc669c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 92 deletions.
124 changes: 34 additions & 90 deletions public/export_wroker.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,59 @@
const QUERY_SERVER_URL = 'https://poe.pathof.top/export/server'
const EXPORT_SERVER = 'http://120.77.179.194:8888'
const HTTP_TIMEOUT = 3000

async function getServer() {
const { exportServer } = await chrome.storage.local.get('exportServer')
return exportServer
}

async function setServer(server) {
return chrome.storage.local.set({ exportServer: server })
}
async function transform(compressed, sendResponse) {
console.log(`fetching ${EXPORT_SERVER}/transform`)
let res = null

async function checkServer() {
console.log(`checking server status`)
const server = await getServer()
if (!server || server.length === 0) {
console.log(`no available server in storage`)
return false
}
try {
console.log(`checking server ${server}`)
const res = await fetch(`${server}/status`)
if (res.status !== 200) {
console.log(`server ${server} returns ${res.status}`)
return false
}
const json = await res.json()
if (!json.code || json.code !== 200) {
console.log(`server ${server} returns ${json}`)
return false
}
} catch (err) {
console.log(err)
return false
}
console.log(`server ${server} works well`)
return true
}
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), HTTP_TIMEOUT)

async function loadServer() {
try {
const res = await fetch(QUERY_SERVER_URL)
if (res.status !== 200) {
console.log(`load server failed: ${res.status}`)
return
}
const server = await res.text()
console.log(`loaded server ${server}`)
await setServer(server)
res = await fetch(`${EXPORT_SERVER}/transform`, {
method: 'post',
headers: {
'Content-type': 'octet-stream'
},
body: compressed,
signal: controller.signal
})
} catch (err) {
console.log(err)
}
}

async function onLoad() {
const ok = await checkServer()
if (!ok) {
console.log('reloading server')
loadServer()
}
}

async function transform(compressed, sendResponse) {
const server = await getServer()
if (!server || server.length === 0) {
sendResponse({
code: 1,
msg: '无可用Export服务器,刷新重试或联系开发者'
msg: `无法访问Export服务器,刷新重试或联系开发者`
})
return
}finally{
clearTimeout(timeoutId)
}
try {
console.log(`fetching ${server}/transform`)
const res = await fetch(`${server}/transform`, {
method: 'post',
headers: {
'Content-type': 'octet-stream'
},
body: compressed
})
if (res.status !== 200) {
sendResponse({
code: 1,
msg: `导出错误: ${res.status}`
})
return
}
const apiResp = await res.json()
const code = apiResp.code
if (code !== 200) {
sendResponse({
code: 1,
msg: `导出错误: ${apiResp.msg}`
})
return
}

if (res.status !== 200) {
sendResponse({
code: 0,
data: apiResp.data.xml
code: 1,
msg: `导出错误: ${res.status}`
})
} catch (err) {
console.log(err)
return
}
const apiResp = await res.json()
const code = apiResp.code
if (code !== 200) {
sendResponse({
code: 1,
msg: `${err}`
msg: `导出错误: ${apiResp.msg}`
})
return
}

sendResponse({
code: 0,
data: apiResp.data.xml
})
}

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.task === 'load') {
onLoad()
//加载逻辑已移除,因此目前不做任何事情
} else if (request.task === 'transform') {
const data = new Uint8Array(request.compressed)
transform(data, sendResponse)
Expand Down
4 changes: 2 additions & 2 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "poexport",
"name": "PoExport",
"description": "国服POE导出工具",
"version": "1.2.0",
"version": "1.2.1",
"manifest_version": 3,
"host_permissions": ["https://*.poe.game.qq.com/"],
"permissions": ["clipboardWrite", "cookies", "storage"],
Expand Down

0 comments on commit 0dc669c

Please sign in to comment.