Commit 4a3416cf authored by Duhoux Pierre-Louis's avatar Duhoux Pierre-Louis

Load AMR V1 Export files

parent 1f376a97
......@@ -694,10 +694,18 @@
"message": "Snippet contains $1 mangas to import.",
"description": "Part of the text describing data to import. Describe how many mangas will be imported. $1 is the number of mangas to import"
},
"ie_has_mangas_v1": {
"message": "Snippet is from All Mangas Reader V1 and contains $1 mangas to import.",
"description": "Part of the text describing data to import. Text used if data to import is in AMR V1 format. Describe how many mangas will be imported. $1 is the number of mangas to import"
},
"ie_has_bookmarks": {
"message": "Snippet contains $1 bookmarks to import.",
"description": "Part of the text describing data to import. Describe how many bookmarks will be imported. $1 is the number of bookmarks to import"
},
"ie_has_bookmarks_v1": {
"message": "Snippet is from All Mangas Reader V1 and contains $1 bookmarks to import.",
"description": "Part of the text describing data to import. Text used if data to import is in AMR V1 format. Describe how many bookmarks will be imported. $1 is the number of bookmarks to import"
},
"ie_import_mangas": {
"message": "Import mangas",
"description": "Label of the button to import only mangas"
......
......@@ -97,53 +97,64 @@ export default {
hasbms: false, // data to import contain bookmarks
importingmangas: false, // currently importing mangas
importingbookmarks: false, // currently importing bookmarks
importcat: "", // category for all imported mangas
importcat: "" // category for all imported mangas
};
},
watch: {
importstr: function(str) {
try {
let res = "";
let imps = JSON.parse(str);
if (imps.mangas && imps.mangas.length > 0) {
this.hasmgs = true;
res += i18n("ie_has_mangas", imps.mangas.length) + "<br/>";
} else {
this.hasmgs = false;
}
if (imps.bookmarks && imps.bookmarks.length > 0) {
this.hasbms = true;
res += i18n("ie_has_bookmarks", imps.bookmarks.length);
} else {
this.hasbms = false;
}
this.importmessage = res;
importstr: function(str) {
try {
let res = "";
let imps = JSON.parse(str);
this.hasmgs = false;
if (imps.mangas) {
if (typeof imps.mangas === "string") {
// AMRV1 export
let mgs = JSON.parse(imps.mangas);
res += i18n("ie_has_mangas_v1", mgs.length) + "<br/>";
this.hasmgs = true;
} else if (imps.mangas.length > 0) {
this.hasmgs = true;
res += i18n("ie_has_mangas", imps.mangas.length) + "<br/>";
}
catch (e) {
this.importmessage = e.name + ": " + e.message
this.hasmgs = false
this.hasbms = false
}
this.hasbms = false;
if (imps.bookmarks) {
if (typeof imps.bookmarks === "string") {
// AMRV1 export
let bms = JSON.parse(imps.bookmarks);
res += i18n("ie_has_bookmarks_v1", bms.length) + "<br/>";
this.hasbms = true;
} else if (imps.bookmarks.length > 0) {
this.hasbms = true;
res += i18n("ie_has_bookmarks", imps.bookmarks.length);
}
}
this.importmessage = res;
} catch (e) {
this.importmessage = e.name + ": " + e.message;
this.hasmgs = false;
this.hasbms = false;
}
}
},
methods: {
i18n: (message, ...args) => i18n(message, ...args),
/**
* Button import file is clicked --> simulate click on hidden file input
* Button import file is clicked --> simulate click on hidden file input
*/
onFocus() {
this.$refs.fileInput.click();
this.$refs.fileInput.click();
},
/**
* File is selected
*/
async handleFileChange(e) {
let reader = new FileReader()
let self = this
reader.readAsText(e.target.files[0])
let reader = new FileReader();
let self = this;
reader.readAsText(e.target.files[0]);
reader.onload = function(event) {
self.importstr = event.target.result
}
self.importstr = event.target.result;
};
},
/**
* Export data to file
......@@ -183,7 +194,7 @@ export default {
//TODO add bookmarks
// create a file containing export and download it
// create a file containing export and download it
let exp = { mangas: mgs /*, bookmarks: bms */ };
var blob = new Blob([JSON.stringify(exp, null, 2)], {
type: "text/json"
......@@ -195,68 +206,86 @@ export default {
});
},
/**
* Import mangas described in importstr in reading list.
* Import mangas described in importstr in reading list.
* Button is accessible only if json is valid and contains mangas
*/
importMangas: async function() {
this.importingmangas = true
let imps = JSON.parse(this.importstr)
if (imps.mangas && imps.mangas.length > 0) {
// add default category if inexistant
if (this.importcat !== "") {
let cats = this.$store.state.options.categoriesStates;
if (-1 === cats.findIndex(cat => cat.name === this.importcat)) {
this.$store.dispatch("addCategory", this.importcat);
}
}
let readall = [];
imps.mangas.forEach(mg => {
// convert manga to something matching readManga requirements
let readmg = {
mirror: mg.m,
name: mg.n,
url: mg.u
}
if (mg.l) readmg.lastChapterReadURL = mg.l
if (mg.r) readmg.read = mg.r
if (mg.p) readmg.update = mg.p
if (mg.d) readmg.display = mg.d
if (mg.c) readmg.cats = mg.c
// add default category if specified
if (this.importcat !== "") {
if (readmg.cats && readmg.cats.length > 0) readmg.cats.push(this.importcat)
else readmg.cats = [this.importcat]
}
readmg.action = "readManga"
readall.push(browser.runtime.sendMessage(readmg))
})
// read all mangas
await Promise.all(readall)
this.importingmangas = true;
let imps = JSON.parse(this.importstr);
// AMR V1 export --> convert it to AMR V2
if (typeof imps.mangas === "string") {
imps.mangas = JSON.parse(imps.mangas).map(mg => {
if (mg.cats && typeof mg.cats === "string")
mg.cats = JSON.parse(mg.cats);
return {
m: mg.mirror,
n: mg.name,
u: mg.url,
l: mg.lastChapterReadURL,
r: mg.read,
p: mg.update,
d: mg.display,
c: mg.cats
};
});
}
if (imps.mangas && imps.mangas.length > 0) {
// add default category if inexistant
if (this.importcat !== "") {
let cats = this.$store.state.options.categoriesStates;
if (-1 === cats.findIndex(cat => cat.name === this.importcat)) {
this.$store.dispatch("addCategory", this.importcat);
}
}
this.importingmangas = false
let readall = [];
imps.mangas.forEach(mg => {
// convert manga to something matching readManga requirements
let readmg = {
mirror: mg.m,
name: mg.n,
url: mg.u
};
if (mg.l) readmg.lastChapterReadURL = mg.l;
if (mg.r) readmg.read = mg.r;
if (mg.p) readmg.update = mg.p;
if (mg.d) readmg.display = mg.d;
if (mg.c) readmg.cats = mg.c;
// add default category if specified
if (this.importcat !== "") {
if (readmg.cats && readmg.cats.length > 0)
readmg.cats.push(this.importcat);
else readmg.cats = [this.importcat];
}
readmg.action = "readManga";
readall.push(browser.runtime.sendMessage(readmg));
});
// read all mangas
await Promise.all(readall);
}
this.importingmangas = false;
},
/**
* Import bookmarks described in importstr in reading list.
* Import bookmarks described in importstr in reading list.
* Button is accessible only if json is valid and contains bookmarks
*/
importBookmarks: function() {
//TODO
//TODO
},
/**
* Import mangas and bookmarks described in importstr in reading list.
* Import mangas and bookmarks described in importstr in reading list.
* Button is accessible only if json is valid and contains data
*/
importAll: function() {
this.importMangas();
this.importBookmarks();
this.importMangas();
this.importBookmarks();
}
}
};
</script>
<style>
.import-text textarea {
font-size: 8pt;
font-size: 8pt;
}
.txtfield {
padding-top: 18px;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment