// ==UserScript==
// @name 千图网收藏夹整理工具
// @namespace https://www.52pojie.cn/home.php?mod=space&uid=508077
// @version 1.4
// @description 解决收藏管理操作繁琐的问题
// @author 未知的动力
// @match *://*.58pic.com/c/*
// @match *://*.58pic.com/collect/*
// @match *://*.58pic.com/u/*/c/create
// @run-at document-end
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// ==/UserScript==
const URL_GET_USERFAV_LIST="https://www.58pic.com/index.php?m=userHomePage&a=favoritePageList",URL_POST_USERFAV="https://www.58pic.com/index.php?m=user&a=ajaxFav",URL_GET_USERID_CURRENT="https://www.58pic.com/index.php?m=ajaxGetUserInfo&a=userInfo",LOCALSTORAGE_USERFAV_KEY="wzddl_userFav_dir",LOCALSTORAGE_USERFAV_LASTSELECTED="wzddl_userFav_lastSelected",DOM_USERSELECTED_FAVID="data-userFav-id",GLOBAL_USERSELECTED_FAVID_CURRENTPAGE="wzddl_userFav_userSelected_currentPage",GLOBAL_DIRID_CURRENTPAGE="wzddl_dirID_currentPage",GLOBAL_ELEMENT_MSGBOX="wzddl_ele_msgBox",GLOBAL_ELEMENT_MSGBOX_TEXT="wzddl_ele_msgBox_text",PAGE_DIR="PAGE_DIR",PAGE_FAV="PAGE_FAV",PAGE_FAV_ANOTHER="PAGE_FAV_ANOTHER",CSS_ANI_TIPS_SHOW="wzddl-ani-tipsShow";function FN_WZDDL_errorHandle(e){}function FN_WZDDL_msgShow(e){window.wzddl_ele_msgBox.style.display="flex",window.wzddl_ele_msgBox.classList.add("wzddl-ani-tipsShow"),window.wzddl_ele_msgBox_text.textContent=""==e?"操作完成":e}function FN_WZDDL_addStyle(){GM_addStyle('\n .favorites-nav,\n .theme-nav {\n background-color: #00bb77;\n color: #fff !important;\n }\n .wzddl-fav-frame {\n width: 100%;\n max-width: 1200px;\n background-color: #00bb77;\n color: #fff !important;\n margin: 0 auto;\n display: flex;\n flex-direction: row;\n justify-content: flex-start;\n border-top: 1px solid #ccc;\n border-bottom: 1px solid #ccc;\n flex-wrap: wrap;\n padding: 10px 20px;\n box-sizing: border-box;\n border-radius: 2px;\n }\n .wzddl-fav-frame .item {\n padding: 0 10px;\n height: 35px;\n line-height: 35px;\n width: 12.5%;\n min-width: 50px;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n transition: all 0.25s;\n display: flex;\n align-items: center;\n }\n .wzddl-fav-frame .item label {\n display: flex;\n color: #fff;\n font-size: 15px;\n font-weight: 700;\n flex-direction: row;\n align-items: center;\n position: relative;\n }\n .wzddl-fav-frame .item label input[type="radio"] {\n position: absolute;\n }\n .wzddl-fav-frame .item label input[type="radio"]::after {\n content: "";\n background-color: #fff;\n border-radius: 50%;\n width: 12px;\n height: 12px;\n display: inline-block;\n position: absolute;\n top: -2px;\n border: 2px solid #00bb77;\n }\n .wzddl-fav-frame .item label input[type="radio"]:checked::after {\n background-color: #363636;\n border: 2px solid #fff;\n }\n .wzddl-fav-frame .item label span {\n margin-left: 20px;\n }\n .wzddl-fav-frame .item:hover {\n cursor: pointer;\n background-color: #363636;\n transform: scale(1.1);\n box-shadow: 0 5px 10px 3px rgba(0, 0, 0, 0.2);\n border-radius: 2px;\n }\n .wzddl-fav-frame .item:hover label {\n cursor: pointer;\n }\n .wzddl-fav-frame .item:hover label input[type="radio"]::after {\n border: 2px solid #363636;\n }\n .wzddl-fav-frame .item:hover label input[type="radio"]:checked::after {\n border: 2px solid #00bb77;\n }\n'),GM_addStyle("\n .wzddl-favBox-control-frame {\n width: 50px;\n height: auto;\n display: flex;\n flex-direction: column;\n box-sizing: border-box;\n position: absolute;\n left: 10px;\n top: 20px;\n transition: all 0.25s;\n }\n .wzddl-favBox-control-frame .wzddl-favBox-controlBtn {\n width: 100%;\n height: 25px;\n line-height: 25px;\n z-index: 999;\n cursor: pointer;\n margin-top: 5px;\n background-color: rgba(0, 0, 0, 0.25);\n text-align: center;\n font-size: 14px;\n color: #fff;\n border-radius: 3px;\n user-select: none;\n transition: all 0.25s;\n }\n .wzddl-favBox-control-frame .wzddl-favBox-controlBtn:nth-of-type(1) {\n margin-top: 35px;\n }\n .wzddl-favBox-control-frame .wzddl-favBox-controlBtn:hover {\n background-color: #00bb77;\n box-shadow: 0 2px 5px 2px rgba(0, 0, 0, 0.25);\n }\n"),GM_addStyle("\n .wzddl-control-tipsBox {\n width: 360px;\n height: 56px;\n position: absolute;\n background-color: rgba(0, 0, 0, 0.75);\n border-radius: 3px;\n display: none;\n align-items: center;\n justify-content: center;\n box-sizing: border-box;\n padding: 0 15px;\n left: 50%;\n transform: translateX(-50%);\n top: 80px;\n z-index: 99999;\n color: #fff;\n user-select: none;\n }\n .wzddl-control-tipsBox span {\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n }\n .wzddl-control-tipsBox .icon-tips {\n font-size: 20px;\n }\n .wzddl-control-tipsBox span.tips {\n max-width: 270px;\n margin-left: 12px;\n }\n .wzddl-ani-tipsShow {\n animation: wzddl-Ani-ShowTips 0.25s;\n }\n .wzddl-ani-tipsHidden {\n animation: wzddl-Ani-HideTips 0.25s;\n }\n @keyframes wzddl-Ani-ShowTips {\n 0% {\n top: 0px;\n }\n 100% {\n top: 80px;\n }\n }\n @keyframes wzddl-Ani-HideTips {\n 0% {\n opacity: 1;\n }\n 100% {\n opacity: 0;\n }\n }\n")}function FN_WZDDL_createUI_navBar(){const e=document.querySelector(".favorites-nav,.theme-nav");if(!e)return;e.style.height="auto";const t=GM_getValue("wzddl_userFav_dir",null),n=GM_getValue("wzddl_userFav_lastSelected",null);if(null==t)return void FN_WZDDL_msgShow("用户没有收藏夹数据,需要更新");const o=document.createElement("div");o.classList.add("wzddl-fav-frame");for(let e=0;e<t.length;e++){const d=document.createElement("div"),a=document.createElement("label"),r=document.createElement("span"),i=document.createElement("input");d.classList.add("item"),d.setAttribute("data-userFav-id",t[e].id),a.setAttribute("for",`item-${e}`),r.textContent=`${t[e].name}`,i.type="radio",i.name="moveTo",i.id=`item-${e}`,i.value=`${t[e]}`,i.addEventListener("click",FN_WZDDL_event_favPage_selected,!1),n&&t[e].id==n.id&&(i.checked=!0,window.wzddl_userFav_userSelected_currentPage=t[e].id),a.append(i,r),d.append(a),o.append(d)}if(e.append(o),!n){document.querySelector(".wzddl-fav-frame .item:nth-of-type(1) label input").checked=!0;const e=document.querySelector(".wzddl-fav-frame .item:nth-of-type(1)"),t=e&&e.getAttribute("data-userFav-id");GM_setValue("wzddl_userFav_lastSelected",{id:t}),window.wzddl_userFav_userSelected_currentPage=t}const d=document.querySelector(".head-box"),a=document.querySelector(".favorites-banner-container"),r=document.querySelector(".favorites-nav,.theme-nav"),i=document.querySelector(".favorites-nav,.theme-nav");let l=null;if(d&&a&&r){const e=parseFloat(window.getComputedStyle(d).height),t=parseFloat(window.getComputedStyle(a).height),n=parseFloat(window.getComputedStyle(r).height);l=e+t;const o=parseFloat(window.getComputedStyle(i).paddingTop);document.addEventListener("scroll",(function(){window.requestAnimationFrame((()=>{window.scrollY>=l-10?(r.style.position="fixed",r.style.top="-200px",r.style["z-index"]=9999,i.style.paddingTop=`${o+n}px`):(r.style.position="relative",r.style.top="auto",i.style.paddingTop=`${o}px`)}))}),!1)}}function FN_wzddl_createUI_favBox(e){const t=window.location.href;let n=/.+?:\/\/.+.58pic.com\/(?:(?:c)|(?:collect))\/(\d+)/gi;n.lastIndex=0;const o=n.exec(t);o&&o[1]&&(window.wzddl_dirID_currentPage=o[1]);const d=document.querySelectorAll(".favorites-pic-floor .qtd-card-container .qtd-card"),a=document.querySelectorAll(".theme-pic-floor .content .qtd-card-container .qtd-card");let r=[];if(d&&d.length>0?r=d:a&&a.length&&(r=a),r)for(let t=0;t<r.length;t++){const n=r[t].querySelector("a"),o=n&&n.getAttribute("data-id");let d=document.createElement("div");d.classList.add("wzddl-favBox-control-frame");let a=document.createElement("span"),i=document.createElement("span");a.classList.add("wzddl-favBox-controlBtn","move-asset"),i.classList.add("wzddl-favBox-controlBtn","copy-asset"),a.textContent="移动","PAGE_FAV"==e?i.textContent="复制":"PAGE_FAV_ANOTHER"==e&&(i.textContent="收藏"),a.wzddl_assetID=o,i.wzddl_assetID=o,a.addEventListener("click",FN_WZDDL_event_moveOrCopyFav.bind(this,{controlType:"move",currentFavBox:r[t]}),!1),i.addEventListener("click",FN_WZDDL_event_moveOrCopyFav.bind(this,{controlType:"copy",currentFavBox:r[t]}),!1),"PAGE_FAV"==e?d.append(a,i):"PAGE_FAV_ANOTHER"==e&&d.append(i),r[t].append(d)}}function FN_wzddl_createUI_tipsBox(){const e=document.createElement("div");e.classList.add("wzddl-control-tipsBox");const t=document.createElement("span"),n=document.createElement("span");t.classList.add("icon-tips","icon","icon-yuanxingxuanzhong"),n.classList.add("tips"),e.append(t,n);const o=document.querySelector(".works-nav");o?o.append(e):document.body.append(e),e.addEventListener("animationend",(function(){window.setTimeout((()=>{this.style.display="none",this.classList.remove("wzddl-ani-tipsShow")}),1e3)}),!1),window.wzddl_ele_msgBox=e,window.wzddl_ele_msgBox_text=n}function FN_WZDDL_event_moveOrCopyFav(e,t){const n=t.target.wzddl_assetID,o=window.wzddl_userFav_userSelected_currentPage,d=window.wzddl_dirID_currentPage;GM_xmlhttpRequest({method:"post",url:`${URL_POST_USERFAV}`,headers:{"Content-Type":"application/x-www-form-urlencoded"},data:`action=addFav&picid=${n}&page_id=${o}&version=3`,onload:t=>{FN_WZDDL_msgShow(JSON.parse(t.response).info),"move"==e.controlType&&GM_xmlhttpRequest({method:"post",url:`${URL_POST_USERFAV}`,headers:{"Content-Type":"application/x-www-form-urlencoded"},data:`action=rFav&picid=${n}&page_id=${d}&version=3`,onload:t=>{FN_WZDDL_msgShow(JSON.parse(t.response).info),e.currentFavBox.remove()}})}})}function FN_WZDDL_event_favPage_selected(e){const t=e.target.parentElement.parentElement,n=t&&t.getAttribute("data-userFav-id");n&&(GM_setValue("wzddl_userFav_lastSelected",{id:n}),window.wzddl_userFav_userSelected_currentPage=n)}function FN_WZDDL_refreshFavDirData(){let e=[];FN_WZDDL_getUserFavDir().then((t=>{const n=JSON.parse(t.response);if(1!=n.status)throw new Error("错误E1: 千图收藏夹目录API异常");e=[...e,...n.data.favor_pages];const o=n.data.totalpage;if(o>1){let t=1;return new Promise((function(n,d){for(let d=2;d<=o;d++)FN_WZDDL_getUserFavDir(d).then((d=>{const a=JSON.parse(d.response);if(1!=a.status)throw new Error("错误E1: 千图收藏夹目录API异常2");e=[...e,...a.data.favor_pages],t++,t==o&&n(e)})).catch(FN_WZDDL_errorHandle)}))}return e})).then((e=>{let t=[];e.forEach((e=>{t.push({id:e.id,name:e.name})})),GM_deleteValue("wzddl_userFav_dir"),GM_setValue("wzddl_userFav_dir",t)})).catch(FN_WZDDL_errorHandle)}function FN_WZDDL_getUserFavDir(e=1){return new Promise((function(t,n){GM_xmlhttpRequest({method:"post",url:`${URL_GET_USERFAV_LIST}`,headers:{"Content-Type":"application/x-www-form-urlencoded"},data:`type=&page=${e}`,onload:e=>{t(e)}})}))}function FN_WZDDL_matchPageType(){const e=window.location.href;let t=/.+?:\/\/.+.58pic.com\/u\/\d+\/c\/create/,n=/(?:.+?:\/\/.+.58pic.com\/c\/\d+)|(?:.+?:\/\/.+.58pic.com\/collect\/\d+)/;t.lastIndex=0,n.lastIndex=0;const o=t.test(e),d=n.test(e);let a=!1;if(d){const e=document.querySelector("div.favorites-banner-container div.favorites-info-box div.user-face > div > button"),t=document.querySelector('input[type="hidden"]#uid');e&&e.getAttribute("data-uid")==t.value&&(a=!0)}return o?"PAGE_DIR":d&&a?"PAGE_FAV":d&&!a?"PAGE_FAV_ANOTHER":void 0}function FN_WZDDL_hotKeyBind_pageChange(){let e=document.querySelectorAll(".works-list-row .page-box a");if(e)for(let t=0;t<e.length;t++){const n=e[t].textContent.trim();"上一页"!=n&&"下一页"!=n||FN_WZDDL_keyPrevAndNext(n,e[t])}}function FN_WZDDL_keyPrevAndNext(e,t){switch(e){case"上一页":document.addEventListener("keyup",(function(e){"ArrowLeft"==e.code&&(window.location.href=t.href)}),!1);break;case"下一页":document.addEventListener("keyup",(function(e){"ArrowRight"==e.code&&(window.location.href=t.href)}),!1);break}}!function(){switch(GM_registerMenuCommand("更新收藏目录数据",(()=>{FN_WZDDL_refreshFavDirData()})),FN_WZDDL_matchPageType()){case"PAGE_DIR":FN_WZDDL_refreshFavDirData();break;case"PAGE_FAV":FN_WZDDL_addStyle(),FN_WZDDL_createUI_navBar(),FN_wzddl_createUI_favBox("PAGE_FAV"),FN_wzddl_createUI_tipsBox();break;case"PAGE_FAV_ANOTHER":FN_WZDDL_addStyle(),FN_WZDDL_createUI_navBar(),FN_wzddl_createUI_favBox("PAGE_FAV_ANOTHER"),FN_wzddl_createUI_tipsBox();break}FN_WZDDL_hotKeyBind_pageChange()}();