Skip to content

Instantly share code, notes, and snippets.

@iwasa-kosui
Last active February 5, 2022 09:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwasa-kosui/7fede688956e18e8ed7b57d9ade53b65 to your computer and use it in GitHub Desktop.
Save iwasa-kosui/7fede688956e18e8ed7b57d9ade53b65 to your computer and use it in GitHub Desktop.
BigQuery での課金額を表示する UserScript
// ==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)
}
})()
@iwasa-kosui
Copy link
Author

免責事項

私は当 gist に掲載した情報について可能な限り安全かつ正確であるように努めておりますが、安全性または正確性などについて責任を負うものでは有りません。
当 gist に掲載された情報によって生じたあらゆる損害等について、理由の如何に関わらず、私は一切責任を負いません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment