Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
a6y3ap authored Dec 1, 2023
1 parent dc8432b commit 2ca8ecc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
2 changes: 1 addition & 1 deletion api-url.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<h1>Online Outline Manager</h1>
<label for="json-url">Enter URL:</label><br/>
<input type="text" id="json-url" name="json-url" placeholder="https://xxx.xxx.xxx.xxx:xxxxx/xxxxxxxxxxxxxxxxxxxxxx" size="50"><br/>
<button type="submit">Submit</button>
<button type="button" onClick="goHome()">Go Back</button>&nbsp;</button><button type="submit">Submit</button>
</form>
<div id="json-display"></div>
<script src="script.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="menu">
<h1>Online Outline Manager</h1>
<p>View outline access keys online. You can use Management API URL or Installation Output to use this tool.</p>
<a href="api-url.html">Management API URL</a>
<a href="api-url.html">Management API URL</a><br/><br/>
<a href="installation-output.html">Installation Output</a>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion installation-output.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<h1>Online Outline Manager</h1>
<label for="json-text">Paste your installation output here:</label><br/>
<textarea id="json-text" name="json-text" rows="6" cols="50" placeholder='{"apiUrl":"https://xxx.xxx.xxx.xxx:xxxxx/xxxxxxxxxxxxxxxxxxxxxx","certSha259":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}'></textarea><br/>
<button type="submit">Submit</button>
<button type="button" onClick="goHome()">Go Back</button>&nbsp;<button type="submit">Submit</button>
<p>Note: Make sure there is no line break in the JSON.</p>
</form>
<div id="json-display"></div>
Expand Down
87 changes: 65 additions & 22 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ const jsf = document.getElementById('json-form');
const uf = document.getElementById('url-form');

if (jsf) {
jsf.addEventListener('submit', function(event) {
event.preventDefault();
const jsonText = document.getElementById('json-text').value;
if (localStorage.getItem('jsonText')) {
// If it exists, retrieve the value and directly run the code
var jsonText = localStorage.getItem('jsonText');
fetch_output(jsonText);
} else {

jsf.addEventListener('submit', function(event) {
event.preventDefault();
const jsonText = document.getElementById('json-text').value;
localStorage.setItem('jsonText', jsonText);

fetch_output(jsonText)
});
}
}

function fetch_output(jsonText) {

try {
const jsonData = JSON.parse(jsonText);
const apiUrl = jsonData.apiUrl + '/access-keys/';
Expand All @@ -18,16 +32,12 @@ if (jsf) {
const accessKeys = data.accessKeys;
const container = document.getElementById('json-display');
container.innerHTML = ''; // Clear previous data

/*

// sending a post request to /access-keys/ generates a new key
const itemDiv = document.createElement('div');
itemDiv.innerHTML = `<form id="addKey" method="post" action="${jsonUrl}"><button type="button" onclick="addKey()">Add New Key</button></form>`;
itemDiv.innerHTML = `<button type="button" onclick="createAccessKey('${apiUrl}')">Add New Key</button>`;
itemDiv.classList.add('addKey');
container.appendChild(itemDiv);
*/

// Loop through the accessKeys and display the items
accessKeys.forEach(accessKey => {
Expand All @@ -54,30 +64,39 @@ if (jsf) {
container.innerHTML = '<p>Invalid JSON format</p>';
console.error('Error parsing JSON:', error);
}
});
}

if (uf) {
uf.addEventListener('submit', function(event) {
event.preventDefault();
const jsonUrl = document.getElementById('json-url').value + '/access-keys/';
if (localStorage.getItem('jsonUrl')) {
// If it exists, retrieve the value and directly run the code
var jsonUrl = localStorage.getItem('jsonUrl');
fetch_data(jsonUrl);
} else {

uf.addEventListener('submit', function(event) {
event.preventDefault();
const jsonUrl = document.getElementById('json-url').value + '/access-keys/';
localStorage.setItem('jsonUrl', jsonUrl);

fetch_data(jsonUrl)
});
}
}

// Fetch the JSON data from the input URL
function fetch_data(jsonUrl) {
// Fetch the JSON data from the input URL
fetch(jsonUrl)
.then(response => response.json())
.then(data => {
.then(data => {
const accessKeys = data.accessKeys;
const container = document.getElementById('json-display');
container.innerHTML = ''; // Clear previous data

/*

// sending a post request to /access-keys/ generates a new key
const itemDiv = document.createElement('div');
itemDiv.innerHTML = `<form id="addKey" method="post" action="${jsonUrl}"><button type="button" onclick="addKey()">Add New Key</button></form>`;
itemDiv.innerHTML = `<button type="button" onclick="createAccessKey('${jsonUrl}')">Add New Key</button>`;
itemDiv.classList.add('addKey');
container.appendChild(itemDiv);
*/

// Loop through the accessKeys and display the items
accessKeys.forEach(accessKey => {
Expand All @@ -95,7 +114,6 @@ if (uf) {
container.innerHTML = `<p>Error fetching JSON data. Possible reason may be SSL, click <a href="${jsonUrl}">here</a> to allow unsafe connection and click on submit again..</p>`;
console.error('Error fetching JSON:', error);
});
});
}

// Function to copy the accessUrl to the clipboard
Expand All @@ -109,6 +127,25 @@ function copyToClipboard(text) {
alert('Copied the text: ' + text);
}

function handleResponse(response) {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
}

function createAccessKey(addK) {
const container = document.getElementById('json-display');
var firstChild = container.firstChild;
var errorDiv = document.createElement('div');
errorDiv.textContent = 'Error while creating a key';

fetch(addK, { method: 'POST', headers: { 'Content-Type': 'application/json' }, mode: 'cors' })
.then(handleResponse)
.then(data => window.location.reload())
.catch(error => container.insertBefore(errorDiv, firstChild.nextSibling));
}

// Function to add new access key
function addKey() {

Expand All @@ -120,4 +157,10 @@ function addKey() {

// Refresh the page after the form is submitted
window.location.reload();
}

// Function to add new access key
function goHome() {
// Go to Home Page
window.location.href = 'index.html';
}
7 changes: 5 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ a {
margin: auto;
padding: 1em;
margin-bottom: 1em;
width: max-content;
}

input, textarea {
width: 100%;
max-width: 400px;
}

#json-display div {
background: #3e494e;
max-width: 90%;
padding: 1em;
margin: auto;
text-align: left;
Expand Down

0 comments on commit 2ca8ecc

Please sign in to comment.