本文为 其乐用户(UID:1757671) 发布的原创文章,转摘前请联系该用户获得许可
本帖最后由 1757671 于 2025-6-23 00:23 编辑
自动转换网页价格,另外右上角悬浮窗可以自定义查询汇率。
有需要的就自己改改汇率就好了。我用的是饿区汇率。
汇率有手动和自动。
如果要改的话可以把货币锁写改下,然后货币代号改下就行。
实在不会的就回帖我改吧。 主要是自动获取太容易错误了。
那个锁是悬浮窗移动位置的锁定和解锁。
输入框就是手动输入的
回复自取吧
第一次发帖,应该没搞错格式。
2025.6.23 0:23
抱歉各位,插入代码时候,被自动改了网址项
重新更新了代码 有问题留言。
- // ==UserScript==
- // @name STEAM页面价格转换(周洋自用)
- // @namespace http://tampermonkey.net/
- // @version 3.0
- // @description Convert RUB prices to CNY on Steam pages
- // @author You
- // // @match *://store.steampowered.com/*
- // @match *://help.steampowered.com/*
- // @match https://checkout.steampowered.com/checkout/*
- // @grant GM_getValue
- // @grant GM_setValue
- // @grant GM_xmlhttpRequest
- // @grant GM_registerMenuCommand
- // ==/UserScript==
- (function() {
- 'use strict';
- console.log("脚本已加载");
- let currentRateDisplay = null;
- const defaultRate = 10;
- function createCurrentRateDisplay() {
- currentRateDisplay = document.createElement('div');
- currentRateDisplay.style.position = 'fixed';
- currentRateDisplay.style.top = '10px';
- currentRateDisplay.style.right = '10px';
- currentRateDisplay.style.zIndex = '1000';
- currentRateDisplay.style.color = 'white';
- currentRateDisplay.style.fontSize = '12px';
- currentRateDisplay.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
- currentRateDisplay.style.padding = '5px';
- currentRateDisplay.style.borderRadius = '5px';
- document.body.appendChild(currentRateDisplay);
- updateCurrentRateDisplay();
- }
- function getExchangeMode() {
- return GM_getValue('exchangeMode', 'auto');
- }
- function setExchangeMode(mode) {
- GM_setValue('exchangeMode', mode);
- }
- function getExchangeRate() {
- return GM_getValue('exchangeRate', defaultRate);
- }
- function setExchangeRate(rate) {
- GM_setValue('exchangeRate', rate);
- }
- async function fetchExchangeRate() {
- return new Promise((resolve, reject) => {
- GM_xmlhttpRequest({
- method: "GET",
- url: 'https://api.exchangerate-api.com/v4/latest/CNY',
- onload: function(response) {
- if (response.status === 200) {
- const data = JSON.parse(response.responseText);
- resolve(data.rates.RUB);
- } else {
- reject('无法获取汇率');
- }
- },
- onerror: function() {
- reject('请求错误');
- }
- });
- });
- }
- async function convertPrices() {
- console.log("开始转换价格");
- let exchangeRate;
- const mode = getExchangeMode();
- if (mode === 'auto') {
- exchangeRate = await fetchExchangeRate();
- setExchangeRate(exchangeRate);
- } else {
- exchangeRate = getExchangeRate();
- }
- // 获取页面中所有含有 "руб" 的元素
- const priceElements = document.querySelectorAll('*:not(script):not(style)'); // 获取所有元素
- priceElements.forEach(element => {
- if (element.childNodes.length > 0) {
- element.childNodes.forEach(node => {
- if (node.nodeType === Node.TEXT_NODE && node.nodeValue.includes('руб')) {
- const rubPriceMatch = node.nodeValue.match(/(\d+([.,]\d+)?)\s*руб/);
- if (rubPriceMatch) {
- // 检查是否已经转换过
- const existingCnyElement = element.querySelector('.converted-price');
- if (!existingCnyElement) {
- const rubPrice = parseFloat(rubPriceMatch[1].replace(',', '.'));
- const cnyPrice = (rubPrice / exchangeRate).toFixed(2);
- // 创建新的转换价格文本
- const convertedText = ` (¥${cnyPrice})`;
- const newNode = document.createTextNode(convertedText);
- // 在原文本后插入新的价格
- const span = document.createElement('span');
- span.className = 'converted-price';
- span.style.marginLeft = '5px';
- span.style.fontSize = '12px';
- span.style.color = 'red';
- span.appendChild(newNode);
- element.appendChild(span);
- }
- }
- }
- });
- }
- });
- }
- function updateCurrentRateDisplay() {
- const exchangeRate = getExchangeRate();
- const mode = getExchangeMode();
- currentRateDisplay.textContent = `${exchangeRate} RUB (${mode === 'auto' ? '自动' : '手动'})`;
- }
- function toggleExchangeMode() {
- const currentMode = getExchangeMode();
- const newMode = currentMode === 'auto' ? 'manual' : 'auto';
- setExchangeMode(newMode);
- alert(`汇率模式已切换为: ${newMode === 'auto' ? '自动获取' : '手动设置'}`);
- updateCurrentRateDisplay();
- convertPrices();
- }
- function setManualExchangeRate() {
- const userRate = prompt("请输入手动汇率 (1 CNY = ? RUB):", getExchangeRate());
- if (userRate !== null) {
- const rate = parseFloat(userRate);
- if (!isNaN(rate) && rate > 0) {
- setExchangeRate(rate);
- alert(`手动汇率已设置为: ${rate} RUB`);
- updateCurrentRateDisplay();
- convertPrices();
- } else {
- alert('请输入一个有效的数字汇率!');
- }
- }
- }
- GM_registerMenuCommand("切换汇率模式", toggleExchangeMode);
- GM_registerMenuCommand("设置手动汇率", setManualExchangeRate);
- function init() {
- console.log("初始化脚本");
- createCurrentRateDisplay();
- const mode = getExchangeMode();
- if (mode === 'auto') {
- fetchExchangeRate().then(rate => {
- setExchangeRate(rate);
- updateCurrentRateDisplay();
- convertPrices();
- }).catch(console.error);
- } else {
- updateCurrentRateDisplay();
- convertPrices();
- }
- }
- const observer = new MutationObserver(() => {
- convertPrices();
- });
- observer.observe(document.body, {
- childList: true,
- subtree: true
- });
- window.addEventListener('load', init);
- })();
复制代码
|