Skip to content

Instantly share code, notes, and snippets.

@guschnwg
Last active February 24, 2023 15:03
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 guschnwg/72228a52cac4f00daa0473b8b0fa664e to your computer and use it in GitHub Desktop.
Save guschnwg/72228a52cac4f00daa0473b8b0fa664e to your computer and use it in GitHub Desktop.
Select last selected DB in redash
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://redash.internal.adroll.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=adroll.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
const find = (selector, container, lastFromBefore, fail) => {
// Try to find our element
let elementToClick = document.querySelector(selector);
if (elementToClick) {
elementToClick.click();
return;
}
// Try scrolling to reveal the other elements
let scrollToEl = container.lastChild;
// If we are already in the last one, fail
if (scrollToEl === lastFromBefore) {
if (fail) return;
// Try one more time
setTimeout(() => find(selector, container, scrollToEl, true));
}
// Scroll then try again
scrollToEl.scrollIntoView();
setTimeout(() => find(selector, container, scrollToEl));
}
// Getting it before load because when the page finishes loading it auto selects the first one -- DUMB
const lastOne = window.localStorage.getItem('lastSelectedDataSourceId');
// Defaults to our good old C4G
let selector = lastOne ? `[data-test="SelectDataSource${lastOne}"]` : '[data-name="Cats4Gold"]';
// Exhaustively try until we have our DOM
const interval = setInterval(() => {
// Get the current one, we need it to find the DOM element that contains our list
const currentSelectedSelector = '.editor__left__data-source';
const currentSelected = document.querySelector(currentSelectedSelector);
if (!currentSelected) return;
// Open the selector (this adds the elements used below to the DOM tree)
let select = document.querySelector('.ant-select-single .ant-select-selector');
if (!select) return;
// Open the selector
let clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('mousedown', true, true);
select.dispatchEvent(clickEvent);
// If we opened, we don't try this again
clearInterval(interval);
const findInterval = setInterval(() => {
// Find the container of the selectable options
const itemInSelectableList = document.querySelector(`[data-name="${currentSelected.innerText}"]`);
if (!itemInSelectableList) return;
clearInterval(findInterval);
find(selector, itemInSelectableList.parentElement);
}, 50);
}, 100);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment