Skip to content

Commit

Permalink
Add import/export function (#1457)
Browse files Browse the repository at this point in the history
* Add Import/Export form to options.html

* Update messages.json

* Tweak a couple identifiers for clarity

* Add Export function

* Add Import function

* Update saveOptions()

* Clears export textarea upon calling sendOptions()

* Rearrange exportSettings in saveOptions()

* Add initial variable and missing semicolon

* Update common.js

* Add import success or fail message to messages.json

* Add Import success/fail case to options.js

* update importSettings()

* Update alerts

* Fix JSON Check

* Fix import not working

* Fix changes not highlighting

* Remove unnecessary changes

* Remove extra newlines and updates comment

* Remove extra whitespace

* Update formatting

* small format fix

* Add HZ+ JSON test

* Add one more test variable

* Fix json test

* Formatting fixes

* More format fixes
  • Loading branch information
LiliaDoe authored Nov 8, 2024
1 parent de95fae commit 6988bee
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 21 deletions.
60 changes: 44 additions & 16 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -595,18 +595,18 @@
"message": "caption",
"description": "[options] Add download caption option"
},
"optDownloadReplaceOriginalFilenameTooltip": {
"message": "Replace original filename or remove it if no value supplied (extension is preserved)",
"description": "[options] Tooltip for replace original filename when downloading option"
},
"optDownloadReplaceOriginalFilenamePlaceholder": {
"message": "Example: fullsize_image",
"description": "[options] Placeholder for download folder option"
},
"optDownloadReplaceOriginalFilename": {
"message": "On save, replace original filename by:",
"description": "[options] Replace original filename when downloading option"
},
"optDownloadReplaceOriginalFilenameTooltip": {
"message": "Replace original filename or remove it if no value supplied (extension is preserved)",
"description": "[options] Tooltip for replace original filename when downloading option"
},
"optDownloadReplaceOriginalFilenamePlaceholder": {
"message": "Example: fullsize_image",
"description": "[options] Placeholder for download folder option"
},
"optDownloadReplaceOriginalFilename": {
"message": "On save, replace original filename by:",
"description": "[options] Replace original filename when downloading option"
},
"optSectionTroubleshooting": {
"message": "Troubleshooting",
"description": "[options] Page section for troubleshooting"
Expand All @@ -630,10 +630,38 @@
"optSupportedVideoFormats": {
"message": "Video :",
"description": "[options] Page section for supported video formats"
},
"optSupportedAudioFormats": {
"message": "Audio :",
"description": "[options] Page section for supported audio formats"
},
"optSupportedAudioFormats": {
"message": "Audio :",
"description": "[options] Page section for supported audio formats"
},
"optSectionImportExportSettings": {
"message": "Import/Export",
"description": "[options] Page section for import/export"
},
"optSectionImportExportSettingsTooltip": {
"message": "Press export to get a string of your current settings. Press import to load your pasted settings",
"description": "[options] Tooltip for input and export options"
},
"optImportExportTextArea": {
"message": "Paste your saved settings here",
"description": "[options] Placeholder for import and export text area"
},
"optButtonImportSettings": {
"message": "Import",
"description": "[options] Import a save string from the textarea"
},
"optButtonExportSettings": {
"message": "Export",
"description": "[options] Export a save string into the textarea"
},
"optImport": {
"message": "Import Success!",
"description": "[options] Notification after successful import to options"
},
"optImportFailed": {
"message": "Import Failed",
"description": "[options] Notification after failed import to options"
},
"optPageFAQ": {
"message": "FAQ",
Expand Down
13 changes: 13 additions & 0 deletions html/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,19 @@ <h1 class="ten columns text-center" data-i18n="optTitle"></h1>
</li>
</ul>
</fieldset>
<fieldset>
<legend data-i18n="optSectionImportExportSettings"></legend>
<ul>
<div class="ttip" data-i18n-tooltip="optSectionImportExportSettingsTooltip">
<div class="field">
<textarea class="input textarea" rows="5" id="txtBoxImportExportSettings" data-i18n-placeholder="optImportExportTextArea"></textarea>
<div style="display:block; justify-content:flex-start; gap:10px;">
<div class="btn default"><a href="#" id="btnImportSettings" data-i18n="optButtonImportSettings"></a></div>
<div class="btn default"><a href="#" id="btnExportSettings" data-i18n="optButtonExportSettings"></a></div>
</div>
</div>
</ul>
</fieldset>
</form>
</div>
<div class="tab-content">
Expand Down
49 changes: 44 additions & 5 deletions js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function loadKeys(sel) {

// Saves options to localStorage.
// TODO: Migrate to https://developer.chrome.com/extensions/storage
function saveOptions() {
function saveOptions(exportSettings = false) {
options.extensionEnabled = $('#chkExtensionEnabled')[0].checked;
options.darkMode = $('#chkDarkMode')[0].checked;
options.zoomFactor = $('#txtZoomFactor')[0].value;
Expand Down Expand Up @@ -150,11 +150,14 @@ function saveOptions() {
options.useSeparateTabOrWindowForUnloadableUrlsEnabled = $('#chkUseSeparateTabOrWindowForUnloadableUrlsEnabled')[0].checked;
options.useSeparateTabOrWindowForUnloadableUrls = $('#selectUseSeparateTabOrWindowForUnloadableUrls').val();

localStorage.options = JSON.stringify(options);

sendOptions(options);
restoreOptions();
if (exportSettings) {
$('#txtBoxImportExportSettings').val(JSON.stringify(options));
} else {
localStorage.options = JSON.stringify(options);

sendOptions(options);
restoreOptions();
}
return false;
}

Expand Down Expand Up @@ -632,6 +635,8 @@ function initColorPicker(color){
const Saved = Symbol("saved");
const Cancel = Symbol("cancel");
const Reset = Symbol("reset");
const Imported = Symbol("imported");
const ImportFail = Symbol("importFail");
function displayMsg(msg) {
switch (msg) {
case Saved:
Expand All @@ -643,6 +648,12 @@ function displayMsg(msg) {
case Reset:
$('#msgtxt').removeClass().addClass('centered text-center alert info').text(chrome.i18n.getMessage('optReset')).clearQueue().animate({opacity:1}, 500).delay(5000).animate({opacity:0}, 500);
break;
case Imported:
$('#msgtxt').removeClass().addClass('centered text-center alert success').text(chrome.i18n.getMessage('optImport')).clearQueue().animate({opacity:1}, 500).delay(5000).animate({opacity:0}, 500);
break;
case ImportFail:
$('#msgtxt').removeClass().addClass('centered text-center alert danger').text(chrome.i18n.getMessage('optImportFailed')).clearQueue().animate({opacity:1}, 500).delay(5000).animate({opacity:0}, 500);
break;
default:
break;
}
Expand All @@ -662,6 +673,8 @@ $(function () {
$('#btnReset').click(function() { restoreOptionsFromFactorySettings(); displayMsg(Reset); return false; });
$('#btnDisableAllPlugins').click(function() { disableAllPlugins(); return false; });
$('#btnEnableAllPlugins').click(function() { enableAllPlugins(); return false; });
$('#btnImportSettings').click(function() { importSettings(); return false; });
$('#btnExportSettings').click(function() { exportSettings(); return false; });
$('#chkWhiteListMode').parent().on('gumby.onChange', chkWhiteListModeOnChange);
$('#txtZoomFactor').change(percentageOnChange);
$('#txtPicturesOpacity').change(percentageOnChange);
Expand Down Expand Up @@ -708,6 +721,32 @@ function enableAllPlugins() {
$('input.chkPlugin').each(function() { $(this).trigger('gumby.check'); })
}

//Checks if string is JSON.
//If yes, imports settings and clears textarea.
function importSettings() {
let jsonImport;
try {
jsonImport = JSON.parse($('#txtBoxImportExportSettings')[0].value);
// Checks if a few HZ+ settings are defined to test if it's a valid HZ+ JSON
const jsonTest = [jsonImport.centerImages, jsonImport.fullZoomKey, jsonImport.hideMouseCursor];
jsonTest.forEach((variable) => {
if (typeof variable === 'undefined') {
throw new Error('Not a valid HZ+ import JSON');
}
});
} catch (e) {
displayMsg(ImportFail);
return false;
}
displayMsg(Imported);
restoreOptions({jsonImport});
$('#txtBoxImportExportSettings').val('');
}

function exportSettings() {
saveOptions(true);
}

// highlight item if modified, unhighlight if not modified
function checkModification(item) {
if (item[0].dataset.val1 == undefined) return;
Expand Down

0 comments on commit 6988bee

Please sign in to comment.