Export settings but for chrome. Also some fixes for FF

This commit is contained in:
Tamius Han 2019-07-15 23:18:12 +02:00
parent 060d644487
commit e08a7eea74

View File

@ -119,7 +119,12 @@
Import, export, reset settings Import, export, reset settings
</div> </div>
<div class="flex flex-column"> <div class="flex flex-column">
<div v-if="corruptedSettingsWarning" <div v-if="downloadPermissionError"
class="w100 center-text warning-lite"
>
Exporting settings requires the 'downloads' permission. (If you want to export settings without granting 'downloads' permission, you can copy-paste settings from 'Super advanced settings' tab)
</div>
<div v-if="corruptedSettingsError"
class="w100 center-text warning-lite" class="w100 center-text warning-lite"
> >
Settings import failed. The settings file is probably corrupted. Settings import failed. The settings file is probably corrupted.
@ -165,7 +170,8 @@ export default {
ExtensionMode: ExtensionMode, ExtensionMode: ExtensionMode,
VideoAlignment: VideoAlignment, VideoAlignment: VideoAlignment,
stretchThreshold: 0, stretchThreshold: 0,
corruptedSettingsWarning: false, corruptedSettingsError: false,
downloadPermissionError: false,
} }
}, },
created () { created () {
@ -199,23 +205,46 @@ export default {
this.settings.active = JSON.parse(JSON.stringify(this.settings.default)); this.settings.active = JSON.parse(JSON.stringify(this.settings.default));
this.settings.save(); this.settings.save();
}, },
exportSettings() { async exportSettings() {
browser.permissions.request({permissions: ['downloads']}); this.downloadPermissionError = false;
const blob = new Blob([JSON.stringify(this.settings.active)], {type: 'application/json'}); const blob = new Blob([JSON.stringify(this.settings.active)], {type: 'application/json'});
const fileUrl = URL.createObjectURL(blob); const fileUrl = URL.createObjectURL(blob);
if (BrowserDetect.firefox) {
browser.downloads.download({saveAs: true, filename: 'ultrawidify-settings.json', url: fileUrl}); try {
if (BrowserDetect.firefox) {
await browser.permissions.request({permissions: ['downloads']});
browser.downloads.download({saveAs: true, filename: 'ultrawidify-settings.json', url: fileUrl});
} else if (BrowserDetect.chrome) {
const ths = this;
chrome.permissions.request(
{permissions: ['downloads']},
(granted) => {
if (granted) {
ths.exportSettingsChrome(fileUrl);
} else {
ths.downloadPermissionError = true
}
}
)
}
} catch (e) {
this.downloadPermissionError = true;
} }
}, },
exportSettingsChrome(fileUrl){
chrome.downloads.download({saveAs: true, filename: 'ultrawidify-settings.json', url: fileUrl});
},
async importSettings($event) { async importSettings($event) {
let file, text, settingsObj; let file, text, settingsObj;
this.corruptedSettingsWarning = false; this.corruptedSettingsError = false;
try { try {
file = $event.target.files[0]; file = $event.target.files[0];
} catch (e) { } catch (e) {
console.error("error grabbing a file!"); console.error("error grabbing a file!");
this.corruptedSettingsWarning = true; this.corruptedSettingsError = true;
return; return;
} }
@ -224,7 +253,7 @@ export default {
settingsObj = JSON.parse(text); settingsObj = JSON.parse(text);
} catch (e) { } catch (e) {
console.error("error parsing file to json"); console.error("error parsing file to json");
this.corruptedSettingsWarning = true; this.corruptedSettingsError = true;
return; return;
} }
@ -232,7 +261,7 @@ export default {
for (const key in this.settings.default) { for (const key in this.settings.default) {
if (!settingsObj[key]) { if (!settingsObj[key]) {
console.error("corrupted settings!") console.error("corrupted settings!")
this.corruptedSettingsWarning = true; this.corruptedSettingsError = true;
return; return;
} }
} }