// ==UserScript== // @name 监测汇率每日变化 // @namespace wyx // @description 美元现汇卖出价 // @version 0.0.1 // @author wyx // @crontab * 10-23 once * * // @grant GM_xmlhttpRequest // @grant GM_notification // @grant GM_getValue // @grant GM_setValue // @connect chl.cn // @connect sct.icodef.com // @require https://scriptcat.org/lib/946/%5E1.0.1/PushCat.js // ==/UserScript== let accessKey = GM_getValue("PushCat.AccessKey"); // //* 消息推送: https://sct.icodef.com/ const push = new PushCat({ accessKey, }); //* 返回的是一个函数体 function pushSend(title, content) { return new Promise(resolve => { if (accessKey) { push.send(title, content); } // 启用浏览器的通知功能 GM_notification({ title: title, text: content, }); resolve(); }) } // 功能函数,获取子串,获取包含在html内的json数据 function getSubstring(inputStr, startStr, endStr) { const startIndex = inputStr.indexOf(startStr); if (startIndex == -1) { return null; } const endIndex = inputStr.indexOf(endStr, startIndex + startStr.length); if (endIndex == -1) { return null; } return inputStr.substring(startIndex + startStr.length, endIndex); } function getChangeRateInfo() { return new Promise((resolve, reject) => { // 获取汇率网站信息 GM_xmlhttpRequest({ url:'https://chl.cn/?usd', onload(resp) { if (resp.status == 200) { resolve(resp); } else { // 获取失败,在此尝试 pushSend("汇率获取失败", "请求返回错误: " + resp.status).then(() => reject()); } }, onerror(e) { pushSend("汇率获取失败", e || "未知错误").then(() => reject()); } }); }) } // 主要功能函数 function handler() { return getChangeRateInfo().then(async resp => { // 获取的数据是存在html中 const data = resp.responseText; const startStra = "