Last active
November 17, 2023 22:36
-
-
Save Justin-Byrne/5aa9b9f2d2d3567632979e48126900b1 to your computer and use it in GitHub Desktop.
Additional functionality for Deviant Art
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* DeviantIO - Additional functionality for Deviant Art | |
* Copyright (C) 2023 Justin D. Byrne | |
* | |
* This library is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU Library General Public | |
* License as published by the Free Software Foundation; either | |
* version 2 of the License, or (at your option) any later version. | |
* | |
* This library is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* Library General Public License for more details. | |
* | |
* You should have received a copy of the GNU Library General Public | |
* License along with this library; if not, write to the | |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, | |
* Boston, MA 02110-1301, USA. | |
* | |
* Byrne-Systems, hereby disclaims all copyright interest in | |
* the library 'DeviantIO' (Additional functionality for Deviant Art) written | |
* by Justin D. Byrne. (justin@byrne-systems.com) | |
*/ | |
"use strict";(window=>{let u={input_hotkeys:{previous:["a","left"],next:["d","right"],home:["w","up"],favourite:["q","space","enter"],watch:["e","shift"],expand:["f","alt"],user:["c","/","command"],redirect:["s","down"]},feature_flags:{favourite_to_next:!0,show_notification_favourite:!0,show_notification_favourite_stage:!0,show_notification_watch:!0,show_status_favourite:!0,skip_favourite:!1,skip_watch:!1,skip_tier:!0},redirect_urls:["https://google.com","https://gmail.com","https://github.com","https://trello.com"],internal_data:{accent_colour:"rgba(118, 228, 177, 1)",time_interval:500,url_reference:void 0,deviantarturl:"https://www.deviantart.com",mousetrap_cdn:"//cdnjs.cloudflare.com/ajax/libs/mousetrap/1.4.6/mousetrap.min.js",ui_data_hooks:{watch:"[data-hook='user_watch_button']",favourite:"[data-hook='fave_button']",stage:"[data-hook='art_stage']",thumbs:"[data-hook='deviation_std_thumb']",user:"[data-hook='user_link']",action:"[data-hook='action_bar']",meta:"[data-hook='deviation_meta']"},picture_rules:{off:{border:"none",boxShadow:"none",opacity:1},on:{border:"2px solid %ACCENT_COLOUR%",boxShadow:"0px 0px 5px 2px %ACCENT_COLOUR%",opacity:.5}}}},r={site:{home:u.internal_data.deviantarturl+"/\\B",search:u.internal_data.deviantarturl+"/search?",watch:u.internal_data.deviantarturl+"/watch/deviations",daily:u.internal_data.deviantarturl+"/daily-deviations",popular:u.internal_data.deviantarturl+"/popular/deviations",topic:u.internal_data.deviantarturl+"/topic/\\w+",tag:u.internal_data.deviantarturl+"/tag/\\w+",art:u.internal_data.deviantarturl+"/[^/]+/art/.+",tier:u.internal_data.deviantarturl+"/[^/]+/tier"},user:{favourites:u.internal_data.deviantarturl+"/(?!watch|daily|tag|popular)\\w+/favourites",gallery:u.internal_data.deviantarturl+"/(?!watch|daily|tag|popular)\\w+/gallery",home:u.internal_data.deviantarturl+"/(?!watch|daily|tag|popular)\\w+"}};var e={position:"absolute","z-index":999,"font-size":"4em"};let s={notifications:{watch:{css:{...e,animation:"wiggle 2s linear infinite",right:"1em"},animation:"@keyframes wiggle{0%, 7% { transform: rotateZ(0); }15% { transform: rotateZ(-15deg); }20% { transform: rotateZ(10deg); }25% { transform: rotateZ(-10deg); }30% { transform: rotateZ(6deg); }35% { transform: rotateZ(-4deg); }40%, 100% { transform: rotateZ(0); }}"},favourite:{css:{...e,animation:"bounce 1s ease-out infinite",left:"1em"},animation:"@keyframes bounce{0% { transform: scale(0.75); }100% { transform: scale(1); }}"}},status:{favourite:{css:{...e}}},icons:{favourite:"❤️",watch:"🔍"}},o={previous:()=>{Mousetrap.bind("%HOT_KEYS%",function(e){"art"==deviantIO.getPageType().type&&(deviantIO.clearNotification("favourite"),deviantIO.clearNotification("watch"),document.querySelectorAll("[data-hook='arrowL']")[0].children[0].click())})},next:()=>{Mousetrap.bind("%HOT_KEYS%",function(e){"art"==deviantIO.getPageType().type&&(deviantIO.clearNotification("favourite"),deviantIO.clearNotification("watch"),document.querySelectorAll("[data-hook='arrowR']")[0].children[0].click())})},home:()=>{Mousetrap.bind("%HOT_KEYS%",function(e){if(e.preventDefault(),"art"==deviantIO.getPageType().type){var t;let e=null;for(t of["Home","Back","Profile","Search"])try{e=document.evaluate(`//span[text()='${t}']`,document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue.parentElement}catch(e){}null!=e&&e.click()}})},favourite:()=>{Mousetrap.bind("%HOT_KEYS%",function(e){e.preventDefault(),"art"==deviantIO.getPageType().type&&(deviantIO.clearNotification("favourite"),document.querySelectorAll("[data-hook='fave_button']")[0].click(),deviantIO.config.feature_flags.favourite_to_next)&&Mousetrap.trigger("%NEXT%")})},watch:()=>{Mousetrap.bind("%HOT_KEYS%",function(e){e.preventDefault(),"art"==deviantIO.getPageType().type&&(deviantIO.clearNotification("watch"),document.querySelectorAll("[data-hook='user_watch_button']")[0].click())})},expand:()=>{Mousetrap.bind("%HOT_KEYS%",function(e){"art"==deviantIO.getPageType().type&&deviantIO.setExpand()})},user:()=>{Mousetrap.bind("%HOT_KEYS%",function(e){e.preventDefault(),"art"==deviantIO.getPageType().type&&(e=document.querySelectorAll("[data-hook='deviation_meta']")[0].querySelectorAll("[data-hook='user_link']")[0].href,window.open(e+"/gallery","_blank"))})},redirect:()=>{Mousetrap.bind("%HOT_KEYS%",function(e){e.preventDefault(),entry=Math.floor(Math.random()*deviantIO.config.redirect_urls.length),window.open(deviantIO.config.redirect_urls[entry],"_blank")})}};function i(e,t){var a,n;a=t,(n=document.createElement("style")).type="text/css",n.innerHTML=a.animation,document.getElementsByTagName("head")[0].appendChild(n),a=e,n=t,(e=document.createElement("span")).id=a+"_button",e.innerHTML="favourite"==a?s.icons.favourite:s.icons.watch,e.style.cssText=f(n.css),document.querySelectorAll(u.internal_data.ui_data_hooks.stage)[0].appendChild(e)}function a(){window.addEventListener("load",()=>{var e="text/javascript",t=document.createElement("script"),t=(t.type=e,t.text=function(){let e="",n=0;function t(e,t){var a=function(e){let t="";if(1==e.length)t=`'${e[0]}'`;else for(var a in e)t+=`'${e[a]}',`;return t=`[ ${t.replace(/,+$/,"")} ]`}(u.input_hotkeys[e]);return t="favourite"===e?t.replace(/\([^\{]+{/,"").replace(/"%HOT_KEYS%"/,a).replace(/"%NEXT%"/,`'${u.input_hotkeys.next[0]}'`):t.replace(/\([^\{]+{/,"").replace(/"%HOT_KEYS%"/,a),n?t=t.slice(9):n++,t.substring(0,t.length-1)}for(var a in o){var r=o[a].toString();r=t(a,r),e+=r}return e}(),t.onload=()=>console.info(">> Mousetrap script has loaded !"),t.onerror=()=>console.warn(">> Mousetrap script has errored !"),document.body.appendChild(t),document.createElement("script"));t.type=e,t.text=setInterval(h,u.internal_data.time_interval),t.onload=()=>console.info(">> DeviantIO script has loaded !"),t.onerror=()=>console.warn(">> DeviantIO script has errored !"),document.body.appendChild(t)})}function t(){var e,t;a();for(e of document.querySelectorAll(u.internal_data.ui_data_hooks.favourite))e.addEventListener("click",()=>{deviantIO.checkNotification("favourite")});for(t of document.querySelectorAll(u.internal_data.ui_data_hooks.watch))t.addEventListener("click",()=>{deviantIO.checkNotification("watch")})}function n(e){var t,a,n,r,o=function(e){let t=void 0;try{t=e.children[1].children[2].children[2].children[1].children[1].children[0].children[0].children[0].outerHTML}catch(e){}return null!=t?t:console.warn("Could not get portrait thumb's path !")}(e),i=u.internal_data.picture_rules;if(/fill-rule=\"evenodd\"/.test(o))for(var l in i.off)e.style[l]=i.off[l];else for(var c in o=(o=e).children[0].children[0],n=s.status.favourite,(r=document.createElement("div")).className="io_favourite",r.innerHTML=s.icons.favourite,r.style.cssText=f(n.css),1==o.children.length&&o.appendChild(r),i.on){var d=i.on[c];a="%ACCENT_COLOUR%",d="string"==typeof(t=d)&&t.includes(a)?t.replace(a,u.internal_data[a.toLowerCase().replace(/%/g,"")]):t,e.style[c]=d}}function l(){var e,t=window.location.href,a=r;for(e in a)for(var n in a[e])if(new RegExp(a[e][n]).test(t))return{category:e,type:n}}function c(e){var t=document.querySelectorAll(u.internal_data.ui_data_hooks[e])[0];switch(e){case"watch":return null!=t&&"Watch"===t.innerText;case"favourite":return"In Favourites"===t.innerText}}function f(e){let t="";for(var[a,n]of Object.entries(e))n="number"==typeof n&&"z-index"!=a?n+"px":n,t+=`${a}: ${n}; `;return t}function d(e){return e.charAt(0).toUpperCase()+e.slice(1)}function p(e){var t=document.getElementById(e+"_button");"favourite"===e&&(document.querySelectorAll(u.internal_data.ui_data_hooks.stage)[0].style.backgroundColor=""),null!=t&&t.remove()}function _(e){r=e,u.feature_flags["skip_"+r]&&c(r)&&(["favourite","watch"].forEach(e=>p(e)),Mousetrap.trigger(""+u.input_hotkeys.next[0]));var t,a,n,r="flagged"+d(e)+"Status";"yes"!=(localStorage.getItem(r)||"")&&(c(e)?(r=e,t=document.getElementById(r+"_button"),a=s.notifications[r],n="flagged"+d(r)+"Status","favourite"===r&&u.feature_flags.show_notification_favourite_stage&&(document.querySelectorAll(u.internal_data.ui_data_hooks.stage)[0].style.backgroundColor=u.internal_data.accent_colour),null==t&&u.feature_flags["show_notification_"+r]&&i(r,a),localStorage.setItem(n,"yes")):p(e))}function h(){var e;switch(window.location.href!=u.internal_data.url_reference&&localStorage.clear(),l().type){case"art":_("favourite"),_("watch");break;case"home":case"search":case"watch":case"daily":case"popular":case"topic":case"tag":case"favourites":case"gallery":if(u.feature_flags.show_status_favourite){var t=0<(e=document.querySelectorAll(u.internal_data.ui_data_hooks.thumbs)).length?e:console.warn("Portrait thumbs could not be cached !");for(let e=0;e<t.length;e++)n(t[e])}break;case"tier":u.feature_flags.skip_tier&&(e=u.input_hotkeys.next[0],Mousetrap.trigger(""+e));break;default:console.warn(`Nothing to do for present url: ${u.internal_data.url_reference}.`)}}function m(){var e={config:u,clearNotification:e=>p(e),checkNotification:e=>_(e)};return e.setExpand=()=>{var e,t=document.querySelectorAll(deviantIO.config.internal_data.ui_data_hooks.stage)[0],a=t.parentElement,n=(a.style.width=""==a.style.width?"100%":"",{header:{display:"flex",element:document.getElementsByTagName("header")[0]},contributing:{display:"block",element:a.nextElementSibling},support:{display:"block",element:t.nextElementSibling},action:{display:"flex",element:document.querySelectorAll(deviantIO.config.internal_data.ui_data_hooks.action)[0]},meta:{display:"flex",element:document.querySelectorAll(deviantIO.config.internal_data.ui_data_hooks.meta)[0].parentElement.parentElement},comments:{display:"flex",element:document.querySelectorAll(deviantIO.config.internal_data.ui_data_hooks.meta)[0].parentElement.parentElement.nextElementSibling},footer:{display:"flex",element:document.getElementsByTagName("footer")[0]}});for(e in n){var r=n[e],o=window.getComputedStyle(r.element);r.element.style.display=o.display==r.display?"none":r.display}},e.getPageType=()=>l(),e}function v(){var e;window.deviantIO=m(),(e=document.createElement("script")).setAttribute("src",u.internal_data.mousetrap_cdn),document.head.appendChild(e),t(),(e=document.createElement("style")).innerText=".io_favourite{animation: bounce 1s ease-out infinite;} @keyframes bounce{0% { transform: scale(0.75); }100% { transform: scale(1); }}",document.head.appendChild(e)}void 0===window.deviantIO&&v()})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment