/*
* docs-settings-links.js
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* @link https://www.librenms.org
*
* @copyright 2021 Tony Murray
* @author Tony Murray
*/
function findGetParameter(parameterName) {
let result = null, tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function (item) {
tmp = item.split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
});
return result;
}
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
function promptSettingUrl(e) {
e.preventDefault();
let librenmsUrl = prompt("Enter your LibreNMS URL to get direct settings link.\nNote: This URL is only stored in your browser.", localStorage.getItem('librenms_url') || '');
if (! isValidHttpUrl(librenmsUrl)) {
alert("Invalid url, must start with http:// or https://")
return false;
}
wrapSettingsLinks(librenmsUrl);
return false;
}
function wrapSettingsLinks(librenmsUrl) {
// fetch saved url
if (! librenmsUrl) {
librenmsUrl = localStorage.getItem('librenms_url');
}
if (librenmsUrl) {
localStorage.setItem('librenms_url', librenmsUrl);
librenmsUrl = librenmsUrl.replace(/\/+$/i, ''); // trim trailing /
[].forEach.call(document.querySelectorAll('.admonition.setting>.admonition-title'), function (el) {
if (! el.dataset.setting_url) {
el.dataset.setting_url = el.innerText;
}
let link = document.createElement('a');
link.classList.add('setting-link');
link.href = librenmsUrl + '/settings/' + el.dataset.setting_url;
link.innerText = link.href;
link.target = '_blank';
let edit = document.createElement('a');
edit.classList.add('url-edit-link');
edit.title = "Change setting base url"
edit.onclick = promptSettingUrl;
edit.innerHTML = ''
el.innerText = '';
el.appendChild(link);
el.appendChild(document.createTextNode(' '))
el.appendChild(edit);
});
} else {
[].forEach.call(document.querySelectorAll('.admonition.setting>.admonition-title'), function (el) {
if (!el.dataset.setting_url) {
el.dataset.setting_url = el.innerText;
}
let link = document.createElement('a');
link.classList.add('setting-link');
link.onclick = promptSettingUrl;
link.innerText = 'https:///' + el.dataset.setting_url;
el.innerText = '';
el.appendChild(link);
});
}
}
document.addEventListener('DOMContentLoaded', function () {
wrapSettingsLinks(findGetParameter('librenms_url'));
}, false);