2019-11-11 14:40:30 +00:00
|
|
|
import {browser} from 'webextension-polyfill-ts';
|
|
|
|
import {
|
|
|
|
querySelector,
|
|
|
|
flashMessage,
|
|
|
|
Settings,
|
|
|
|
log,
|
|
|
|
getSettings,
|
|
|
|
isValidTildesUsername,
|
|
|
|
isValidHexColor,
|
|
|
|
setSettings
|
|
|
|
} from './utilities';
|
|
|
|
|
|
|
|
export async function importSettingsHandler(event: MouseEvent): Promise<void> {
|
|
|
|
event.preventDefault();
|
|
|
|
const fileInput: HTMLInputElement = querySelector('#import-file');
|
|
|
|
fileInput.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function importFileHandler(event: Event): Promise<void> {
|
|
|
|
const fileList: FileList | null = (event.target as HTMLInputElement).files;
|
|
|
|
if (fileList === null) {
|
|
|
|
flashMessage('No file imported.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const reader: FileReader = new FileReader();
|
|
|
|
reader.addEventListener(
|
|
|
|
'load',
|
|
|
|
async (): Promise<void> => {
|
|
|
|
let data: Partial<Settings>;
|
|
|
|
try {
|
2020-06-05 21:55:11 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
2019-11-11 14:40:30 +00:00
|
|
|
data = JSON.parse(reader.result!.toString());
|
|
|
|
} catch (error) {
|
|
|
|
log(error, true);
|
|
|
|
flashMessage(error, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const settings: Settings = await getSettings();
|
|
|
|
const newSettings: Settings = {...settings};
|
|
|
|
if (typeof data.data !== 'undefined') {
|
|
|
|
if (typeof data.data.userLabels !== 'undefined') {
|
|
|
|
newSettings.data.userLabels = [];
|
|
|
|
for (const label of data.data.userLabels) {
|
|
|
|
if (
|
|
|
|
typeof label.username === 'undefined' ||
|
|
|
|
!isValidTildesUsername(label.username)
|
|
|
|
) {
|
|
|
|
log(`Invalid username in imported labels: ${label.username}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
newSettings.data.userLabels.push({
|
2020-10-03 11:20:12 +00:00
|
|
|
color: isValidHexColor(label.color) ? label.color : '#f0f',
|
2019-11-11 14:40:30 +00:00
|
|
|
id: newSettings.data.userLabels.length + 1,
|
2020-06-05 21:55:11 +00:00
|
|
|
priority: Number.isNaN(label.priority) ? 0 : label.priority,
|
2019-11-11 14:40:30 +00:00
|
|
|
text: typeof label.text === 'undefined' ? 'Label' : label.text,
|
|
|
|
username: label.username
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 21:18:48 +00:00
|
|
|
|
|
|
|
if (typeof data.data.hideVotes !== 'undefined') {
|
|
|
|
newSettings.data.hideVotes = data.data.hideVotes;
|
|
|
|
}
|
2019-11-11 14:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof data.features !== 'undefined') {
|
|
|
|
newSettings.features = {...data.features};
|
|
|
|
}
|
|
|
|
|
|
|
|
await setSettings(newSettings);
|
|
|
|
flashMessage(
|
|
|
|
'Successfully imported your settings, reloading the page to apply.'
|
|
|
|
);
|
|
|
|
setTimeout(() => {
|
|
|
|
window.location.reload();
|
|
|
|
}, 2500);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
reader.addEventListener('error', (): void => {
|
|
|
|
log(reader.error, true);
|
|
|
|
reader.abort();
|
|
|
|
});
|
|
|
|
reader.readAsText(fileList[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function exportSettingsHandler(event: MouseEvent): Promise<void> {
|
|
|
|
event.preventDefault();
|
|
|
|
const settings: Settings = await getSettings();
|
|
|
|
const settingsBlob: Blob = new Blob([JSON.stringify(settings, null, 2)], {
|
|
|
|
type: 'text/json'
|
|
|
|
});
|
|
|
|
const blobObjectURL: string = URL.createObjectURL(settingsBlob);
|
|
|
|
try {
|
|
|
|
await browser.downloads.download({
|
|
|
|
filename: 'tildes_reextended-settings.json',
|
|
|
|
url: blobObjectURL,
|
|
|
|
saveAs: true
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
} finally {
|
|
|
|
// According to MDN, when creating an object URL we should also revoke it
|
|
|
|
// when "it's safe to do so" to prevent excess memory/storage use. 60
|
|
|
|
// seconds should be enough time to download the settings.
|
|
|
|
setTimeout(() => URL.revokeObjectURL(blobObjectURL), 60000);
|
|
|
|
}
|
|
|
|
}
|