Skip to content

Instantly share code, notes, and snippets.

@sbocinec
Created May 7, 2020 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbocinec/6e2b88940958ac91af40cfa0634ef392 to your computer and use it in GitHub Desktop.
Save sbocinec/6e2b88940958ac91af40cfa0634ef392 to your computer and use it in GitHub Desktop.
Userscript to fix broken GCP console header in Firefox
// ==UserScript==
// @name gcp console header fixer for firefox
// @namespace Violentmonkey Scripts
// @match https://console.cloud.google.com/*
// @grant none
// @version 1.0
// @author Stano Bocinec
// @description 5/7/2020, 3:49:08 PM
// ==/UserScript==
/**
* A utility function for userscripts that detects and handled AJAXed content.
* https://github.com/CoeJoder/waitForKeyElements.js
*
* Usage example:
*
* function callback(domElement) {
* domElement.innerHTML = "This text inserted by waitForKeyElements().";
* }
*
* waitForKeyElements("div.comments", callback);
* // or
* waitForKeyElements(selectorFunction, callback);
*
* @param {(string|function)} selectorOrFunction - The selector string or function.
* @param {function} callback - The callback function; takes a single DOM element as parameter.
* If returns true, element will be processed again on subsequent iterations.
* @param {boolean} [waitOnce=true] - If true, only one iteration is performed.
* @param {number} [interval=300] - The time (ms) to wait between iterations.
*/
function waitForKeyElements(selectorOrFunction, callback, waitOnce, interval) {
if (typeof waitOnce === "undefined") {
waitOnce = true;
}
if (typeof interval === "undefined") {
interval = 300;
}
var targetNodes = (typeof selectorOrFunction === "function")
? selectorOrFunction()
: document.querySelectorAll(selectorOrFunction);
var targetsFound = targetNodes && targetNodes.length > 0;
if (targetsFound) {
targetNodes.forEach(function(targetNode) {
var attrAlreadyFound = "data-userscript-alreadyFound";
var alreadyFound = targetNode.getAttribute(attrAlreadyFound) || false;
if (!alreadyFound) {
var cancelFound = callback(targetNode);
if (cancelFound) {
targetsFound = false;
}
else {
targetNode.setAttribute(attrAlreadyFound, true);
}
}
});
}
if (!targetsFound || !waitOnce) {
setTimeout(function() {
waitForKeyElements(selectorOrFunction, callback, waitOnce, interval);
}, interval);
}
}
waitForKeyElements("pcc-dropdown-wrapper", (element) => {
element.parentNode.removeChild(element);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment