Last active
March 23, 2020 17:34
-
-
Save kyletaylored/b85f4792622b09b28d59679bec3a53a9 to your computer and use it in GitHub Desktop.
Add MySQL link in Pantheon dashboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Add MySQL Link | |
// @namespace http://tampermonkey.net/ | |
// @version 0.2 | |
// @description Just give me a link. | |
// @author Kyle Taylor | |
// @match https://*.dashboard.pantheon.io/sites/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var checkChartTime = performance.now(); | |
var checkExist = setInterval(function() { | |
window.connectionInfoButton = document.querySelector(".workspace-header"); | |
if (window.connectionInfoButton !== null && Object.getOwnPropertyNames(window.connectionInfoButton).length !== 0) { | |
clearInterval(checkExist); | |
// Run main | |
main(); | |
} else { | |
console.log("Still waiting for dashboard"); | |
var diff1 = performance.now() - checkChartTime; | |
// If longer than 10 seconds, bail. | |
if (diff1 > 10000) { | |
clearInterval(checkExist); | |
console.log("Bailed on connection info"); | |
} | |
} | |
}, 1000); | |
// Actual main function | |
function main() { | |
document.querySelector(".connection-info-button").addEventListener("click", initConnection); | |
function initConnection() { | |
setTimeout(getConnectionInfo, 60); | |
} | |
function getConnectionInfo() { | |
var connectionInfo = document.querySelector('#connection-info'); | |
var cObj = {}; | |
var checkDb = false; | |
var dbAnchor = null; | |
var dbIndex = 0; | |
var dbEl = null; | |
console.log(connectionInfo); | |
if (connectionInfo !== null) { | |
var connectArr = Array.from(connectionInfo.children); | |
for (var ind in connectArr) { | |
var el = connectArr[ind]; | |
if (el.innerText.includes('atabase')) { | |
dbIndex = ind; | |
dbEl = el; | |
break; | |
} | |
} | |
// Get connection | |
var curr = null; | |
for (var l = 0; l < 6; l++) { | |
curr = (curr !== null) ? curr : dbEl; | |
console.log(curr); | |
switch (l) { | |
case 1: | |
dbAnchor = curr.querySelector("label"); | |
break; | |
case 2: | |
cObj.host = getValue(curr); | |
break; | |
case 3: | |
cObj.username = getValue(curr); | |
break; | |
case 4: | |
cObj.password = getValue(curr); | |
break; | |
case 5: | |
cObj.port = getValue(curr); | |
break; | |
case 6: | |
cObj.db = getValue(curr); | |
break; | |
} | |
// Increment curr | |
curr = curr.nextSibling; | |
} | |
console.log(cObj); | |
// Create DB link | |
var dbLink = document.createElement('a'); | |
// Create the text node for anchor element. | |
var linkT = "Open in MySQL client"; | |
var link = document.createTextNode(linkT); | |
// Add link | |
dbLink.appendChild(link); | |
dbLink.class = "mysql-quick-connect"; | |
dbLink.style = "margin-bottom: 15px;display: inline-block;"; | |
dbLink.title = linkT; | |
dbLink.href = `mysql://${cObj.username}:${cObj.password}@${cObj.host}:${cObj.port}`; | |
var dBreak = document.createElement('br'); | |
dbAnchor.after(dbLink); | |
dbAnchor.after(dBreak); | |
} | |
} | |
function getValue(el) { | |
return el.querySelector("input").value; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment