Last active
August 29, 2024 02:43
-
-
Save iwasa-kosui/7fede688956e18e8ed7b57d9ade53b65 to your computer and use it in GitHub Desktop.
BigQuery での課金額を表示する UserScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name How you will be billied with BigQuery | |
// @version 0.1 | |
// @description BigQuery での課金額を表示します | |
// @author Kosui Iwasa | |
// @match https://console.cloud.google.com/bigquery* | |
// @icon https://www.google.com/s2/favicons?domain=google.com | |
// @grant none | |
// ==/UserScript== | |
'use strict'; | |
(function() { | |
const USD_PER_TB = 5 | |
const COST_ELEMENT_ID = 'userscript-bq-billed' | |
const roundByBase = (value, base) => Math.round(value * Math.pow(10, base)) / Math.pow(10, base) | |
const calcCost = (bytes) => Number(bytes) * USD_PER_TB * Math.pow(10, -12) | |
const createCostElement = (cost) => { | |
const costElement = document.createElement('div') | |
costElement.id = COST_ELEMENT_ID | |
costElement.textContent = `${cost} USD (in US region)` | |
return costElement | |
} | |
const globalSend = XMLHttpRequest.prototype.send | |
XMLHttpRequest.prototype.send = function(){ | |
this.addEventListener("readystatechange", function() { | |
if (this.responseText.includes("totalBytesProcessed")) { | |
const totalBytesProcessed = JSON.parse(this.responseText)[0]?.data?.response?.totalBytesProcessed | |
if (totalBytesProcessed == null) { | |
return | |
} | |
const cost = calcCost(totalBytesProcessed) | |
const costRounded = roundByBase(cost, 3) | |
const costElement = createCostElement(costRounded) | |
document.getElementById(COST_ELEMENT_ID)?.remove() | |
document.querySelector(".cfc-truncated-text.ng-star-inserted").append(costElement) | |
} | |
}, false); | |
globalSend.apply(this, arguments) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
免責事項
私は当 gist に掲載した情報について可能な限り安全かつ正確であるように努めておりますが、安全性または正確性などについて責任を負うものでは有りません。
当 gist に掲載された情報によって生じたあらゆる損害等について、理由の如何に関わらず、私は一切責任を負いません。