Add 'redirif-token-manager.html'

This commit is contained in:
Jan 2021-02-23 00:09:10 +00:00
parent 28700878db
commit 2a6faa01e8

470
redirif-token-manager.html Normal file
View file

@ -0,0 +1,470 @@
<!DOCTYPE html>
<html>
<head>
<title>redirif thing</title>
<meta property="og:title" content="rm -rf /" />
<meta property="og:description" content="do it pussy" />
<meta property="og:site_name" content="run it" />
<style>
/* @import url("https://www.w3.org/TR/wai-aria-practices/examples/listbox/css/listbox.css"); */
.annotate {
font-style: italic;
color: #366ed4;
}
.listbox-area {
padding: 20px;
background: #eee;
border: 1px solid #aaa;
font-size: 0;
}
.left-area,
.right-area {
box-sizing: border-box;
display: inline-block;
font-size: 14px;
vertical-align: top;
width: 95%;
}
.left-area {
padding-right: 10px;
}
.right-area {
padding-left: 10px;
}
[role="listbox"] {
min-height: 18em;
padding: 0;
background: white;
border: 1px solid #aaa;
}
[role="option"] {
display: block;
padding: 0 1.2em 0 1.8em;
position: relative;
line-height: 1.8em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
[role="option"].focused {
background: #bde4ff;
}
[role="option"][aria-selected="true"]::before {
content: '⇢';
position: absolute;
left: 0.5em;
}
[role="option"][aria-selected="true"] {
background-color: #999999;
}
button {
font-size: 16px;
}
button[aria-disabled="true"] {
opacity: 0.5;
}
.move-right-btn {
padding-right: 20px;
position: relative;
}
.move-right-btn::after {
content: ' ';
height: 10px;
width: 12px;
background-image: url('../imgs/Arrows-Right-icon.png');
background-position: center right;
position: absolute;
right: 2px;
top: 6px;
}
.move-left-btn {
padding-left: 20px;
position: relative;
}
.move-left-btn::after {
content: ' ';
height: 10px;
width: 12px;
background-image: url('../imgs/Arrows-Left-icon.png');
background-position: center left;
position: absolute;
left: 2px;
top: 6px;
}
#ss_elem_list {
max-height: 18em;
overflow-y: auto;
position: relative;
}
#exp_button {
border-radius: 0;
font-size: 16px;
text-align: left;
padding: 5px 10px;
width: 150px;
position: relative;
}
#exp_button::after {
width: 0;
height: 0;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-top: 8px solid #aaa;
content: " ";
position: absolute;
right: 5px;
top: 10px;
}
#exp_button[aria-expanded="true"]::after {
width: 0;
height: 0;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-top: 0;
border-bottom: 8px solid #aaa;
content: " ";
position: absolute;
right: 5px;
top: 10px;
}
#exp_elem_list {
border-top: 0;
max-height: 10em;
overflow-y: auto;
position: absolute;
margin: 0;
width: 148px;
}
.hidden {
display: none;
}
.toolbar {
font-size: 0;
}
.toolbar-item {
border: 1px solid #aaa;
background: #ccc;
}
.toolbar-item[aria-disabled="false"]:focus {
background-color: #eee;
}
.offscreen {
clip: rect(1px 1px 1px 1px);
clip: rect(1px, 1px, 1px, 1px);
font-size: 14px;
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
.instanceinfo {
float: left;
margin-right: 50px;
}
.creds {
padding-top: 20px;
}
</style>
</head>
<body>
<div class="instanceinfo">
<h3>Server info</h3>
<p>API enabled: <a id="apienabled">n/a</a></p>
<p>Total redirects: <a id="redirectcount">n/a</a></p>
<p>Server version: <a id="serverversion">n/a</a></p>
</div>
<div class="creds">
<form autocomplete="on">
<label for="apiURL">API URL:</label><br />
<input type="text" id="apiURLField" name="apiURL" value="https://r.shitass.studio/api" autocomplete="on"><br/>
<label for="masterToken">Master token:</label><br />
<input type="text" id="masterTokenField" name="masterToken" autocomplete="on"><br/>
</form>
<br/>
<button type="button" onclick="fetchServerInfo(true); fetchAllRedirects()">Load</button>
</div>
<br/><br/>
<div class="listbox-area">
<div class="left-area">
<span id="ss_elem">
Nerd tokens: <a id="token-count"></a>
</span>
<ul id="ss_elem_list" tabindex="0" role="listbox" aria-labelledby="ss_elem">
<!--
<li id="peepeepoopoo" role="option">
peepee poopoo
</li>
-->
</ul>
<button id="copy_token" onclick="copyToken()">⇨ Copy token</button>
<button id="add_token" onclick="addToken()">+ Add token</button>
<button id="delete_token" onclick="deleteSelectedToken()">- Revoke token</button>
</div>
</div>
<br/>
<div class="listbox-area">
<div class="left-area">
<span id="ss_elem-redirs">
Redirects: <a id="redir-count"></a>
</span>
<ul id="ss_elem_list-redirs" tabindex="0" role="listbox" aria-labelledby="ss_elem">
<!--
<li id="peepeepoopoo" role="option">
peepee poopoo
</li>
-->
</ul>
<!-- <button id="create_redir_nerd" onclick="createRedir('master')">+ New redirect</button> -->
<!-- <button id="delete_redir_master" onclick="createRedir('selected')">+ New redirect (Master)</button> -->
<button id="create_redir" disabled="true">+ Create redirect</button>
<button id="delete_redir" onclick="deleteRedir()">- Delete redirect</button>
</div>
</div>
<br/>
<script>
async function fetchServerInfo(serverInfoOnly) {
try {
let data = {
apiURL: document.getElementById('apiURLField').value,
apiToken: document.getElementById('masterTokenField').value
}
if (!data.apiURL) return alert("No API URL specified");
let res = await fetch(data.apiURL);
res = await res.json();
updateServerInfo(res);
// If master token is present, fetch nerd tokens
if (data.apiToken && serverInfoOnly == true) {
fetchNerdTokens(data);
}
} catch(e) {
console.error(e);
updateServerInfo({});
}
}
async function fetchNerdTokens(data) {
let res = await fetch(data.apiURL + '/master/list', { headers: { 'Api-Token': data.apiToken } });
res = await res.json();
if (!(res instanceof Array)) return alert('api returned bullshit');
document.getElementById('ss_elem_list').innerHTML = '';
res.forEach(token => {
document.getElementById('ss_elem_list').innerHTML += `<li id="token_${token}" role="option" onclick="selectToken('${token}')">\n`
+ `${token}\n`
+ `</li>\n`
});
document.getElementById('token-count').innerHTML = res.length;
}
function updateServerInfo(info) {
document.getElementById('apienabled').innerHTML = info.apiEnabled != undefined ? (info.apiEnabled ? 'Yes' : 'No') : 'n/a';
document.getElementById('redirectcount').innerHTML = info.totalRedirects ?? 'n/a';
document.getElementById('serverversion').innerHTML = info.appVersion ?? 'n/a';
}
function addToken() {
let data = {
apiURL: document.getElementById('apiURLField').value,
apiToken: document.getElementById('masterTokenField').value
}
if (!data.apiURL) return alert("No API URL specified");
if (!data.apiToken) return alert("No API token specified");
try {
fetch(data.apiURL + '/master/create', { method: 'POST', headers: { 'Api-Token': data.apiToken } })
.catch(alert).then(() => fetchNerdTokens(data));
} catch(e) {
console.error(e);
alert('Failed to create new token\n' + e);
}
}
function deleteSelectedToken() {
let selected, selectedIndex;
let children = document.getElementById('ss_elem_list').children;
for (let i = 0; i < children.length; i++) {
if (children[i].getAttribute('aria-selected')) { selected = children[i]; selectedIndex = i }
}
if (!selected) return alert('No token selected');
deleteToken(selected.id.substr('token_'.length));
}
function deleteToken(token) {
let data = {
apiURL: document.getElementById('apiURLField').value,
apiToken: document.getElementById('masterTokenField').value
}
if (!data.apiURL) return alert("No API URL specified");
if (!data.apiToken) return alert("No API token specified");
try {
fetch(data.apiURL + '/master/delete', { method: 'DELETE', headers: { 'Api-Token': data.apiToken }, body: token })
.catch(alert).then(() => fetchNerdTokens(data));
} catch(e) {
alert('Failed to delete token\n' + e);
}
}
function copyToken() {
let selected;
let children = document.getElementById('ss_elem_list').children;
for (let i = 0; i < children.length; i++) {
if (children[i].getAttribute('aria-selected')) selected = children[i];
}
if (!selected) return alert('No token selected');
let token = selected.innerHTML;
let copy = document.createElement('div');
copy.innerHTML = token;
copy.style.position = 'absolute';
copy.style.left = '-9999px';
document.body.appendChild(copy);
var copyRange = document.createRange();
copyRange.selectNode(copy);
window.getSelection().addRange(copyRange);
try {
document.execCommand('copy');
} catch(e) {
console.error(e);
alert(e);
}
window.getSelection().removeRange(copyRange);
document.body.removeChild(copy);
}
async function fetchAllRedirects() {
let data = {
apiURL: document.getElementById('apiURLField').value,
apiToken: document.getElementById('masterTokenField').value
}
if (!data.apiURL || !data.apiToken) return;
let fetchAmount = 500;
let fetchInterval = 1000;
let index = 0;
let redirects = [];
fetchMore();
let interval = setInterval(fetchMore, fetchInterval);
async function fetchMore() {
try {
let res = fetch(`${data.apiURL}/master/listRedirects?size=${fetchAmount}&start=${index}`, { headers: { 'Api-Token': data.apiToken }, method: 'GET' });
index += fetchAmount;
res = await res;
res = await res.json();
if (!(res instanceof Array)) throw 'API returned non-array';
res.forEach(redir => redirects.push(redir));
let redirList = document.getElementById('ss_elem_list-redirs');
redirList.innerHTML = '';
redirects.forEach(redir => {
redirList.innerHTML += `<li id="redir_${redir.Key}" role="option" onclick="selectRedir('${redir.Key}')">\n`
+ `${redir.Key}\n`
+ `</li>\n`
});
if (res.length < fetchAmount) {
clearInterval(interval);
console.log(redirects);
}
} catch(e) { clearInterval(interval); alert('Failed to fetch redirects:\n' + e) }
}
}
function deleteRedir() {
let data = {
apiURL: document.getElementById('apiURLField').value,
apiToken: document.getElementById('masterTokenField').value
}
if (!data.apiURL) return alert("No API URL specified");
if (!data.apiToken) return alert("No API token specified");
let selected;
let children = document.getElementById('ss_elem_list-redirs').children;
for (let i = 0; i < children.length; i++) {
if (children[i].getAttribute('aria-selected')) selected = children[i];
}
if (!selected) return alert('No redirect selected');
let redirID = selected.id.substr('redir_'.length);
try {
fetch(data.apiURL + '/master/deleteRedirect', { headers: { 'Api-Token': data.apiToken }, body: redirID, method: 'DELETE' })
.then(() => fetchAllRedirects());
} catch(e) {
alert(e);
}
}
</script>
<script>
function selectToken(token) {
let obj = document.getElementById('token_' + token);
if (!obj) throw 'Token not found in document';
let children = document.getElementById('ss_elem_list').children;
for (let i = 0; i < children.length; i++) {
children[i].setAttribute('aria-selected', false);
}
obj.setAttribute('aria-selected', true);
}
function selectRedir(redir) {
let obj = document.getElementById('redir_' + redir);
if (!obj) throw 'Redir not found in document';
let children = document.getElementById('ss_elem_list-redirs').children;
for (let i = 0; i < children.length; i++) {
children[i].setAttribute('aria-selected', false);
}
obj.setAttribute('aria-selected', true);
}
</script>
</body>
</html>