Implement options page to store user access token and submit form's URL
This commit is contained in:
parent
893ff4e1e0
commit
3beeeb4c5f
|
@ -18,8 +18,12 @@
|
|||
"manifest_version": 2,
|
||||
"name": "Nexus Archive Tracker",
|
||||
"options_ui": {
|
||||
"browser_style": true,
|
||||
"page": "options/index.html"
|
||||
},
|
||||
"permissions": [
|
||||
"storage"
|
||||
],
|
||||
"short_name": "NA Tracker",
|
||||
"version": "0.0.1"
|
||||
"version": "0.0.2"
|
||||
}
|
||||
|
|
|
@ -1,11 +1,31 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link href="style.css" rel="stylesheet" />
|
||||
</head>
|
||||
<body>
|
||||
<h1 id="myHeading">My content</h1>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
<html lang="en-GB">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Options - Nexus Archive Tracker</title>
|
||||
<link href="style.css" rel="stylesheet" />
|
||||
</head>
|
||||
<body>
|
||||
<form id="optionsForm" method="post">
|
||||
<fieldset>
|
||||
<legend>Tracker options</legend>
|
||||
<p>
|
||||
<label>
|
||||
User access token:
|
||||
<input name="userAccessToken" required size="64" type="text" pattern="[0-9A-Fa-f]+">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
Submit form's URL:
|
||||
<input name="trackerSubmitUrl" required size="64" type="url">
|
||||
</label>
|
||||
</p>
|
||||
</fieldset>
|
||||
<p>
|
||||
<button type="submit">Save</button>
|
||||
</p>
|
||||
</form>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1 +1,41 @@
|
|||
document.getElementById('myHeading').style.color = 'red'
|
||||
const getOptionsForm = () => document.getElementById('optionsForm');
|
||||
const getStorage = () => browser.storage.sync;
|
||||
|
||||
const saveForm = event => {
|
||||
event.preventDefault()
|
||||
|
||||
const optionsForm = getOptionsForm();
|
||||
const userAccessToken = optionsForm.elements['userAccessToken'].value
|
||||
const trackerSubmitUrl = optionsForm.elements['trackerSubmitUrl'].value
|
||||
|
||||
getStorage().set({userAccessToken, trackerSubmitUrl}).catch(
|
||||
error => {
|
||||
const message = `Error saving preferences: ${error}`
|
||||
window.console.error(message)
|
||||
window.alert(message)
|
||||
}
|
||||
)
|
||||
};
|
||||
|
||||
const initForm = () => {
|
||||
const optionsForm = getOptionsForm()
|
||||
optionsForm.addEventListener('submit', saveForm)
|
||||
|
||||
getStorage().get(null).then(
|
||||
results => {
|
||||
if (results.hasOwnProperty('userAccessToken')) {
|
||||
optionsForm.elements['userAccessToken'].value = results.userAccessToken
|
||||
}
|
||||
if (results.hasOwnProperty('trackerSubmitUrl')) {
|
||||
optionsForm.elements['trackerSubmitUrl'].value = results.trackerSubmitUrl
|
||||
}
|
||||
},
|
||||
error => {
|
||||
const message = `Error loading preferences: ${error}`
|
||||
window.console.error(message)
|
||||
window.alert(message)
|
||||
}
|
||||
)
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', initForm)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
h1 {
|
||||
font-style: italic;
|
||||
body {
|
||||
padding: 0.5em 2ex;
|
||||
}
|
||||
|
|
Reference in New Issue