// ==UserScript== // @name 每日美元/人民币汇率获取 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.3 // @description 获取每日变动的美元/人民币的汇率,数据来源于 MSN 财经,可以根据汇率值设置通知,支持Service酱发送到微信 // @author Exisi // @crontab * 1-23 once * * // @grant GM_notification // @grant GM_getValue // @grant GM_setValue // @grant GM_xmlhttpRequest // @homepage https://scriptcat.org/script-show-page/790 // @supportURL https://exi.ink // ==/UserScript== /* ==UserConfig== 汇率设置: rswitch: title: 汇率线开关 description: 是否使用汇率线 type: checkbox default: false rate: title: 仅当汇率值低于汇率线时发送浏览器通知 description: 请输入汇率线 default: 7.2 通知推送设置: switch: title: 浏览器通知开关 description: 是否启用通知 type: checkbox default: true notice: title: 通知标题 description: 请输入通知标题 default: 今日美元汇率为: time: title: 通知时间 description: 请输入通知时间(毫秒),默认 3000 default: 3000 push: title: Server酱推送开关 description: 是否推送到 Service 酱 type: checkbox default: false push_title: title: Server酱推送标题 description: 请输入推送标题 default: 今日美元汇率为: SendKey: title: Server酱的 SendKey description: 请输入您的 SendKey ==/UserConfig== */ return new Promise((resolve, reject) => { let setting = getConfig(); let rate = getExchangeRate(setting); resolve("ok"); }); //获取设置 function getConfig() { let setting = { rswitch: GM_getValue("汇率设置.rswitch"), nswitch: GM_getValue("通知推送设置.switch") ?? true, push: GM_getValue("通知推送设置.push"), rate: GM_getValue("汇率设置.rate"), notice: GM_getValue("通知推送设置.notice"), time: GM_getValue("通知推送设置.time"), push_title : GM_getValue("通知推送设置.push_title"), SendKey: GM_getValue("通知推送设置.SendKey"), }; return setting; } //获取汇率 function getExchangeRate(setting) { GM_xmlhttpRequest({ url: "https://assets.msn.cn/service/Finance/Quotes?apikey=0QfOX3Vn51YCzitbLaRkTTBadtWpgTN8NZLW0C1SEM&activityId=85C25DB2-49DA-43C3-BEFD-5F71F7C667CC&ocid=finance-finance::portfolio-Peregrine&cm=zh-cn&ids=avym77&wrapodata=false", method: "get", headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.76", }, onload(response) { if (response.status == 200) { let data = response.responseText; data = JSON.parse(data); let rate = data[0].price == null || data[0].price == "" ? 0 : data[0].price; if (rate > 0) { notification(setting.notice, setting, rate); } } else { notification("获取美元汇率失败,请检查api", setting, 0); } }, onerror() { notification("获取美元汇率失败,请检查网络设置", setting, null); }, }); } //浏览器通知 function notification(title, setting, rate) { if (setting.rswitch && rate > setting.rate) { return; } if (setting.nswitch) { GM_notification({ title: title, text: "美元/人民币:" + rate, timeout: setting.time, }); } if (rate && setting.push) { pushService(setting, rate); } } //推送到 Service 酱 function pushService(setting, rate) { GM_xmlhttpRequest({ url: "https://sctapi.ftqq.com/" + setting.SendKey + ".send?title=" + setting.push_title + rate + "&desp=美元/人民币:" + rate, method: "get", headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.76", }, }); }