跨域测试小工具

2022-06-26 17:31:59 +08:00
 estk

https://blockchain-tools.pages.dev/#/cors-checker

截图 workers-dev 出自:/t/862194

import { Box, TextField, Button, Typography } from "@mui/material"
import { useState } from "react"
import CircularProgress from '@mui/material/CircularProgress'

export default function CorsChecker() {
  const [inputValue, setInputValue] = useState('')
  const [urlsInfo, setUrlsInfo] = useState<{url: string, cros: string}[]>([])
  const [checking, setChecking] = useState(false)
  async function check() {
    let _urlsInfo = []
    setChecking(true)
    setUrlsInfo([])
    for (const url of inputValue.split('\n')) {
      if (!url.trim() || _urlsInfo.filter(urlInfo=>{return urlInfo.url===url.trim()}).length>0 ) continue
      let urlInfo = {url: url.trim(), cros: '❔'}
      let res
      try {
        res = await fetch(url.trim())
      } catch(e) {}
      if ('cors'===res?.type) {
        urlInfo.cros = '✅'
      } else {
        urlInfo.cros = '❌'
      }
      _urlsInfo.push(urlInfo)
    }
    setChecking(false)
    setUrlsInfo(_urlsInfo)
  }
  return (
    <Box display='flex' alignItems='center' justifyContent='center' minHeight='62vh'>
      <Box maxWidth='800px' width='100%'>
        <Typography variant="h3" component="div" gutterBottom>
          CORS Checker
        </Typography>
        <TextField
          fullWidth
          label="One url per line"
          multiline
          rows={8}
          value={inputValue}
          onChange={(e)=>setInputValue(e.target.value)}
        />
        <Button onClick={()=>check()} disabled={checking} variant="contained" className="my-4">
          {checking&&<CircularProgress color='inherit' size='1rem' value={50} className='mr-2' />}
          {checking?'Checking':'Check'}
        </Button>
        {urlsInfo.map(urlInfo=>(
          <div key={urlInfo.url+urlInfo.cros}>{urlInfo.cros} {urlInfo.url}</div>
        ))}
      </Box>
    </Box>
  )
}
757 次点击
所在节点    前端开发
1 条回复
liushuigs
2022-06-26 18:36:30 +08:00
谢谢分享,也许后面用得上。我稍做修改做了一个可线上演示的 demo: https://runjs.qingting.work/#/projects/deb7d696c41741e7

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/862301

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX