Skip to content

Instantly share code, notes, and snippets.

@chrismeyersfsu
Last active April 6, 2022 03:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismeyersfsu/0b870a9de15545bfc6a5c02ead07db6b to your computer and use it in GitHub Desktop.
Save chrismeyersfsu/0b870a9de15545bfc6a5c02ead07db6b to your computer and use it in GitHub Desktop.
Inject api links into UI links.
// ==UserScript==
// @name Tower UI -> API links
// @version 0.2
// @description Inject API links to UI resources where-ever found.
// @author Chris Meyers
// @match *
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
function register_page_change_watcher(callback) {
var fireOnHashChangesToo = true;
var pageURLCheckTimer = setInterval (
function () {
if ( this.lastPathStr !== location.pathname
|| this.lastQueryStr !== location.search
|| (fireOnHashChangesToo && this.lastHashStr !== location.hash)
) {
this.lastPathStr = location.pathname;
this.lastQueryStr = location.search;
this.lastHashStr = location.hash;
callback(lastPathStr, lastQueryStr, lastHashStr);
}
}
, 111
);
}
String.prototype.format = function()
{
var content = this;
for (var i=0; i < arguments.length; i++)
{
var replacement = '{' + i + '}';
content = content.replace(replacement, arguments[i]);
}
return content;
};
var MAP = [{
'ui_url': new RegExp('#/jobs/playbook/([0-9]+)\?'),
'api_url': '/api/v2/jobs/{0}/',
}, {
'ui_url': new RegExp('/#/inventories/inventory/([0-9]+)\?'),
'api_url': '/api/v2/inventories/{0}/',
}, {
'ui_url': new RegExp('#/inventories/inventory/([0-9]+)\?'),
'api_url': '/api/v2/inventories/{0}/',
}, {
'ui_url': new RegExp('/#/projects/([0-9]+)\?'),
'api_url': '/api/v2/projects/{0}/',
}, {
'ui_url': new RegExp('/#/jobs/project/([0-9]+)\?'),
'api_url': '/api/v2/project_updates/{0}/',
}, {
'ui_url': new RegExp('/#/templates/job_template/([0-9]+)\?'),
'api_url': '/api/v2/job_templates/{0}/',
}, {
'ui_url': new RegExp('#/inventories/inventory/([0-9]+)/hosts/edit/([0-9]+)\?'),
'api_url': '/api/v2/hosts/{1}/',
}];
function mapUrl(ui_url) {
for (let i=0; i < MAP.length; ++i) {
let res = ui_url.match(MAP[i].ui_url);
if (res) {
return String.prototype.format.apply(MAP[i].api_url, res.slice(1));
}
}
}
function run_on_page_change(path, query, hash) {
if (hash.startsWith("#")) {
var as = $('a[href]');
for (let i=0; i < as.length; ++i) {
let api_url = mapUrl($(as[i]).attr('href'));
if (api_url) {
$(as[i]).after('<a width=100% style="background-image: url(\'/static/assets/logo-header.svg\'); background-repeat: no-repeat;" href="' + api_url + '" target="_blank">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>');
}
}
}
}
$(document).ready(function() {
$('.at-Layout-topNavRightAligner').after('<a href="/api/v2/">API</a>');
register_page_change_watcher(run_on_page_change);
//run_on_page_change(location.pathname, location.search, location.hash);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment