仅需两个文件,实现在 VS Code 状态栏监控黄金价格

最近金价波动剧烈,要是能一边写代码,一边实时监控金价变动,就不会错过高低点了!

C:\Users\你的用户名\.vscode\extensions 新建文件夹 gold-monitor,在文件夹中创建两个文件 package.jsonextension.js

package.json
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
{
"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.js
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
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();

// 设置定时器,每隔 6666 毫秒更新一次
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);
}

仅需两个文件,实现在 VS Code 状态栏监控黄金价格

https://www.imaegoo.com/2025/vscode-ext-gold-monitor/

作者

iMaeGoo

发布于

2025-02-21

更新于

2025-02-21

许可协议

CC BY 4.0

评论