最近金价波动剧烈,要是能一边写代码,一边实时监控金价变动,就不会错过高低点了!
在 C:\Users\你的用户名\.vscode\extensions
新建文件夹 gold-monitor
,在文件夹中创建两个文件 package.json
和 extension.js
。
package.json1 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
| { "name": "jd-gold-price-monitor", "displayName": "JD Gold Price Monitor", "description": "Monitor gold price from JD", "version": "0.0.3", "author": "iMaeGoo", "publisher": "iMaeGoo", "engines": { "vscode": "^1.85.0" }, "categories": [ "Other" ], "activationEvents": [ "onStartupFinished" ], "main": "./extension.js", "contributes": { "commands": [ { "command": "gold-price-monitor.start", "title": "Start Gold Price Monitor" } ] } }
|
extension.js1 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
| const vscode = require("vscode");
let statusBarItem; let intervalId;
function activate(context) { statusBarItem = vscode.window.createStatusBarItem( vscode.StatusBarAlignment.Left, -999 ); context.subscriptions.push(statusBarItem);
let disposable = vscode.commands.registerCommand( "gold-price-monitor.start", () => { startMonitoring(); } );
context.subscriptions.push(disposable);
startMonitoring(); }
async function updateGoldPrice() { try { const price = await getPrice(); statusBarItem.text = price; statusBarItem.show(); } catch (error) { console.error("获取价格失败", error); statusBarItem.text = error.message; statusBarItem.show(); } }
async function getPrice() { const response = await fetch( "https://api.jdjygold.com/gw/generic/hj/h5/m/latestPrice?reqData={}" ); const data = await response.json(); const price = data.resultData.datas.price; return price; }
function startMonitoring() { if (intervalId) { clearInterval(intervalId); }
updateGoldPrice();
intervalId = setInterval(updateGoldPrice, 6666); }
function deactivate() { if (intervalId) { clearInterval(intervalId); } }
module.exports = { activate, deactivate, };
|
重新启动 VS Code(Ctrl + Shift + P,输入 reload window,回车)即可看到效果。

如果你想监测其他品牌金价,可以修改 getPrice
方法,具体实现如下。
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
| async function zheshang() { const response = await fetch( "https://api.jdjygold.com/gw2/generic/jrm/h5/m/stdLatestPrice?productSku=1961543816" ); const data = await response.json(); const price = data.resultData.datas.price; return price; }
async function zhoushengsheng() { try { const res = await fetch("https://cn.chowsangsang.com/gold-info"); const data = await res.text(); const gold_data_match = data.match(/:gold_data='(.*)'/); const gold_data = gold_data_match ? JSON.parse(decode(gold_data_match[1])) : []; const price = gold_data.find( (i: any) => i.description === "黄金金价" ).price; return price; } catch (e) { console.error("zhoushengsheng", e); } }
async function laofengxiang() { const res = await fetch("http://lfx1848.com"); const data = await res.text(); const price_match = data.match(/<span id="labContent">(.*)<\/span>/); const price = price_match ? price_match[1] : ""; return price; }
async function zhouliufu() { const res = await fetch("https://price.zlf.cn/index_35.aspx"); const data = await res.text(); const price_match = data.match(/<span class="fr">(.*)<\/span>/); const price = price_match ? price_match[1] : ""; return price; }
async function liufuzhubao() { const res = await fetch("https://www.lukfookeshop.com.cn"); const data = await res.text(); const price_match = data.match(/>:(.*?)元\/克/); const price = price_match ? price_match[1] : ""; return price; }
async function zhoudafu() { const res = await fetch( `https://api2.ctfmall.com/gateway//ctfmall-common2-server/common/ctfTodayGoldPrice?timestamp=${Date.now()}` ); const data: any = await res.json(); const price = data.data.todayPriceHK; return price; }
async function laomiao() { const res = await fetch( "https://vip.laomiao.com.cn/index.php/m/home-gold_price.html" ); const data: any = await res.json(); const price = data.data.price_list.find( (i: any) => i.name === "足金饰品" ).price; return parseInt(price); }
|