Skip to content

Commit

Permalink
Add export to JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
proginosko committed Oct 22, 2024
1 parent 3ab3115 commit 9e811b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
7 changes: 5 additions & 2 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,13 @@
<strong>Note:</strong> Import will be disabled if you have prevented access to the options for any block set.
</p>
<p>
<button id="exportOptions" type="button">Export Options to File</button>
<button id="importOptions" type="button">Import Options from File</button>
<button id="exportOptions" type="button">Export Options to Text File</button>
<button id="importOptions" type="button">Import Options from Text File</button>
<input type="file" id="importFile">
</p>
<p class="simplifiable">
<button id="exportOptionsJSON" type="button">Export Options to JSON File</button>
</p>
<p>
<input id="exportPasswords" type="checkbox">
<label for="exportPasswords">Include passwords in exported options</label>
Expand Down
38 changes: 36 additions & 2 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const DEFAULT_OPTIONS_FILE = "LeechBlockOptions.txt";
const DEFAULT_JSON_FILE = "LeechBlockOptions.json";

const SUB_OPTIONS = {
"applyFilter" : [ "filterName", "filterMute" ],
Expand Down Expand Up @@ -125,6 +126,7 @@ function initForm(numSets) {
$("#clockOffsetTime").click(showClockOffsetTime);
$("#exportOptions").click(exportOptions);
$("#importOptions").click(importOptions);
$("#exportOptionsJSON").click(exportOptionsJSON);
$("#exportOptionsSync").click(exportOptionsSync);
$("#importOptionsSync").click(importOptionsSync);
$("#openDiagnostics").click(openDiagnostics);
Expand All @@ -137,6 +139,8 @@ function initForm(numSets) {
// Hide sync options (sync storage not supported on Android yet)
getElement("syncOpts1").style.display = "none";
getElement("syncOpts2").style.display = "none";
// Disable export to JSON button
getElement("exportOptionsJSON").disabled = true;
}

// Set active tab
Expand Down Expand Up @@ -854,7 +858,7 @@ function applyImportOptions(options) {
}
}

// Export options to file
// Export options to text file
//
function exportOptions() {
let exportPasswords = getElement("exportPasswords").checked;
Expand Down Expand Up @@ -902,7 +906,7 @@ function exportOptions() {
}
}

// Import options from file
// Import options from text file
//
function importOptions() {
let file = getElement("importFile").files[0];
Expand Down Expand Up @@ -965,6 +969,36 @@ function importOptions() {
}
}

// Export options to JSON file
//
function exportOptionsJSON() {
let exportPasswords = getElement("exportPasswords").checked;

let options = compileExportOptions(exportPasswords);

// Convert options to JSON string
let json = JSON.stringify(options);

// Create blob and download it
let blob = new Blob([json], { type: "text/plain", endings: "native" });
let url = URL.createObjectURL(blob);
let downloadOptions = {
url: url,
filename: DEFAULT_JSON_FILE,
saveAs: true
};
browser.downloads.download(downloadOptions).then(onSuccess, onError);

function onSuccess() {
$("#alertExportSuccess").dialog("open");
}

function onError(error) {
warn("Cannot download options: " + error);
$("#alertExportError").dialog("open");
}
}

// Export options to sync storage
//
function exportOptionsSync(event) {
Expand Down

3 comments on commit 9e811b8

@RX14
Copy link

@RX14 RX14 commented on 9e811b8 Dec 8, 2024

Choose a reason for hiding this comment

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

Why no JSON import? I accidentally exported my options as JSON and didn't realise until import, and had to write a jq script to convert them to text to re-import them.

@proginosko
Copy link
Owner Author

Choose a reason for hiding this comment

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

The JSON export was added because of this request: #368

No plans to add JSON import unless there's demand for it. Feel free to post it in the Ideas category, though, and see if it gets any upvotes.

@RX14
Copy link

@RX14 RX14 commented on 9e811b8 Dec 9, 2024

Choose a reason for hiding this comment

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

Ah, I see. Perhaps consider a UI tweak to make the JSON export button more obviously "you don't want this unless you know what you're doing".

Please sign in to comment.