aboutsummaryrefslogtreecommitdiff
path: root/js/importexport.js
diff options
context:
space:
mode:
authorEinar Egilsson <einar@einaregilsson.com>2019-12-11 13:17:42 +0000
committerEinar Egilsson <einar@einaregilsson.com>2019-12-11 13:17:42 +0000
commitb53d093463e94096f16f0fd7656fc88d642faa3d (patch)
treeef8b1a0b8f07c2ab965a805421e1564622ac814f /js/importexport.js
parentcdb8d5e3d5af6dc2d4d630481c4385734ecc6a1d (diff)
Completely removed angular.js
Diffstat (limited to 'js/importexport.js')
-rw-r--r--js/importexport.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/js/importexport.js b/js/importexport.js
new file mode 100644
index 0000000..c47b71d
--- /dev/null
+++ b/js/importexport.js
@@ -0,0 +1,89 @@
+// Shows a message explaining how many redirects were imported.
+function showImportedMessage(imported, existing) {
+ if (imported == 0 && existing == 0) {
+ showMessage('No redirects existed in the file.');
+ }
+ if (imported > 0 && existing == 0) {
+ showMessage('Successfully imported ' + imported + ' redirect' + (imported > 1 ? 's.' : '.'), true);
+ }
+ if (imported == 0 && existing > 0) {
+ showMessage('All redirects in the file already existed and were ignored.');
+ }
+ if (imported > 0 && existing > 0) {
+ var m = 'Successfully imported ' + imported + ' redirect' + (imported > 1 ? 's' : '') + '. ';
+ if (existing == 1) {
+ m += '1 redirect already existed and was ignored.';
+ } else {
+ m += existing + ' redirects already existed and were ignored.';
+ }
+ showMessage(m, true);
+ }
+}
+
+function importRedirects(ev) {
+
+ let file = ev.target.files[0];
+ if (!file) {
+ return;
+ }
+ var reader = new FileReader();
+
+ reader.onload = function(e) {
+ var data;
+ try {
+ data = JSON.parse(reader.result);
+ } catch(e) {
+ showMessage('Failed to parse JSON data, invalid JSON: ' + (e.message||'').substr(0,100));
+ return;
+ }
+
+ if (!data.redirects) {
+ showMessage('Invalid JSON, missing "redirects" property');
+ return;
+ }
+
+ var imported = 0, existing = 0;
+ for (var i = 0; i < data.redirects.length; i++) {
+ var r = new Redirect(data.redirects[i]);
+ r.updateExampleResult();
+ if (REDIRECTS.some(function(i) { return new Redirect(i).equals(r);})) {
+ existing++;
+ } else {
+ REDIRECTS.push(r.toObject());
+ imported++;
+ }
+ }
+
+ showImportedMessage(imported, existing);
+
+ saveChanges();
+ renderRedirects();
+ };
+
+ try {
+ reader.readAsText(file, 'utf-8');
+ } catch(e) {
+ showMessage('Failed to read import file');
+ }
+}
+
+function updateExportLink() {
+ var redirects = REDIRECTS.map(function(r) {
+ return new Redirect(r).toObject();
+ });
+
+ let version = chrome.runtime.getManifest().version;
+
+ var exportObj = {
+ createdBy : 'Redirector v' + version,
+ createdAt : new Date(),
+ redirects : redirects
+ };
+
+ var json = JSON.stringify(exportObj, null, 4);
+
+ //Using encodeURIComponent here instead of base64 because base64 always messed up our encoding for some reason...
+ el('#export-link').href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(json);
+}
+
+updateExportLink(); \ No newline at end of file