Sort-button custom sites

This commit is contained in:
magnolia1234 2020-04-11 19:40:12 +02:00 committed by GitHub
parent 372bf90e97
commit 257d7ddeb0
2 changed files with 30 additions and 6 deletions

View file

@ -41,7 +41,7 @@
<div style="clear:both;"></div>
<div style="width:90%;">
<h3>Json file</h3>
You can edit the text area and save (only when json-text is valid).
You can edit/sort the text area and save (only when json-text is valid).
Clear & save to reset. You can also export/import json-text for new installations.
</div>
<br/>
@ -51,6 +51,7 @@
<div id="error"></div>
<span style='float:left;padding-bottom:50px'>
<button id="save">Save</button>
<button id="sort">Sort</button>
<button id="export">Export</button>
<button id="import">Import</button>
<input type="file" id="importInput" accept=".txt" style="display:none"/>

View file

@ -1,8 +1,20 @@
var ext_api = chrome || browser;
function capitalize(str) {
if (typeof str !== 'string') return '';
return str.charAt(0).toUpperCase() + str.slice(1);
}
function sortJson(json) {
return Object.keys(json)
.sort().reduce(function (Obj, key) {
Obj[key] = json[key];
return Obj;
}, {});
}
// Saves options to ext_api.storage
function save_options() {
var gh_url = document.getElementById('bypass_sites').value;
var textareaEl = document.querySelector('#bypass_sites textarea');
var sites_custom = {};
if (textareaEl.value !== '')
@ -21,6 +33,18 @@ function save_options() {
});
}
// Sort json by key in textarea
function sort_options() {
var textareaEl = document.querySelector('#bypass_sites textarea');
var sites_custom = {};
if (textareaEl.value !== '') {
var sites_custom = JSON.parse(textareaEl.value);
var sites_custom_sorted = sortJson(sites_custom);
textareaEl.value = JSON.stringify(sites_custom_sorted);
}
}
// Export custom sites to file
function export_options() {
ext_api.storage.sync.get({
@ -63,13 +87,12 @@ function _imp() {
// Add custom site to ext_api.storage
function add_options() {
var gh_url = document.getElementById('add_site').value;
var inputEls = document.querySelectorAll('#add_site input');
var sites_custom = {};
for (let i = 0; i < inputEls.length; i++) {
if (inputEls[i].dataset.key === 'title') {
var title = inputEls[i].value;
var title = capitalize(inputEls[i].value);
if (title === '')
break;
sites_custom[title] = {};
@ -84,7 +107,7 @@ function add_options() {
if (sites_custom[title]['domain'] === '')
sites_custom = {};
else
sites_custom[title]['domain'] = sites_custom[title]['domain'].replace('www.', '');
sites_custom[title]['domain'] = sites_custom[title]['domain'].replace('www.', '').toLowerCase();
// add new site to local storage
ext_api.storage.sync.get({
@ -112,7 +135,6 @@ function add_options() {
// Delete custom site from ext_api.storage
function delete_options() {
var gh_url = document.getElementById('custom_sites').value;
var selectEl = document.querySelector('#custom_sites select');
var sites_custom = {};
var remove_key = selectEl.value;
@ -204,6 +226,7 @@ function renderOptions() {
document.addEventListener('DOMContentLoaded', renderOptions);
document.getElementById('save').addEventListener('click', save_options);
document.getElementById('sort').addEventListener('click', sort_options);
document.getElementById('export').addEventListener('click', export_options);
document.getElementById('import').onclick = function () {importInput.click()}
document.getElementById('importInput').addEventListener("change", import_options, false);