Skip to content

Instantly share code, notes, and snippets.

@Darker
Created February 3, 2017 18:46
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 Darker/a3bab9b64a4270fce189aa94282a28ef to your computer and use it in GitHub Desktop.
Save Darker/a3bab9b64a4270fce189aa94282a28ef to your computer and use it in GitHub Desktop.
Userscript to allow students of CTU to open multiple tabs of KOS
// ==UserScript==
// @name KOS multiple tabs
// @namespace util
// @description Allows you to use KOS in mutiple tabs.
// @include https://www.kos.cvut.cz/kos/*
// @version 1
// @grant none
// ==/UserScript==
// Name of local storage entry where page id is stored
const DATA_NAME = "KOS_PAGE_ID";
console.info("Multiple tab KOS init, page code is: ", pageCode);
/**
* This is called on DOM load. It will broadcast MD5 hash page names to all other open tabs **/
function code_broadcast()
{
var data = {};
data.pageCode = pageCode;
console.info("Broadcasting code: ", pageCode);
localStorage.setItem(DATA_NAME,JSON.stringify(data));
}
function code_receive(event)
{
console.log(event);
if (event.key != DATA_NAME)
return;
var message;
try {
message = JSON.parse(event.newValue);
if(typeof message!="object" || typeof message.pageCode!="string")
throw new Error("pageCode is missing from data.");
console.log("New code received: ", message);
}
catch(e) {
console.error("Invalid data for KOS page id in local storage. This really shouldn't happen normally, so if it happened to you, you probably deserve it.");
throw e;
}
// Only replace if different
if(pageCode!=message.pageCode) {
console.info("Replacing code", pageCode, "with",message.pageCode);
// Replace page code in URL, if applicable
var url = window.location.href;
url = url.replace(/page=([a-f0-9]{20,32})/i, "page="+message.pageCode);
if(url!=window.location.href)
window.history.replaceState("", "", url);
// Change the idiotic pageCode global variable
pageCode = message.pageCode;
}
else {
console.info("Code did not change: ", pageCode);
}
}
// This condition checks if page is in iframe. Only non-iframe
// pages are handled by this script
if(window.self == window.top) {
code_broadcast();
window.addEventListener("storage", code_receive);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment