WU PWS inHg to mb

Converts inHg to mb on Wunderground PWS pages

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         WU PWS inHg to mb
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Converts inHg to mb on Wunderground PWS pages
// @author       w_biggs
// @match        https://www.wunderground.com/dashboard/pws/*
// @grant        none
// ==/UserScript==

const convertInHg = function convertInHg(inHg) {
  return Math.round(inHg * 33.86389 * 100) / 100;
};

const replaceInHg = function replaceInHg(element) {
  let text = element.innerText;
  if (text) {
    if (text.includes('in')) {
      text = text.split(' in')[0];
    }
    const val = Number.parseFloat(text);
    // set 35 as a max legal value for inhg
    if (!Number.isNaN(val) && val < 35) {
      element.innerText = convertInHg(val);
    }
  }
}

const replaceAllInHg = function replaceAllInHg() {
  const currPressures = document.querySelectorAll('span.wu-unit-pressure span.wu-value');
  currPressures.forEach((currPressure) => {
    replaceInHg(currPressure);
  });
  const currPressureUnits = document.querySelectorAll('span.wu-unit-pressure span.wu-label span.ng-star-inserted');
  currPressureUnits.forEach((currPressureUnit) => {
    currPressureUnit.innerText = 'mb';
  });
  const chartGridLines = document.querySelectorAll('div.charts-canvas div:nth-child(5) text.tick-label');
  chartGridLines.forEach((chartGridLine) => {
    replaceInHg(chartGridLine);
  });
}

const mutationTarget = document.querySelector('app-root');

console.log(mutationTarget);

const observer = new MutationObserver((mutationsList) => {
  for (let i = 0; i < mutationsList.length; i += 1) {
    const mutation = mutationsList[i];
    if (mutation.type === 'childList' && mutation.addedNodes.length &&
      !(mutation.addedNodes.length === 1 && mutation.addedNodes[0].nodeType === Node.TEXT_NODE)) {
      console.log('mutation!');
      replaceAllInHg();
    }
  }
});

observer.observe(mutationTarget, { subtree: true, childList: true });

replaceAllInHg();