/* jshint esversion: 9 */ // ==UserScript== // @name Fantia downloader // @name:en Fantia downloader // @name:ja Fantia downloader // @namespace http://tampermonkey.net/ // @version 2.1 // @description Download your Fantia rewards more easily! // @description:en Download your Fantia rewards more easily! // @description:ja Download your Fantia rewards more easily! // @author suzumiyahifumi // @match https://fantia.jp/posts/* // @icon https://www.google.com/s2/favicons?domain=fantia.jp // @require https://code.jquery.com/jquery-3.4.1.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.min.js // @grant none // @downloadURL none // ==/UserScript== (function () { 'use strict'; Date.prototype.Format = function (fmt) { let o = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S": this.getMilliseconds() }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (let k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; }; $('head').append(``); $('nav.scroll-tabs>div').append(`擷取下載`); window.getDownLoadButton = () => { $('div.btn-group-tabs').each((i, div) => { $(div).append(``); $('#set').remove(); }); $("div#page").append(`
`); $("div#page").append(setting.settingCenterTemplate()); setting.renderSettingParams(); setting.paramsTemplate(); return; }; let init = setInterval(() => { if ($('nav.post-next-prev-buttons').length != 0) { if ($('div.btn-group-tabs').length != 0) { window.setting = new Setting(); window.getDownLoadButton(); clearInterval(init); } else { $('#set').remove(); clearInterval(init); } } }, 500); class Setting { constructor() { let authorId = $("h1.fanclub-name>a").attr(`href`).split("/").pop(); const keyArr = [`cookieSave`, `lang`, `authorId_${authorId}`, `generalSaveZIP`, `generalSaveFile`, `authorSaveZIP_${authorId}`, `authorSaveFile_${authorId}`, `dateFormat`]; this.cookieOri = document.cookie; this.cookie = this.cookieParser(keyArr); this.cookieSave = this.cookie.cookieSave || this.defaultCookie(`cookieSave`); this.setCookie({ cookieSave: this.cookieSave, lang: (this.cookieSave == `On`) ? this.cookie.lang : this.getOriLang(), generalSave: this.defaultCookie(`generalSave`), authorSave: this.defaultCookie(`authorSave`), authorSaveCheck: this.cookie[`authorId_${authorId}`] || this.defaultCookie(`authorSaveCheck`), authorId: authorId, dateFormat: (this.cookieSave == `On`) ? this.cookie.dateFormat : this.defaultCookie(`dateFormat`) }); if (this.cookieSave == `On`) { let g = this.cookieParser([`generalSaveZIP`, `generalSaveFile`]); this.setCookie({ generalSave: { zipName: g.generalSaveZIP, fileName: g.generalSaveFile } }); if (this.cookie[`authorId_${authorId}`] || this.cookie[`authorId_${authorId}`] == `On`) { let a = this.cookieParser([`authorSaveZIP_${authorId}`, `authorSaveFile_${authorId}`]); this[`authorId_${authorId}`] = `On`; this.setCookie({ authorSaveCheck: `On`, authorSave: { zipName: a[`authorSaveZIP_${authorId}`], fileName: a[`authorSaveFile_${authorId}`] } }); } } return this; } saveCookie() { if (this.cookie.cookieSave == 'On') { let date = new Date(); date.setDate(date.getDate() + 75); let cookie = { cookieSave: this.cookie.cookieSave, lang: this.cookie.lang, dateFormat: this.cookie.dateFormat, domain: `https://fantia.jp`, path: `/`, expires: date.toUTCString() }; cookie.generalSaveZIP = this.cookie.generalSave.zipName; cookie.generalSaveFile = this.cookie.generalSave.fileName; if (this[`authorId_${this.authorId}`] == 'On') { cookie[`authorId_${this.authorId}`] = 'On'; cookie[`authorSaveZIP_${this.authorId}`] = this.cookie.authorSave.zipName; cookie[`authorSaveFile_${this.authorId}`] = this.cookie.authorSave.fileName; } this.updateCookie(cookie); } else { let date = new Date(); date.setDate(date.getTime() - 1); let cookie = { cookieSave: 'Off', expires: date.toUTCString() }; cookie[`authorId_${this.authorId}`] = 'Off'; this.updateCookie(cookie); } return alert(this.getDefault(`saveMessage`, this.lang)); } updateCookie(cookie = undefined) { for (let [key, value] of Object.entries(cookie)) { document.cookie = `${key}=${value}`; } this.cookieOri = document.cookie; return this.cookieOri; } cookieParser(keyArr) { return (typeof keyArr == `string`) ? document.cookie.split('; ').map(row => row.split('=')).filter(row => keyArr == row[0])[1] || undefined : Object.fromEntries(document.cookie.split('; ').map(row => row.split('=')).filter(row => keyArr.includes(row[0]))); } setCookie(settingObj) { for (let [key, value] of Object.entries(settingObj)) { this.cookie[key] = value; this[key] = value; } return this.cookie; } setLang(Lang) { let lang = Lang || this.getOriLang().toLowerCase(); if (/^ja\b/.test(lang)) this.lang = `ja`; if (/^zh\b/.test(lang)) this.lang = `zh`; return this.lang; } getOriLang() { let res = `en`; let lang = (navigator.language || navigator.browserLanguage).toLowerCase(); if (/^ja\b/.test(lang)) res = `ja`; if (/^zh\b/.test(lang)) res = `zh`; return res; } defaultCookie(key) { let defaultSetting = { cookieSave: "Off", lang: this.getOriLang(), generalSave: { zipName: `{postTitle}_{boxTitle}`, fileName: `{imgIndex:0}` }, authorSave: { zipName: `[{user}]{postTitle}_{boxTitle}`, fileName: `{boxTitle}_{imgIndex:0}` }, authorSaveCheck: `Off`, dateFormat: `yyyyMMdd` }; return (key) ? defaultSetting[key] : defaultSetting; } getDefault(key, lang = undefined) { let defaultSetting = { en: { downloadImg: `Download Images`, retrieving: `Retrieving Link`, zipping: `Zipping`, processing: `Processing`, done: `Done`, // 按紐 settingCenter: `Setting`, cookieButton: `Cookie`, languageButton: `言語設定/Language/界面語言`, generalSave: `Global Save`, authorSave: `Author Save`, zipName: `ZIP file name`, fileName: `image file name`, dateFormat: `Date Format`, saveSetting: `Apply Setting`, closeSetting: `Close`, tableParams: `Parameter`, tableMean: `Mean`, saveMessage: `Setting has save!`, // 參數 user: `Creator Name`, uid: `Creator ID`, postTitle: `Post Title`, postId: `Post ID`, boxTitle: `Image Box Title`, "imgIndex:0": `Index for image, Number is first index number.`, postDate: `Post Date`, taskDate: `Download Date`, ext: `Filename Extension` }, ja: { downloadImg: `ダウンロード`, retrieving: `擷取連結中`, zipping: `zip圧縮`, processing: `圧縮`, done: `ダウンロード完了`, // 按紐 settingCenter: `設定`, cookieButton: `Cookie`, languageButton: `Language/言語設定/界面語言`, generalSave: `保存の設定`, authorSave: `クリエイターに保存`, zipName: `ZIPフォルダ`, fileName: `ファイル`, dateFormat: `日期時間格式`, saveSetting: `設定を保存する`, closeSetting: `閉める`, tableParams: `パラメータ`, tableMean: `效果`, saveMessage: `設定が保存しだ!`, // 參數 user: `クリエイターの名前`, uid: `クリエイターのID`, postTitle: `投稿のタイトル`, postId: `投稿のID`, boxTitle: `イラストボックスのタイトル`, "imgIndex:0": `画像のインデックス、番号は最初のインデックス番号です。`, postDate: `リリース時間`, taskDate: `ダウンロード時間`, ext: `拡張子` }, zh: { downloadImg: `全圖片下載`, retrieving: `擷取連結中`, zipping: `壓縮檔案中`, processing: `壓縮`, done: `下載已完成`, // 按紐 settingCenter: `設定`, cookieButton: `Cookie`, languageButton: `Language/界面語言/言語設定`, generalSave: `全域設定`, authorSave: `作者特訂`, zipName: `壓縮檔檔名`, fileName: `圖片檔檔名`, dateFormat: `日期時間格式`, saveSetting: `儲存設定`, closeSetting: `關閉`, tableParams: `參數`, tableMean: `效果`, saveMessage: `設定已儲存!`, // 參數 user: `創作者名稱`, uid: `創作者 ID`, postTitle: `投稿標題`, postId: `投稿 ID`, boxTitle: `圖片區標題`, "imgIndex:0": `檔案排序號,數字決定起始數字`, postDate: `投稿日期`, taskDate: `下載日期`, ext: `檔案副檔名` } }; return (key) ? defaultSetting[lang || this.lang][key] : defaultSetting[lang || this.lang]; } renderSettingParams() { $(`#cookie${this.cookieSave}`).attr("checked", true); $(`#${(this.authorSaveCheck == `On`)? `authorSave` : `generalSave` }`).attr("checked", true); $(`#selectSetting option[value='${this.lang}']`).attr("selected", true); $(`#zipNameInput`).val(this[(this.authorSaveCheck == `On`) ? `authorSave` : `generalSave`].zipName); $(`#fileNameInput`).val(this[(this.authorSaveCheck == `On`) ? `authorSave` : `generalSave`].fileName); $(`#dateFormatInput`).val(this.dateFormat); $(`#paramsTable`).html(this.paramsTemplate(this.lang)); return; } changeStyle(mode) { } changeLang() { let lang = $("#selectSetting option:selected").val(); this.setCookie({ lang: lang }); $("#titleSetting").text(this.getDefault(`settingCenter`, lang)); $("#cookieSetting").text(this.getDefault(`cookieButton`, lang)); $("#langSetting").text(this.getDefault(`languageButton`, lang)); $("#generalSaveSetting").text(this.getDefault(`generalSave`, lang)); $("#authorSaveSetting").text(this.getDefault(`authorSave`, lang)); $("#zipNameSetting").text(this.getDefault(`zipName`, lang)); $("#fileNameSetting").text(this.getDefault(`fileName`, lang)); $("#dateFormatSetting").text(this.getDefault(`dateFormat`, lang)); $("#saveSetting").text(this.getDefault(`saveSetting`, lang)); $("#closeSetting").text(this.getDefault(`closeSetting`, lang)); $(".downloadSpan").text(this.getDefault('downloadImg', lang)); $("#paramsTable").html(this.paramsTemplate(this.lang)); return; } changeName(type) { let name = $(`#${type}NameInput`).val(); let setting = $(`input[name='saveCheck']:checked`).attr("id"); if (name == ``) { this.changeSaveSetting(setting); } else { this[setting][`${type}Name`] = name; } return; } changeSaveSetting(id) { let setting = this[id]; $(`#zipNameInput`).val(setting.zipName); $(`#zipNameInput`).attr("placeholder", setting.zipName); $(`#fileNameInput`).val(setting.fileName); $(`#fileNameInput`).attr("placeholder", setting.fileName); return; } paramsTemplate(lang = this.lang) { let table = `| ${setting.getDefault(`tableParams`, lang)} | ${setting.getDefault(`tableMean`, lang)} |
|---|---|
| {${p}} | ${setting.getDefault(p, lang)} |
${this.getDefault(`settingCenter`, lang)}
${this.getDefault(`languageButton`, lang)}
${this.getDefault(`zipName`, lang)}
.zip
${this.getDefault(`fileName`, lang)}
.{ext}
${this.getDefault(`dateFormat`, lang)}
${this.getDefault(`saveSetting`, lang)}
${this.getDefault(`closeSetting`, lang)}