Skip to content

Commit

Permalink
Merge pull request #1490 from gamebeaker/Library_export_import
Browse files Browse the repository at this point in the history
Library export import function
  • Loading branch information
gamebeaker authored Sep 18, 2024
2 parents 0a78fc2 + 6ce91a2 commit 02a5963
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 10 deletions.
8 changes: 8 additions & 0 deletions plugin/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
"message": "Clear Library",
"description": "Label on button to clear Local Storage"
},
"__MSG_button_Lib_Template_Export_Library__": {
"message": "Export Library",
"description": "Label on button to export Library in json format"
},
"__MSG_button_Lib_Template_Import_Library__": {
"message": "Import Library",
"description": "Label on button to import Library in json format"
},
"__MSG_button_Lib_Template_Upload_Epub__": {
"message": "Upload Epub",
"description": "Label on button to Upload Epub in Library"
Expand Down
106 changes: 98 additions & 8 deletions plugin/js/Library.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ class Library {
}
if (LibidURL == -1) {
Library.LibHandelUpdate(-1, AddEpub, document.getElementById("startingUrlInput").value, fileName.replace(".epub", ""), LibidURL);
return Download.save(AddEpub, fileName, overwriteExisting, backgroundDownload);
if (document.getElementById("LibDownloadEpubAfterUpdateCheckbox").checked) {
return Download.save(AddEpub, fileName, overwriteExisting, backgroundDownload);
}else{
return new Promise((resolve) => {resolve();});
}
}

fileName = EpubPacker.addExtensionIfMissing(items["LibFilename" + LibidURL]);
Expand All @@ -44,10 +48,6 @@ class Library {
}
});
}

onUserPreferencesUpdate(userPreferences) {
Library.userPreferences = userPreferences;
}

static async LibMergeEpub(PreviousEpub, AddEpub, LibidURL){
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -241,8 +241,23 @@ class Library {
}

static Libdeleteall(){
chrome.storage.local.clear();
Library.LibRenderSavedEpubs();
chrome.storage.local.get(null, async function(items) {
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub");
let storyurls = [];
for (let i = 0; i < CurrentLibKeys.length; i++) {
CurrentLibKeys[i] = CurrentLibKeys[i].replace("LibEpub","");
}
for (let i = 0; i < CurrentLibKeys.length; i++) {
storyurls[i] = items["LibStoryURL" + CurrentLibKeys[i]];
}
let userPreferences = new UserPreferences;
userPreferences = UserPreferences.readFromLocalStorage();
for (let i = 0; i < storyurls.length; i++) {
userPreferences.readingList.tryDeleteEpubAndSave(storyurls[i]);
}
chrome.storage.local.clear();
Library.LibRenderSavedEpubs();
});
}

static LibRenderSavedEpubs(){
Expand Down Expand Up @@ -271,6 +286,10 @@ class Library {
LibTemplateMergeUploadButton = document.getElementById("LibTemplateMergeUploadButton").innerHTML;
LibTemplateEditMetadataButton = document.getElementById("LibTemplateEditMetadataButton").innerHTML;
LibRenderString += "<button id='libdeleteall'>"+document.getElementById("LibTemplateClearLibrary").innerHTML+"</button>";
LibRenderString += "<button id='libexportall'>"+document.getElementById("LibTemplateExportLibrary").innerHTML+"</button>";
LibRenderString += "<label data-libbuttonid='LibImportLibraryButton' data-libepubid='' id='LibImportLibraryLabel' for='LibImportLibraryFile' style='cursor: pointer;'>";
LibRenderString += "<button id='LibImportLibraryButton' style='pointer-events: none;'>"+document.getElementById("LibTemplateImportEpubButton").innerHTML+"</button></label>";
LibRenderString += "<input type='file' data-libepubid='LibImportLibrary' id='LibImportLibraryFile' hidden>";
LibRenderString += "<br>";
LibRenderString += "<p>"+document.getElementById("LibTemplateUploadEpubFileLabel").innerHTML+"</p>";
LibRenderString += "<label data-libbuttonid='LibUploadEpubButton' data-libepubid='' id='LibUploadEpubLabel' for='LibEpubNewUploadFile' style='cursor: pointer;'>";
Expand Down Expand Up @@ -330,6 +349,10 @@ class Library {
Library.AppendHtmlInDiv(LibRenderString, LibRenderResult, "LibDivRenderWraper");
if (ShowAdvancedOptions) {
document.getElementById("libdeleteall").addEventListener("click", function(){Library.Libdeleteall()});
document.getElementById("libexportall").addEventListener("click", function(){Library.Libexportall()});
document.getElementById("LibImportLibraryLabel").addEventListener("mouseover", function(){Library.LibMouseoverButtonUpload(this)});
document.getElementById("LibImportLibraryLabel").addEventListener("mouseout", function(){Library.LibMouseoutButtonUpload(this)});
document.getElementById("LibImportLibraryFile").addEventListener("change", function(){Library.LibHandelImport(this)});
document.getElementById("LibUploadEpubLabel").addEventListener("mouseover", function(){Library.LibMouseoverButtonUpload(this)});
document.getElementById("LibUploadEpubLabel").addEventListener("mouseout", function(){Library.LibMouseoutButtonUpload(this)});
document.getElementById("LibEpubNewUploadFile").addEventListener("change", function(){Library.LibHandelUpdate(this, -1, "", "", -1)});
Expand Down Expand Up @@ -560,6 +583,7 @@ class Library {
LibFileReader.LibStorageValueId = Id;
LibFileReader.readAsDataURL(Blobdata);
}

static async LibFileReaderload(){
if (-1 == LibFileReader.LibStorageValueId) {
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub");
Expand Down Expand Up @@ -622,7 +646,9 @@ class Library {

static LibDeleteEpub(objbtn){
let LibRemove = ["LibEpub" + objbtn.dataset.libepubid, "LibStoryURL" + objbtn.dataset.libepubid, "LibFilename" + objbtn.dataset.libepubid, "LibCover" + objbtn.dataset.libepubid];
Library.userPreferences.readingList.tryDeleteEpubAndSave(document.getElementById("LibStoryURL" + objbtn.dataset.libepubid).value);
let userPreferences = new UserPreferences;
userPreferences = UserPreferences.readFromLocalStorage();
userPreferences.readingList.tryDeleteEpubAndSave(document.getElementById("LibStoryURL" + objbtn.dataset.libepubid).value);
chrome.storage.local.remove(LibRemove);
Library.LibRenderSavedEpubs();
}
Expand Down Expand Up @@ -654,6 +680,70 @@ class Library {
});
}

static Libexportall(){
chrome.storage.local.get(null, async function(items) {
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub");
var retobj = {};
retobj.Library = [];
for (let i = 0; i < CurrentLibKeys.length; i++) {
CurrentLibKeys[i] = CurrentLibKeys[i].replace("LibEpub","");
}
for (let i = 0; i < CurrentLibKeys.length; i++) {
retobj.Library[i] = {};
retobj.Library[i].LibCover = items["LibCover" + CurrentLibKeys[i]];
retobj.Library[i].LibEpub = items["LibEpub" + CurrentLibKeys[i]];
retobj.Library[i].LibFilename = items["LibFilename" + CurrentLibKeys[i]];
retobj.Library[i].LibStoryURL = items["LibStoryURL" + CurrentLibKeys[i]];
}
let storyurls = retobj.Library.map(a => a.LibStoryURL);
let readingList = new ReadingList();
readingList.readFromLocalStorage();
retobj.ReadingList = JSON.parse(readingList.toJson());
retobj.ReadingList.epubs = retobj.ReadingList.epubs.filter(a => storyurls.includes(a.toc));
let serialized = JSON.stringify(retobj);
let blob = new Blob([serialized], {type : "application/json"});
return Download.save(blob, "Libraryexport.json").catch (err => ErrorLog.showErrorMessage(err));
});
}

static async LibHandelImport(objbtn){
Library.LibShowLoadingText();
await Library.LibFileReaderAddListenersImport(LibFileReader);
let Blobdata = objbtn.files[0];
LibFileReader.readAsText(Blobdata);
}

static LibFileReaderAddListenersImport(LibFileReader){
LibFileReader.addEventListener("load", function(){Library.LibFileReaderloadImport()});
LibFileReader.addEventListener("error", function(event){Library.LibFileReadererror(event)});
LibFileReader.addEventListener("abort", function(event){Library.LibFileReaderabort(event)});
}

static async LibFileReaderloadImport(){
let json = JSON.parse(LibFileReader.result);
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub");
let HighestLibEpub = 0;
CurrentLibKeys.forEach(element => {
element = element.replace("LibEpub","");
if (parseInt(element)>=HighestLibEpub) {
HighestLibEpub = parseInt(element)+1;
}
});
for (let i = 0; i < json.Library.length; i++) {
chrome.storage.local.set({
["LibEpub" + HighestLibEpub]: json.Library[i].LibEpub,
["LibStoryURL" + HighestLibEpub]: json.Library[i].LibStoryURL,
["LibCover" + HighestLibEpub]: json.Library[i].LibCover,
["LibFilename" + HighestLibEpub]: json.Library[i].LibFilename
});
HighestLibEpub++;
}
let userPreferences = new UserPreferences;
userPreferences = UserPreferences.readFromLocalStorage();
userPreferences.loadReadingListFromJson(json);
Library.LibRenderSavedEpubs();
}

static LibSaveTextURLChange(obj){
let LibGetFileAndName = obj.id;
chrome.storage.local.set({
Expand Down
1 change: 0 additions & 1 deletion plugin/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var main = (function () {
if (setParser(url, dom)) {
try {
userPreferences.addObserver(parser);
userPreferences.addObserver(library);
let metaInfo = parser.getEpubMetaInfo(dom, userPreferences.useFullTitle.value);
populateMetaInfo(metaInfo);
setUiToDefaultState();
Expand Down
2 changes: 1 addition & 1 deletion plugin/js/parsers/GenesiStudioParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ parserFactory.register("genesistudio.com", () => new GenesiStudioParser());
class GenesiStudioParser extends Parser{
constructor() {
super();
this.minimumThrottle = 3000;
this.minimumThrottle = 1000;
}

clampSimultanousFetchSize() {
Expand Down
2 changes: 2 additions & 0 deletions plugin/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ <h3>Instructions</h3>
<tr>
<td id="LibTemplateLibraryUses">Library uses: </td>
<td colspan="2"><button id="LibTemplateClearLibrary">__MSG_button_Lib_Template_Clear_Library__</button></td>
<td colspan="2"><button id="LibTemplateExportLibrary">__MSG_button_Lib_Template_Export_Library__</button></td>
<td colspan="2"><button id="LibTemplateImportEpubButton">__MSG_button_Lib_Template_Import_Library__</button></td>
</tr>
<tr>
<td id="LibTemplateUploadEpubFileLabel">__MSG_label_Lib_Template_Upload_Epub_File_Label__</td>
Expand Down

0 comments on commit 02a5963

Please sign in to comment.