This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
/
sharedFunctions.js
1 lines (1 loc) · 8.74 KB
/
sharedFunctions.js
1
function isEmpty(a){for(let b in a)return!1;return!0}async function storageGet(a){try{null!==a&&(a+="");let b;return CHROME?(await browser.storage.local.get(null===a?null:a,function(c){b=null===a?c:c[a]}),b):(b=await browser.storage.local.get(null===a?null:a),null===a?b:b[a])}catch(a){console.error(a)}}async function storageSet(a,b){try{return await browser.storage.local.set(null===a?b:{[a]:b})}catch(a){console.error(a)}}async function loadOptions(){let a=await storageGet("options");return void 0===a?(SimpleNotification.info("First time using MyMangaDex","It looks like it is your first time using MyMangaDex, for any help go to https://github.com/Glagan/MyMangaDex, and don't forget to look at the settings !",{sticky:!0}),await storageSet("options",defaultOptions),JSON.parse(JSON.stringify(defaultOptions))):(a.version<defaultOptions.version&&(1.7>a.version&&(a.auto_md_list=!1),1.8>a.version&&(a={lastReadColor:a.colors.last_read,lowerChaptersColor:a.colors.lower_chapter,lastOpenColors:a.colors.last_open,openedChaptersColor:a.colors.opened_chapters,hideLowerChapters:a.hide_lower,saveOnlyHigher:a.last_only_higher,saveAllOpened:a.save_all_opened,maxChapterSaved:a.max_save_opened,updateMDList:a.auto_md_list,showTooltips:!0}),1.9>a.version&&(a.highlightChapters=defaultOptions.highlightChapters,a.showNotifications=defaultOptions.showNotifications,a.showErrors=defaultOptions.showErrors,a.version=1.9,SimpleNotificatin.info("MyMangaDex as been updated to 1.9","You can see the changelog on https://github.com/Glagan/MyMangaDex, new options have been added, you should check them out !")),2>a.version&&(a.onlineSave=defaultOptions.onlineSave,a.onlineURL=defaultOptions.onlineURL,a.username=defaultOptions.username,a.password=defaultOptions.password,a.isLoggedIn=defaultOptions.isLoggedIn,a.token=defaultOptions.token,a.version=2,SimpleNotificatin.info("MyMangaDex as been updated to 2.0","Online Save as been added, if you wish to use it you need to manually enable it. You can see the changelog on https://github.com/Glagan/MyMangaDex.")),await storageSet("options",a)),void 0===a.showNoMal&&(a.showNoMal=defaultOptions.showNoMal,await storageSet("options",a)),a)}async function updateLocalStorage(a,b){var c=Math.max;if(a.lastMangaDexChapter=b.saveOnlyHigher?c(a.lastMangaDexChapter,a.currentChapter.chapter):a.currentChapter.chapter,await storageSet(a.mangaDexId,{mal:a.myAnimeListId,last:a.lastMangaDexChapter,chapters:a.chapters}),b.showNotifications&&0==a.myAnimeListId&&a.currentChapter.chapter>a.lastMangaDexChapter&&SimpleNotificatin.success("Manga updated",a.name+" last open Chapter as been updated to "+a.lastMangaDexChapter,{image:"https://mangadex.org/images/manga/"+a.mangaDexId+".thumb.jpg"}),b.onlineSave&&b.isLoggedIn){let c={mal:a.myAnimeListId,last:a.lastMangaDexChapter,options:{saveAllOpened:b.saveAllOpened,maxChapterSaved:b.maxChapterSaved}};try{let d=await fetch(b.onlineURL+"user/self/title/"+a.mangaDexId,{method:"POST",mode:"cors",headers:{Accept:"application/json","Content-Type":"application/json; charset=utf-8","X-Auth-Token":b.token},body:JSON.stringify(c)});200!=d.status&&(b.isLoggedIn=!1)}catch(a){b.isLoggedIn=!1,console.error(a)}!1==b.isLoggedIn&&(b.showErrors&&SimpleNotification.error("Couldn't save Online","The Online Service might have a problem, or your credentials has been changed. Go to the options to update it."),await storageSet("options",b))}}function clearDomNode(a){for(;a.firstChild;)a.removeChild(a.firstChild)}function uniqueGUID(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}function processMyAnimeListResponse(a,b){a.is_approved=!/class="badresult"/.test(b),a.exist=!/id="queryTitle"/.test(b),a.comments=/add_manga_comments.+>(.*)</.exec(b)[1],a.ask_to_discuss=/add_manga_is_asked_to_discuss.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b),a.ask_to_discuss=null===a.ask_to_discuss?0:parseInt(a.ask_to_discuss[1]),a.lastMyAnimeListChapter=/add_manga_num_read_chapters.+value="(\d+)?"/.exec(b),a.lastMyAnimeListChapter=null===a.lastMyAnimeListChapter?0:parseInt(a.lastMyAnimeListChapter[1]),a.total_reread=/add_manga_num_read_times.+value="(\d+)?"/.exec(b),a.total_reread=null===a.total_reread?0:parseInt(a.total_reread[1]),a.last_volume=/add_manga_num_read_volumes.+value="(\d+)?"/.exec(b),a.last_volume=null===a.last_volume?0:parseInt(a.last_volume[1]),a.retail_volumes=/add_manga_num_retail_volumes.+value="(\d+)?"/.exec(b),a.retail_volumes=null===a.retail_volumes?0:parseInt(a.retail_volumes[1]),a.priority=/add_manga_priority.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b),a.priority=null===a.priority?0:parseInt(a.priority[1]),a.reread_value=/add_manga_reread_value.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b),a.reread_value=null===a.reread_value?"":a.reread_value[1],a.score=/add_manga_score.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b),a.score=null===a.score?"":parseInt(a.score[1]),a.sns_post_type=/add_manga_sns_post_type.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b),a.sns_post_type=null===a.sns_post_type?0:parseInt(a.sns_post_type[1]),a.start_date={},a.start_date.month=parseInt(/add_manga_start_date_month.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b)[1])||"",a.start_date.day=parseInt(/add_manga_start_date_day.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b)[1])||"",a.start_date.year=parseInt(/add_manga_start_date_year.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b)[1])||"",a.finish_date={},a.finish_date.month=parseInt(/add_manga_finish_date_month.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b)[1])||"",a.finish_date.day=parseInt(/add_manga_finish_date_day.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b)[1])||"",a.finish_date.year=parseInt(/add_manga_finish_date_year.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b)[1])||"",a.status=/add_manga_status.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b),a.status=null===a.status?0:parseInt(a.status[1]),a.storage_type=/add_manga_storage_type.+\s.+value="(\d+)?"\s*selected="selected"/.exec(b),a.storage_type=null===a.storage_type?"":a.storage_type[1],a.tags=/add_manga_tags.+>(.*)*</.exec(b)[1]||"",a.is_rereading=/name="add_manga\[is_rereading\]"\s*value="\d*"\s*checked="checked"/.test(b),a.total_volume=parseInt(/id="totalVol">(.*)?<\//.exec(b)[1])||0,a.total_chapter=parseInt(/id="totalChap">(.*)?<\//.exec(b)[1])||0,a.in_list=0<a.status}function buildMyAnimeListBody(a,b,c,d=1){var e=Math.floor;let f="https://myanimelist.net/ownlist/manga/"+b.myAnimeListId+"/edit?hideLayout";if(a){if(b.status=2==b.status||0<b.total_chapter&&b.currentChapter.chapter>=b.total_chapter?2:d,!b.in_list&&6!=b.status&&""==b.start_date.year){let a=new Date;b.start_date.year=a.getFullYear(),b.start_date.month=a.getMonth()+1,b.start_date.day=a.getDate(),b.start_today=!0}if(2==b.status&&""==b.finish_date.year){let a=new Date;b.finish_date.year=a.getFullYear(),b.finish_date.month=a.getMonth()+1,b.finish_date.day=a.getDate(),b.end_today=!0}b.in_list||(f="https://myanimelist.net/ownlist/manga/add?selected_manga_id="+b.myAnimeListId+"&hideLayout",b.in_list=!0,b.started=!0),b.is_rereading&&0<b.total_chapter&&b.currentChapter.chapter>=b.total_chapter&&(b.completed=!0,b.is_rereading=!1,b.total_reread++)}else b.status=d;b.lastMyAnimeListChapter=e(b.currentChapter.chapter);let g="entry_id=0&";return g+="manga_id="+b.myAnimeListId+"&",g+=encodeURIComponent("add_manga[status]")+"="+b.status+"&",g+=encodeURIComponent("add_manga[num_read_volumes]")+"="+b.currentChapter.volume+"&",g+="last_completed_vol=&",g+=encodeURIComponent("add_manga[num_read_chapters]")+"="+b.lastMyAnimeListChapter+"&",g+=encodeURIComponent("add_manga[score]")+"="+b.score+"&",g+=encodeURIComponent("add_manga[start_date][day]")+"="+b.start_date.day+"&",g+=encodeURIComponent("add_manga[start_date][month]")+"="+b.start_date.month+"&",g+=encodeURIComponent("add_manga[start_date][year]")+"="+b.start_date.year+"&",g+=encodeURIComponent("add_manga[finish_date][day]")+"="+b.finish_date.day+"&",g+=encodeURIComponent("add_manga[finish_date][month]")+"="+b.finish_date.month+"&",g+=encodeURIComponent("add_manga[finish_date][year]")+"="+b.finish_date.year+"&",g+=encodeURIComponent("add_manga[tags]")+"="+encodeURIComponent(b.tags)+"&",g+=encodeURIComponent("add_manga[priority]")+"="+b.priority+"&",g+=encodeURIComponent("add_manga[storage_type]")+"="+b.storage_type+"&",g+=encodeURIComponent("add_manga[num_retail_volumes]")+"="+b.retail_volumes+"&",g+=encodeURIComponent("add_manga[num_read_times]")+"="+b.total_reread+"&",g+=encodeURIComponent("add_manga[reread_value]")+"="+b.reread_value+"&",g+=encodeURIComponent("add_manga[comments]")+"="+encodeURIComponent(b.comments)+"&",g+=encodeURIComponent("add_manga[is_asked_to_discuss]")+"="+b.ask_to_discuss+"&",g+=encodeURIComponent("add_manga[sns_post_type]")+"="+b.sns_post_type+"&",b.is_rereading&&(g+=encodeURIComponent("add_manga[is_rereading]")+"=1&"),g+="submitIt=0&",g+=encodeURIComponent("csrf_token")+"="+c,{requestURL:f,body:g}}