lnguyen46 / GCP Console Shortcuts

// ==UserScript==
// @name         GCP Console Shortcuts
// @namespace    http://tampermonkey.net/
// @description  Google Cloud Platform Console shortcuts
// @author       lnguyen46
// @match        https://console.cloud.google.com/*
// @grant        none
// @version 1.0.3
// @license MIT
// @copyright 2020
// ==/UserScript==

var appendedCss = false;
var appendedDiv = false;
var snackBarCss = "#snackbar{visibility:hidden;min-width:250px;margin-left:-125px;background-color:#333;color:#fff;text-align:left;border-radius:2px;padding:16px;position:fixed;z-index:1;left:50%;bottom:30px;font-size:14px}#snackbar.show{visibility:visible;opacity:0.7;-webkit-animation:fadein 0.5s,fadeout 0.5s 5s;animation:fadein 0.5s,fadeout 0.5s 5s}@-wbkit-keyframes fadein{from{bottom:0;opacity:0}to{bottom:30px;opacity:0.7}}@keyframes fadein{from{bottom:0;opacity:0}to{bottom:30px;opacity:0.7}}@-webkit-keyframes fadeout{from{bottom:30px;opacity:0.7}to{bottom:0;opacity:0}}@keyframes fadeout{from{bottom:30px;opacity:0.7}to{bottom:0;opacity:0}}";
var snackBarContent = "\
Hold Alt key and press:<br/>\
i - IAM <br/>\
c - Compute Engine <br/>\
s - SQL <br/>\
g - GKE <br/>\
f - Firewall Rules<br/>\
shift + b - BigQuery<br/>\
l - Logs<br/>\
. - Storage (GCS)";

function addGlobalStyle(css) {
    if (appendedCss) {
        return;
    }
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
    appendedCss = true;
}

function appendSnackbar() {
    if (appendedDiv) {
        return;
    }
    var body = document.getElementsByTagName('body')[0];
    var div = document.createElement('div');
    div.id="snackbar";
    div.innerHTML = snackBarContent;
    body.appendChild(div);
    appendedDiv = true;
}

function showSnackbar() {
    var x = document.getElementById("snackbar");
    x.className = "show";
    setTimeout(function(){
        x.className = x.className.replace("show", "");
    }, 5 * 1000);
}

(function() {
    window.addEventListener('keydown', function(e) {
        if (e.altKey && e.keyCode == 191) {
            e.preventDefault();
            addGlobalStyle(snackBarCss);
            
            appendSnackbar();
            showSnackbar();
        }
    
        if (e.altKey && e.keyCode == 73) {
            window.location = "/iam-admin/iam";
        }

        if (e.altKey && e.keyCode == 67) {
            window.location = "/compute/instances";
        }

        if (e.altKey && e.keyCode == 83) {
            window.location = "/sql/instances";
        }

        // atl + .
        if (e.altKey && e.keyCode == 190) {
            window.location = "/storage";
        }

        if (e.altKey && e.keyCode == 75) {
            window.location = "/kubernetes";
        }

        if (e.altKey && e.keyCode == 70) {
            window.location = "/networking/firewalls/list";
        }

        // alt + shift + b
        // to avoid shadow emacs key: alt + b (backward-word)
        if (e.altKey && e.shiftKey && e.keyCode == 66) {
            window.location = "/bigquery";
        }

        // alt + L
        if (e.altKey && e.keyCode == 76) {
            window.location = "/logs";
        }
    });
})();