diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 8d0b49b..cd45377 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -25,29 +25,17 @@ jobs: with: # 选择要使用的 node 版本 node-version: 20 - - # 设置密钥 - # Run the set secrets script - - name: Set secrets - env: - WEATHER_KEY: ${{ secrets.WEATHER_KEY }} - run: | - touch key.js - echo "export const WEATHER_KEY = '$WEATHER_KEY';" >> key.js # 安装依赖 # Install dependencies - name: Install Dependencies run: npm ci - - # 运行构建脚本 # Run the build script - name: Build site run: npm run build - # 查看 workflow 的文档来获取更多信息 # @see https://github.com/crazy-max/ghaction-github-pages - name: Deploy to GitHub Pages diff --git a/src/api/jsonp.js b/src/api/jsonp.js new file mode 100644 index 0000000..f6d3f83 --- /dev/null +++ b/src/api/jsonp.js @@ -0,0 +1,42 @@ +import { printLog } from '../utils/common' +export function createJsonp(url, callbackName, params) { + return new Promise((resolve, reject) => { + // Generate a unique ID for the script tag + const id = `jsonp_${Date.now()}_${Math.random().toString(36).substr(2, 5)}`; + // Define the callback function name + const uniqueCallbackName = `${callbackName}_${id}`; + + // Add the callback function to the window object + window[uniqueCallbackName] = (response) => { + resolve(response); + // Remove the script tag and the callback from the window object + document.body.removeChild(script); + delete window[uniqueCallbackName]; + }; + + // Construct the URL with the callback parameter + let finalUrl = url + (url.includes('?') ? '&' : '?') + 'callback=' + uniqueCallbackName; + Object.keys(params).forEach(key => { + finalUrl += '&' + key + '=' + encodeURIComponent(params[key]); + }); + + // Create a script element and append it to the document + const script = document.createElement('script'); + script.src = finalUrl; + script.id = id; + document.body.appendChild(script); + }); +} +export function useJsonpRequest(url, callbackName, params) { + const fetchJsonp = async () => { + try { + const response = await createJsonp(url, callbackName, params); + return response; + } catch (error) { + printLog('error', 'Error fetching JSONP:', error); + throw error; + } + }; + + return { fetchJsonp }; +} \ No newline at end of file diff --git a/src/api/search.js b/src/api/search.js index 01c77c8..efa6a57 100644 --- a/src/api/search.js +++ b/src/api/search.js @@ -1,39 +1,28 @@ -import axios from "axios" +import { useJsonpRequest } from './jsonp.js' -let searchRequestCancel = null let suggestResult = [] -export function getSearchSuggest(keyword) { +export const getSearchSuggest = async (keyword) => { return new Promise((resolve, reject) => { - if (searchRequestCancel !== null) { - searchRequestCancel(); - } - axios.get('/sug', { - params: { - prod: "pc", - wd: keyword - }, - cancelToken: new axios.CancelToken((cancel) => { - // 这里cancel就是取消当前请求的方法 - searchRequestCancel = cancel; - }) - }).then(res => { + const { fetchJsonp } = useJsonpRequest( + 'https://www.baidu.com/sugrec', // 确保这个 URL 支持 JSONP + 'jsonpCallback', // 这里指定的前缀会被用来生成回调函数名称 + { prod: "pc", wd: keyword } + ); + + fetchJsonp().then(data => { suggestResult.splice(0, suggestResult.length); - if (res.data.g !== undefined) { - res.data.g.forEach(element => { + if (data.g !== undefined) { + data.g.forEach(element => { suggestResult.push(element.q) }); } - resolve(suggestResult); + resolve(suggestResult) }).catch(err => { - if (axios.isCancel(err)) { - // 请求被取消 - } else { - reject(err.data); - } + reject(err.data); }) }) -} +}; export function doSearch(url, value, mode) { if (mode === 'current') { diff --git a/src/components/SettingItem.vue b/src/components/SettingItem.vue index 05d812c..a3fd75c 100644 --- a/src/components/SettingItem.vue +++ b/src/components/SettingItem.vue @@ -15,6 +15,7 @@ const props = defineProps({ checked: Boolean, inputValue: String, textValue: String, + disabled: Boolean, }) const settingInputRef = ref() @@ -40,7 +41,7 @@ function ensureSettingInput() {