Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bookmarklet #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Next Next commit
Add initial implementation of bookmarklet
  • Loading branch information
ValHayot committed Jul 29, 2023
commit 1ef0f0befe0d3e7420234523295b47ce497cc1c8
50 changes: 50 additions & 0 deletions bookmarklet/bookmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(async () => {
var endpoint_links = document.querySelectorAll('div[class="h3 my-0"] a[href*="endpoints"]');

Array.prototype.forEach.call(endpoint_links, async function (element, index) {

// step 1: get endpoint id
var segments = element.href.split('/');
var endpoint = segments.pop() || segments.pop();

// step 2: query server for endpoint stats
let url = `https://129.114.109.109:5001/caws?endpoint=${endpoint}`
const response = await fetch(url);
const endpoint_json = await response.json();

// step 3: display results
// Create parent div
// TODO: handle case where div already exists
var energyDiv = document.createElement('div');
energyDiv.classList.add('caws');
energyDiv.classList.add('col-lg');

var energyRowDiv = document.createElement('div');
energyRowDiv.classList.add('row');

// title div
var energyTitleDiv = document.createElement('div');
energyTitleDiv.classList.add('col-4');
energyTitleDiv.classList.add('col-lg-12');
energyTitleDiv.classList.add('small');
energyTitleDiv.classList.add('text-uppercase');
energyTitleDiv.classList.add('text-success');
energyTitleDiv.innerHTML = 'Total energy expenditure';
energyRowDiv.appendChild(energyTitleDiv);

// content div
var energyContentDiv = document.createElement('div');
energyContentDiv.classList.add('col-8');
energyContentDiv.classList.add('col-lg-12');
energyContentDiv.classList.add('small');
energyContentDiv.innerHTML = endpoint_json.total_energy;
energyRowDiv.appendChild(energyContentDiv);

energyDiv.appendChild(energyRowDiv);

var elementSibling = element.parentNode.parentNode.nextElementSibling.firstElementChild.lastElementChild;
elementSibling.parentNode.insertBefore(energyDiv, elementSibling.nextSibling);

}, endpoint_links);

})();
1 change: 1 addition & 0 deletions bookmarklet/bookmark.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bookmarklet/bookmarklet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
javascript:(async()=>{var e=document.querySelectorAll('div[class="h3 my-0"] a[href*="endpoints"]');Array.prototype.forEach.call(e,async function(e,a){var s=e.href.split("/");let t=`https://129.114.109.109:5000/caws?endpoint=${s.pop()||s.pop()}`,l=await fetch(t),d=await l.json();var i=document.createElement("div");i.classList.add("caws"),i.classList.add("col-lg");var n=document.createElement("div");n.classList.add("row");var c=document.createElement("div");c.classList.add("col-4"),c.classList.add("col-lg-12"),c.classList.add("small"),c.classList.add("text-uppercase"),c.classList.add("text-success"),c.innerHTML="Total energy expenditure",n.appendChild(c);var r=document.createElement("div");r.classList.add("col-8"),r.classList.add("col-lg-12"),r.classList.add("small"),r.innerHTML=d.total_energy,n.appendChild(r),i.appendChild(n);var o=e.parentNode.parentNode.nextElementSibling.firstElementChild.lastElementChild;o.parentNode.insertBefore(i,o.nextSibling)},e)})();