Skip to content

Instantly share code, notes, and snippets.

@lrakai
Last active October 23, 2021 22:57
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 lrakai/18102059d74d7589863b92aa83b15217 to your computer and use it in GitHub Desktop.
Save lrakai/18102059d74d7589863b92aa83b15217 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name lab suite
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Suite of lab helpers
// @author You
// @match https://*.cloudacademy.com/*
// @grant none
// @require https://code.jquery.com/jquery-3.6.0.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @downloadURL https://gist.githubusercontent.com/lrakai/18102059d74d7589863b92aa83b15217/raw/lab-suite
// @updateURL https://gist.githubusercontent.com/lrakai/18102059d74d7589863b92aa83b15217/raw/lab-suite
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const lab_debug_selector = "#start-lab";
waitForKeyElements(lab_selector, debug_lab);
function debug_lab() {
const pathElements = window.location.pathname.split('/').filter(e => e);
if (! pathElements.includes("lab") || pathElements.length > 2 /* lab step */) {
return;
}
const url = new URL(window.location.href);
if (!url.searchParams.get('debug_mode')) {
url.searchParams.set('debug_mode', true);
window.location.href = url.href;
}
}
const breadcrumb_title_selector = "span[itemprop='name']";
const page_title_selector = "#lab-page-title";
const admin_link_selector = "#admin-link";
const link_style = "color:#00ffc6;font-size:12px";
waitForKeyElements(breadcrumb_title_selector, adminLinks);
waitForKeyElements(page_title_selector, adminLinks);
function adminLinks() {
const pathElements = window.location.pathname.split('/').filter(e => e);
if (!pathElements.includes("lab")) {
return;
}
const title_element = document.querySelector(page_title_selector);
if (title_element.parentElement.querySelector('#admin-link')) {
return;
}
const breadcrumb_element = document.querySelector(breadcrumb_title_selector).closest('ol').querySelector('li:last-child');
if (breadcrumb_element.querySelector('#admin-link')) {
return;
}
const view = pathElements.length > 2 ? "labstep" : "lab";
const breadcrumb_title_query = breadcrumb_element.textContent.replaceAll(' ', '+');
const title_query = title_element.textContent.replaceAll(' ', '+');
if(view === "lab") {
const lab_admin_link = `https://cloudacademy.com/admin/clouda/laboratories/laboratory/?title=${title_query}`;
title_element.insertAdjacentHTML('afterEnd', `&nbsp;<a id="admin-link" style='${link_style}' href='${lab_admin_link}' target='_blank'>Admin</a>`);
} else {
const lab_admin_link = `https://cloudacademy.com/admin/clouda/laboratories/laboratory/?title=${breadcrumb_title_query}`;
const lab_step_admin_link = `https://cloudacademy.com/admin/clouda/laboratories/labstep/?title=${title_query}`;
breadcrumb_element.insertAdjacentHTML('afterEnd', `&nbsp;<a id="admin-link" style='${link_style}' href='${lab_admin_link}' target='_blank'>Admin</a>`);
title_element.insertAdjacentHTML('afterEnd', `&nbsp;<a id="admin-link" style='${link_style}' href='${lab_step_admin_link}' target='_blank'>Admin</a>`);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment