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

Add token entry/storage functionality to test console #7

Merged
merged 21 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ var home embed.FS

func (h scrapeServer) mustHomeTemplate() *template.Template {
tmpl := template.New("home")
var keyF = func() string { return "" }
if len(h.SigningKey) > 0 {
keyF = func() string {
var authTokenF = func() string { return "" }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just renamed this to make it a little more descriptive.

var authEnabledF = func() bool { return len(h.SigningKey) > 0 }
if authEnabledF() {
authTokenF = func() string {
c, err := auth.NewClaims(
auth.WithSubject("home"),
auth.ExpiresIn(60*time.Minute),
Expand All @@ -126,7 +127,11 @@ func (h scrapeServer) mustHomeTemplate() *template.Template {
return s
}
}
tmpl = tmpl.Funcs(template.FuncMap{"AuthToken": keyF})
funcMap := template.FuncMap{
"AuthToken": authTokenF,
"AuthEnabled": authEnabledF,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds the hook so we can skip the token entry widget when auth is not enabled

}
tmpl = tmpl.Funcs(funcMap)
homeSource, _ := home.ReadFile("templates/index.html")
tmpl = template.Must(tmpl.Parse(string(homeSource)))
return tmpl
Expand Down
267 changes: 191 additions & 76 deletions internal/server/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,207 @@
<meta charset="utf-8" />
<title>scrape</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100vh;
}
div {
box-sizing: border-box;
}
pre {
white-space: pre-wrap;
width: 100%;
}
label {
display: block;
font: 1rem 'Verdana'
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 1.0rem;
}
input,
label {
margin: 0.2rem 0;
input {
padding-bottom: 0.1rem;
}
.page-container {
display: flex;
flex-direction: column;
gap: 0.6rem;
height: 100%;
padding: 0.6rem;
}
.controls-container {
width: 100%;
background-color: #ffffff;
border: 1px solid #414141;
padding: 0.6rem;
}
.controls-container form {
display: flex;
flex-direction: column;
}
.controls-container label {
padding-bottom: 0.3rem;
}
.url-entry-container {
display: flex;
align-items: center;
gap: 0.3rem;
}
.url-entry-container input[type="url"] {
flex-grow: 1;
}
.url-entry-container input[type="submit"] {
padding-inline: 0.6rem;
}
.token-container {
background-color: #ffffff;
border: 1px solid #414141;
padding: 0.6rem;
display: flex;
flex-direction: column;
gap: 0.6rem;
}
.token-controls {
display: flex;
justify-content: space-between;
}
.token-entry {
flex-grow: 1;
margin-right: 0.6rem;
}
.data-iframe {
position: relative;
top: 16px;
width: 98%;
min-width: 768px;
height: calc(100vh - 108px);
.token-textarea {
box-sizing: border-box;
resize: vertical;
width: 100%;
height: 100%;
}
.token-info {
white-space: nowrap;
display: flex;
flex-direction: column;
align-items: flex-start;
}
.token-info button {
width: 100%;
padding: 0.1rem 1.0rem;
cursor: pointer;
}
.token-info button:first-child {
margin-top: 0.1rem;
margin-bottom: 0.4rem;
}
.response-container {
width: 100%;
flex: 1;
overflow: auto;
border: 0.1rem inset #ccc;
padding-block: 0.2rem;
padding-inline: 0.4rem;
user-select: text;
}
</style>
</head>
<body>
<p>
<form id="urlForm" action="/extract" method="POST" name="scrape" target="data-iframe">
<label for="url">Enter a URL:</label>
<input type="submit" value="Hit It">
<input type="url" name="url" id="url" value="https://" size="96" maxlength="200" pattern="https?://.*" required title="URL">
<select title="URL Type" id="actionSelect" onchange="updateFormAction()">
<option value="/extract">Page</option>
<option value="/extract/headless">Headless</option>
<option value="/feed">Feed</option>
</select>
<input type="hidden" name="token" value="{{AuthToken}}">
<input type="hidden" name="pp" value="1">
</form>
</p>
<script>
function updateFormAction() {
var selected = document.getElementById("actionSelect").value;
document.getElementById("urlForm").action = selected;
}
</script>
<div class="page-container">
<div class="controls-container">
<form x-data="formHandler" @submit.prevent="handleSubmit">
<label for="url">Enter a URL:</label>
<div class="url-entry-container">
<select title="URL Type" x-model="formAction">
<option value="/extract">Page</option>
<option value="/extract/headless">Headless</option>
<option value="/feed">Feed</option>
</select>
<input type="url" name="url" id="url" value="https://" maxlength="200" pattern="https?://.*" required title="URL">
<input type="submit" value="Hit It">
<input type="hidden" name="pp" value="1">
</div>
</form>
</div><!-- controls-container -->
<!-- {{if AuthEnabled}} -->
<div class="token-container" x-data="tokenHandler()">
<div class="token-header">
<button x-on:click="isCollapsed = !isCollapsed">Enter Token</button>
</div>
<div x-show="!isCollapsed" class="token-controls" x-transition.scale>
<div class="token-entry">
<textarea x-model="authToken" class="token-textarea" id="token" rows="2" placeholder="Enter your access token here"></textarea>
</div>
<div class="token-info">
<button @click="saveToken()">Save Token</button>
<button @click="clearToken()">Clear Token</button>
</div>
</div>
</div>
<!-- {{end}} -->
<div class="response-container">
<pre x-ref="responseContent" id="responseContent"></pre>
</div>
</div><!-- page-container -->
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
const iframe = document.getElementById('data');
iframe.contentDocument.body.innerHTML = '';
const preElement = document.createElement('pre');
preElement.style.whiteSpace = 'pre-wrap';
preElement.style.width = '100%';
iframe.contentDocument.body.appendChild(preElement);
const form = document.getElementById('urlForm');
form.addEventListener('submit', async function(event) {
event.preventDefault();
const action = form.action;
const headers = new Headers();
const token = form.token.value;
if (token) {
headers.append('Authorization', `Bearer ${token}`);
}
const formData = new FormData(form);
formData.delete('token');
try {
const response = await fetch(action, {
method: 'POST',
headers: headers,
body: formData
})
if (response.ok) {
const json = await response.json();
const jsonStr = JSON.stringify(json, null, 2);
preElement.textContent = jsonStr;
} else {
const text = await response.text();
preElement.textContent = `Error ${response.status}:\n${text}`;
throw new Error(`${response.status} - ${text}`);
function formHandler() {
return {
formAction: '/extract',
async handleSubmit(event) {
const form = event.target;
const headers = new Headers();
const token = document.getElementById('token').value;
if (token) {
headers.append('Authorization', `Bearer ${token}`);
}
const formData = new FormData(form);
try {
const response = await fetch(this.formAction, {
method: 'POST',
headers: headers,
body: formData
})

if (response.ok) {
const json = await response.json();
const jsonStr = JSON.stringify(json, null, 2);
this.updateContent(jsonStr);
} else {
const text = await response.text();
this.updateContent(`Error ${response.status}:\n${text}`);
throw new Error(`${response.status} - ${text}`);
}
} catch (error) {
console.error(error);
}
},
updateContent(content) {
document.getElementById('responseContent').textContent = content;
// following should work but doesn't
// this.$refs.responseContent.textContent = content;
}
} catch (error) {
console.error(error);
}
});
});
</script>
<div>
<iframe
id="data"
title="Scrape Results"
name="data-iframe"
class="data-iframe"
>
</div>
}
}
</script>
<script type="text/javascript">
function tokenHandler() {
return {
isCollapsed: true,
authToken: localStorage.getItem('scrapeToken') || '{{AuthToken}}',
async saveToken() {
try {
const token = document.getElementById('token').value;
if (token) {
localStorage.setItem('scrapeToken', token);
alert('Token saved');
}
} catch (e) {
console.error('Error saving token to local storage', e);
}
},
async clearToken() {
localStorage.removeItem('scrapeToken');
document.getElementById('token').value = '';
},
}
}
</script>

</body>
</html>