From d258f5cebe2c45ac5296421f9bef5bcb546c69dc Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Tue, 22 Jun 2021 11:44:35 +0000 Subject: [PATCH] close #186 user settings import and export feature and fix a few bugs with unset or incorrect defaults in localstorage.js --- CHANGELOG.md | 1 + gulp/res/js/importexport.js | 37 +++++++++++++++++++++++++++++++++++++ gulp/res/js/localstorage.js | 3 ++- views/mixins/modal.pug | 15 ++++++++++++--- 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 gulp/res/js/importexport.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 412a1ea4..fb479c5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,3 +35,4 @@ - Board owners can now edit custom pages - Show a little message and disable reply form on full threads (hit reply limit) - Allow longer language names for code blocks + - User settings import and export option diff --git a/gulp/res/js/importexport.js b/gulp/res/js/importexport.js new file mode 100644 index 00000000..d43e255c --- /dev/null +++ b/gulp/res/js/importexport.js @@ -0,0 +1,37 @@ +window.addEventListener('settingsReady', () => { + + const settingNames = ['volume','loop','imageloadingbars','live','scroll','localtime','relative','24hour','notifications','hiddenimages' + ,'notification-yous-only','yous-setting','filters1','name','theme','codetheme','customcss','disableboardcss','hiderecursive' + ,'heightlimit','crispimages','hidethumbnails','noncolorids','alwaysshowspoilers','hidepoststubs','smoothscrolling']; + + const importExportText = document.getElementById('import-export-setting'); + + const exportSettingsButton = document.getElementById('export-setting'); + const exportSettings = () => { + const settingsData = settingNames.reduce((acc, val) => { + acc[val] = localStorage.getItem(val); + return acc; + }, {}); + importExportText.value = JSON.stringify(settingsData); + } + exportSettingsButton.addEventListener('click', exportSettings, false); + + const importSettingsButton = document.getElementById('import-setting'); + const importSettings = () => { + if (importExportText.value.length > 0) { + try { + const importedSettings = JSON.parse(importExportText.value); + Object.entries(importedSettings).forEach(entry => { + setLocalStorage(entry[0], entry[1]); + }); + //should we have an update listener one day to import without refresh? + location.reload(); + } catch (e) { + //bad data + console.error(e); + } + } + } + importSettingsButton.addEventListener('click', importSettings, false); + +}); diff --git a/gulp/res/js/localstorage.js b/gulp/res/js/localstorage.js index 3ad861c8..29d3cbb0 100644 --- a/gulp/res/js/localstorage.js +++ b/gulp/res/js/localstorage.js @@ -40,13 +40,14 @@ setDefaultLocalStorage('volume', settings.defaultVolume); setDefaultLocalStorage('loop', settings.loop); setDefaultLocalStorage('imageloadingbars', settings.imageLoadingBars); setDefaultLocalStorage('live', settings.live); -setDefaultLocalStorage('scroll', settings.sctollToPosts); +setDefaultLocalStorage('scroll', settings.scrollToPosts); setDefaultLocalStorage('localtime', settings.localTime); setDefaultLocalStorage('relative', settings.relativeTime); setDefaultLocalStorage('24hour', settings.hour24Time); setDefaultLocalStorage('notifications', settings.notificationsEnabled); setDefaultLocalStorage('notification-yous-only', settings.notificationsYousOnly); setDefaultLocalStorage('yous-setting', settings.showYous); +setDefaultLocalStorage('disableboardcss', false); setDefaultLocalStorage('dragtop', null); setDefaultLocalStorage('dragleft', null); diff --git a/views/mixins/modal.pug b/views/mixins/modal.pug index fce1d228..e287dc54 100644 --- a/views/mixins/modal.pug +++ b/views/mixins/modal.pug @@ -100,7 +100,7 @@ mixin modal(data) .row label.postform-style.ph-5 input#alwaysshowspoilers-setting(type='checkbox') - .rlabel Always reveal spoilers + .rlabel Always reveal text spoilers .row label.postform-style.ph-5 input#yous-setting(type='checkbox') @@ -109,7 +109,7 @@ mixin modal(data) label.postform-style.ph-5 input#smoothscrolling-setting(type='checkbox') .rlabel Smooth scrolling - .row + .row.mt-5 .label (You)s input.mr-1#youslist-setting(type='text' readonly) input#youslist-clear(type='button' value='Clear') @@ -146,7 +146,7 @@ mixin modal(data) .row .label Custom CSS textarea#customcss-setting(rows=7) - .row + .row.mt-5 form.text-center#filter-form table tbody#advancedfilters @@ -171,3 +171,12 @@ mixin modal(data) td input(type='checkbox' name='regex') td: input.right(type='submit' value='Add') + .row.mt-5 + .label + | Import/Export Settings + small.title Do NOT import untrusted settings data! + small Export does not include post password or (You)'s + textarea#import-export-setting(type='text') + .row + input.mr-1#export-setting(type='button' value='Export') + input#import-setting(type='button' value='Import')