BigQuery Cost Estimator

Dynamically replace BigQuery's query size estimate (GB) with the estimated cost in USD.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         BigQuery Cost Estimator
// @namespace    https://bigquery.cloud.google.com/
// @version      1.7
// @description  Dynamically replace BigQuery's query size estimate (GB) with the estimated cost in USD.
// @author       Dann Berg
// @match        https://console.cloud.google.com/bigquery*
// @run-at       document-end
// @license      MIT
// @homepageURL  https://greasyfork.org/en/scripts/525352-bigquery-cost-estimator
// @supportURL   https://github.com/dannberg/bigquery-cost-estimator/issues
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const PRICE_PER_TB = 6.25;
    const GB_TO_TB = 1024;

    function updateCost() {
        // Target the specific element inside query-validation-status
        const statusText = document.querySelector('div.cfc-truncated-text.ng-star-inserted');

        if (statusText && statusText.textContent.includes("This query will process")) {
            // Extract the GB value
            const match = statusText.textContent.match(/([\d.]+)\s*GB/);
            if (match) {
                const gb = parseFloat(match[1]);
                if (!isNaN(gb)) {
                    // Calculate cost
                    const cost = ((gb / GB_TO_TB) * PRICE_PER_TB).toFixed(2);

                    // Replace text with cost
                    statusText.textContent = `Estimated cost: $${cost}`;
                }
            }
        }
    }

    // Observe changes in the Query Validation UI
    const observer = new MutationObserver(updateCost);
    observer.observe(document.body, { childList: true, subtree: true });

    // Run once on script load
    updateCost();
})();