Advertisement
Guest User

Untitled

a guest
Mar 29th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Kemono Party No Login Favorites
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @author       You
  6. // @match        https://kemono.party/*
  7. // @grant        none
  8. // ==/UserScript==
  9.  
  10. document.querySelector("ul.header").innerHTML = "<li><a href='/favorites'>Favorites</a></li>"+document.querySelector("ul.header").innerHTML;
  11.  
  12. function map_to_array(mapobject){
  13.     var output = [];
  14.     mapobject.forEach((v,k) => {
  15.         output.push([k,v]);
  16.     });
  17.     return output;
  18. }
  19.  
  20. if(window.localStorage.userscript_favorites !== undefined && window.localStorage.userscript_favorites.length > 0){
  21.     window.extension_favorites = new Map(JSON.parse(window.localStorage.userscript_favorites));
  22. }else{
  23.     if(window.localStorage.favorites !== undefined && window.localStorage.favorites.length > 0){
  24.         // convert existing old no-login favorites to extension format
  25.         console.log("importing old favorites...");
  26.         var old_favs = window.localStorage.favorites.split(",");
  27.         for(let i = 0; i < old_favs.length; i++){
  28.             let sp = old_favs[i].split(":");
  29.             old_favs[i] = [
  30.                 "/"+sp[0]+"/user/"+sp[1],
  31.                 {
  32.                     display_name:"UNKNOWN (visit artist page to update)",
  33.                     id:sp[1],
  34.                     platform:sp[0]
  35.                 }
  36.             ];
  37.         }
  38.         window.extension_favorites = new Map(old_favs);
  39.         window.localStorage.userscript_favorites = JSON.stringify(map_to_array(window.extension_favorites));
  40.         alert("existing favorites detected, importing to extension. extension will update artist names after you visit their page once.");
  41.         console.log("favorites saved");
  42.     }else{
  43.         window.extension_favorites = new Map();
  44.     }
  45. }
  46.  
  47. window.add_extension_favorite = function(){
  48.     document.getElementById("extension_favorite_button").innerText = "★ Favorited!";
  49.     document.getElementById("extension_favorite_button").href = "javascript:window.remove_extension_favorite();";
  50.     let sp = window.location.pathname.split("/user/");
  51.     window.extension_favorites.set(window.location.pathname,{
  52.         display_name:document.querySelector("#info-block a[href='"+window.location.pathname+"']").innerText,
  53.         id:sp[1],
  54.         platform:sp[0].replace("/","")
  55.     });
  56.     console.log("added favorite",window.location.pathname);
  57.     window.save_extension_favorites();
  58. }
  59.  
  60. window.remove_extension_favorite = function(){
  61.     document.getElementById("extension_favorite_button").innerText = "☆ Favorite";
  62.     document.getElementById("extension_favorite_button").href = "javascript:window.add_extension_favorite();";
  63.     window.extension_favorites.delete(window.location.pathname);
  64.     console.log("removed favorite",window.location.pathname);
  65.     window.save_extension_favorites();
  66. }
  67.  
  68. window.save_extension_favorites = function(){
  69.     window.localStorage.userscript_favorites = JSON.stringify(map_to_array(window.extension_favorites));
  70.     console.log("favorites saved");
  71. }
  72.  
  73. if(window.location.href.indexOf("/user/") > -1 && document.querySelector("#info-block a[href^='javascript:favorite_artist']") !== null){
  74.     // this replaces favorite button on artist pages with extension functionality
  75.     document.querySelector("#info-block a[href^='javascript:favorite_artist']").id = "extension_favorite_button";
  76.     document.getElementById("extension_favorite_button").href = "javascript:window.remove_extension_favorite();";
  77.     if(window.extension_favorites.has(window.location.pathname)){
  78.         document.getElementById("extension_favorite_button").innerText = "★ Favorited!";
  79.         // this records artist names if it was an imported unknown artist
  80.         if(window.extension_favorites.get(window.location.pathname).display_name == "UNKNOWN (visit artist page to update)"){
  81.             let sp = window.location.pathname.split("/user/");
  82.             window.extension_favorites.set(window.location.pathname,{
  83.                 display_name:document.querySelector("#info-block a[href='"+window.location.pathname+"']").innerText,
  84.                 id:sp[1],
  85.                 platform:sp[0].replace("/","")
  86.             });
  87.             console.log("unknown artist name updated");
  88.             window.save_extension_favorites();
  89.         }
  90.     }
  91. }else if(window.location.href == "https://kemono.party/account/login"){
  92.     document.querySelector("div.views").setAttribute("class","vertical-views");
  93.     // when not logged in, clicking favorites redirects to login page.
  94.     // this makes the login page show favorites, with login form still at bottom. if you're logged in, you'll see logged-in favorites, not extension favorites. artist favorite buttons won't work for account favorites though, disable the extension if you arent using it lol
  95.     var build_html = `<table class="search-results" width="100%"><thead><tr><th width="50px">Icon</th><th>Name</th><th>Service</th></tr></thead><tbody>`;
  96.     window.extension_favorites.forEach((fav,favurl) => {
  97.         build_html += `<tr class="artist-row"><td><a href="`+favurl+`"><div class="user-icon" style="background-image: url('/icons/`+fav.platform+`/`+fav.id+`');"></div></a></td><td><a href="`+favurl+`">`+fav.display_name+`</a></td><td>`+fav.platform+`</td>`;
  98.     });
  99.     build_html += `</tbody></table><br>`;
  100.     document.querySelector(".login-container").outerHTML = build_html + document.querySelector(".login-container").outerHTML;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement