coolkaja / Hotkeys Capp 2.0

// ==UserScript==
// @name        Hotkeys Capp 2.0
// @namespace   kjo.trijsburg@nwz.nl
// @include     https://noordwest.capp.nl/*
// @include     https://staging.capp.nl/noordwest/*
// @version     1
// @grant       none
// ==/UserScript==

//declare functions
//detects if it's called in an iframe
function inIframe () {
  try {
    return window.self !== window.top;
  } catch (e) {
    return true;
  }
}

//loads script from git
function addGitScript(name){
  var imported = document.createElement('script');
  imported.src = 'https://rawgit.com/coolkaja/Capp-hotkeys/master/' + name + '.js';
  document.head.appendChild(imported);
  return imported;
}

//get the name of the current page
function getPageName(){
  adress = window.location.href;
  adress = adress.split(".");
  adress = adress[2];
  adress.split("/");
  n = "";
  for(i = 2; i < adress.length; i++){
	  if (/^[a-zA-Z]+$/.test(adress[i])){
		  n += adress[i];
	  }
  }
  return n;
}


//add the hotkey script
function addHotkeys(){
  //exit if the script runs in an iframe.
  if (inIframe()){
    return false;
  }
  //get page name and run the script for that page.
  addGitScript(getPageName());
  return true;
}

//add status image to the page
//image source: https://imgur.com/a/J6bTP
if (!inIframe()){
  
  var statusImageLink = document.createElement("a")
  statusImageLink.href = "https://github.com/coolkaja/Capp-hotkeys/blob/master/README.md#" + getPageName().toLowerCase();
  statusImageLink.target = "_blank";
  document.getElementsByTagName("body").item(0).appendChild(statusImageLink);
  
  var statusImage = document.createElement("img");
  statusImage.src = "https://i.imgur.com/SimKpiX.png";
  statusImage.style.position = "fixed";
  statusImage.style.left = "4px";
  statusImage.style.bottom = "4px";
  statusImage.setAttribute("id", "statusImage");
  statusImageLink.appendChild(statusImage);
  
}

//load the hotkey script
addHotkeys();