mirror of
https://github.com/zadam/trilium.git
synced 2025-11-04 20:36:13 +01:00
Add 'apps/web-clipper/' from commit '786d249a6effe0262859d05e6cb1737e5fc8bdd8'
git-subtree-dir: apps/web-clipper git-subtree-mainline:b0e5190718git-subtree-split:786d249a6e
This commit is contained in:
63
apps/web-clipper/options/options.html
Normal file
63
apps/web-clipper/options/options.html
Normal file
@@ -0,0 +1,63 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
|
||||
<meta content="utf-8" http-equiv="encoding">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="error-message" style="font-weight: bold; color: red; display: none;"></div>
|
||||
<div id="success-message" style="font-weight: bold; color: green; display: none;"></div>
|
||||
|
||||
<h2>Trilium desktop instance</h2>
|
||||
|
||||
<p>Web clipper by default tries to find a running desktop instance on port 37740. If you configured your Trilium desktop app to run on a different port, you can specify it here (otherwise keep it empty).</p>
|
||||
|
||||
<form id="trilium-desktop-setup-form">
|
||||
<p>
|
||||
<label for="trilium-desktop-port" style="font-weight: bold;">Trilium desktop port: </label>
|
||||
|
||||
<input type="text" id="trilium-desktop-port" size="6" style="text-align: right;"/> (normally keep this empty)
|
||||
</p>
|
||||
|
||||
<input type="submit" value="Save">
|
||||
</form>
|
||||
|
||||
<h2>Trilium server instance</h2>
|
||||
|
||||
<p>If you have a server instance set up, you can optionally configure it as a fail over target for the clipped notes. Desktop instance will still be given priority, but in cases that the desktop instance is not available (e.g. it's not running), web clipper will send the notes to the server instance instead.</p>
|
||||
|
||||
<div id="trilium-server-configured" style="display: none;">
|
||||
<strong>Trilium server instance has been already configured to <a id="trilium-server-link" href=""></a>.</strong>
|
||||
|
||||
<p>You can also <a id="reset-trilium-server-setup" href="javascript:">remove the current setup</a> and configure it again.</p>
|
||||
</div>
|
||||
|
||||
<form id="trilium-server-setup-form" style="display: none;">
|
||||
<table>
|
||||
<tr>
|
||||
<th>Trilium server URL:</th>
|
||||
<td><input type="text" id="trilium-server-url"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Password:</th>
|
||||
<td><input type="password" id="trilium-server-password"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th></th>
|
||||
<td><input type="submit" value="Login to the server instance"/></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>Note that the entered password is not stored anywhere, it will be only used to retrieve an authorization token from the server instance which will be then used to send the clipped notes.</p>
|
||||
</form>
|
||||
|
||||
<script src="../lib/cash.min.js"></script>
|
||||
<script src="../lib/browser-polyfill.js"></script>
|
||||
<script src="options.js"></script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
135
apps/web-clipper/options/options.js
Normal file
135
apps/web-clipper/options/options.js
Normal file
@@ -0,0 +1,135 @@
|
||||
const $triliumServerUrl = $("#trilium-server-url");
|
||||
const $triliumServerPassword = $("#trilium-server-password");
|
||||
|
||||
const $errorMessage = $("#error-message");
|
||||
const $successMessage = $("#success-message");
|
||||
|
||||
function showError(message) {
|
||||
$errorMessage.html(message).show();
|
||||
$successMessage.hide();
|
||||
}
|
||||
|
||||
function showSuccess(message) {
|
||||
$successMessage.html(message).show();
|
||||
$errorMessage.hide();
|
||||
}
|
||||
|
||||
async function saveTriliumServerSetup(e) {
|
||||
e.preventDefault();
|
||||
|
||||
if ($triliumServerUrl.val().trim().length === 0
|
||||
|| $triliumServerPassword.val().trim().length === 0) {
|
||||
showError("One or more mandatory inputs are missing. Please fill in server URL and password.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let resp;
|
||||
|
||||
try {
|
||||
resp = await fetch($triliumServerUrl.val() + '/api/login/token', {
|
||||
method: "POST",
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
password: $triliumServerPassword.val()
|
||||
})
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
showError("Unknown error: " + e.message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (resp.status === 401) {
|
||||
showError("Incorrect credentials.");
|
||||
}
|
||||
else if (resp.status !== 200) {
|
||||
showError("Unrecognised response with status code " + resp.status);
|
||||
}
|
||||
else {
|
||||
const json = await resp.json();
|
||||
|
||||
showSuccess("Authentication against Trilium server has been successful.");
|
||||
|
||||
$triliumServerPassword.val('');
|
||||
|
||||
browser.storage.sync.set({
|
||||
triliumServerUrl: $triliumServerUrl.val(),
|
||||
authToken: json.token
|
||||
});
|
||||
|
||||
await restoreOptions();
|
||||
}
|
||||
}
|
||||
|
||||
const $triliumServerSetupForm = $("#trilium-server-setup-form");
|
||||
const $triliumServerConfiguredDiv = $("#trilium-server-configured");
|
||||
const $triliumServerLink = $("#trilium-server-link");
|
||||
const $resetTriliumServerSetupLink = $("#reset-trilium-server-setup");
|
||||
|
||||
$resetTriliumServerSetupLink.on("click", e => {
|
||||
e.preventDefault();
|
||||
|
||||
browser.storage.sync.set({
|
||||
triliumServerUrl: '',
|
||||
authToken: ''
|
||||
});
|
||||
|
||||
restoreOptions();
|
||||
});
|
||||
|
||||
$triliumServerSetupForm.on("submit", saveTriliumServerSetup);
|
||||
|
||||
const $triliumDesktopPort = $("#trilium-desktop-port");
|
||||
const $triilumDesktopSetupForm = $("#trilium-desktop-setup-form");
|
||||
|
||||
$triilumDesktopSetupForm.on("submit", e => {
|
||||
e.preventDefault();
|
||||
|
||||
const port = $triliumDesktopPort.val().trim();
|
||||
const portNum = parseInt(port);
|
||||
|
||||
if (port && (isNaN(portNum) || portNum <= 0 || portNum >= 65536)) {
|
||||
showError(`Please enter valid port number.`);
|
||||
return;
|
||||
}
|
||||
|
||||
browser.storage.sync.set({
|
||||
triliumDesktopPort: port
|
||||
});
|
||||
|
||||
showSuccess(`Port number has been saved.`);
|
||||
});
|
||||
|
||||
async function restoreOptions() {
|
||||
const {triliumServerUrl} = await browser.storage.sync.get("triliumServerUrl");
|
||||
const {authToken} = await browser.storage.sync.get("authToken");
|
||||
|
||||
$errorMessage.hide();
|
||||
$successMessage.hide();
|
||||
|
||||
$triliumServerUrl.val('');
|
||||
$triliumServerPassword.val('');
|
||||
|
||||
if (triliumServerUrl && authToken) {
|
||||
$triliumServerSetupForm.hide();
|
||||
$triliumServerConfiguredDiv.show();
|
||||
|
||||
$triliumServerLink
|
||||
.attr("href", triliumServerUrl)
|
||||
.text(triliumServerUrl);
|
||||
}
|
||||
else {
|
||||
$triliumServerSetupForm.show();
|
||||
$triliumServerConfiguredDiv.hide();
|
||||
}
|
||||
|
||||
const {triliumDesktopPort} = await browser.storage.sync.get("triliumDesktopPort");
|
||||
|
||||
$triliumDesktopPort.val(triliumDesktopPort);
|
||||
}
|
||||
|
||||
$(restoreOptions);
|
||||
Reference in New Issue
Block a user