Embedded Image Viewer

Embedds the clicked Image on the Current Site, so you can view it without loading the submission Page

当前为 2024-01-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Embedded Image Viewer
  3. // @namespace Violentmonkey Scripts
  4. // @match *://*.furaffinity.net/*
  5. // @require https://greasyfork.org/scripts/475041-furaffinity-custom-settings/code/Furaffinity-Custom-Settings.js
  6. // @require https://greasyfork.org/scripts/483952-furaffinity-request-helper/code/Furaffinity-Request-Helper.js
  7. // @require https://greasyfork.org/scripts/485153-furaffinity-loading-animations/code/Furaffinity-Loading-Animations.js
  8. // @grant none
  9. // @version 2.0.1
  10. // @author Midori Dragon
  11. // @description Embedds the clicked Image on the Current Site, so you can view it without loading the submission Page
  12. // @icon https://www.furaffinity.net/themes/beta/img/banners/fa_logo.png?v2
  13. // @homepageURL https://greasyfork.org/de/scripts/458971-embedded-image-viewer
  14. // @supportURL https://greasyfork.org/de/scripts/458971-embedded-image-viewer/feedback
  15. // @license MIT
  16. // ==/UserScript==
  17.  
  18. // jshint esversion: 8
  19.  
  20. const matchList = ['net/browse', 'net/gallery', 'net/search', 'net/favorites', 'net/scraps', 'net/controls/favorites', 'net/controls/submissions', 'net/msg/submissions', 'd.furaffinity.net'];
  21.  
  22. const isDFuraffinity = window.location.toString().includes("d.furaffinity.net");
  23. const isDownloadImage = window.location.toString().includes("?eidownload");
  24.  
  25. if (isDFuraffinity) {
  26. if (isDownloadImage)
  27. downloadImage();
  28. return;
  29. }
  30.  
  31. CustomSettings.name = "Extension Settings";
  32. CustomSettings.provider = "Midori's Script Settings";
  33. CustomSettings.headerName = `${GM_info.script.name} Settings`;
  34. const preventInstantDownloadSetting = CustomSettings.newSetting("Prevent Instant Download", "Sets wether to instantly download the Image when the download Button is pressed.", SettingTypes.Boolean, "Prevent Instant Download", false);
  35. const loadingSpinSpeedSetting = CustomSettings.newSetting("Fav Loading Animation", "Sets the spinning speed of the loading animation in milliseconds.", SettingTypes.Number, "", 100);
  36. CustomSettings.loadSettings();
  37.  
  38. let color = "color: blue";
  39. if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches)
  40. color = "color: aqua";
  41. if (window.location.toString().includes("?extension")) {
  42. console.info(`%cSettings: ${GM_info.script.name} v${GM_info.script.version}`, color);
  43. return;
  44. }
  45.  
  46. if (!matchList.some(x => window.location.toString().includes(x)))
  47. return;
  48.  
  49. console.info(`%cRunning: ${GM_info.script.name} v${GM_info.script.version} ${CustomSettings.toString()}`, color);
  50.  
  51. const requestHelper = new FARequestHelper(2);
  52.  
  53. class EmbeddedImage {
  54. constructor(figure) {
  55. this.embeddedElem;
  56. this.backgroundElem;
  57. this.submissionContainer;
  58. this.submissionImg;
  59. this.buttonsContainer;
  60. this.favButton;
  61. this.downloadButton;
  62. this.closeButton;
  63.  
  64. this._onRemoveAction;
  65.  
  66. this.createStyle();
  67. this.createElements();
  68.  
  69. this.loadingSpinner = new LoadingSpinner(this.submissionContainer);
  70. this.loadingSpinner.spinnerThickness = 6;
  71. this.loadingSpinner.visible = true;
  72. this.fillSubDocInfos(figure);
  73. }
  74.  
  75. createStyle() {
  76. if (document.getElementById("embeddedStyle")) return;
  77. const style = document.createElement("style");
  78. style.id = "embeddedStyle";
  79. style.type = "text/css";
  80. style.innerHTML = `
  81. #embeddedElem {
  82. position: fixed;
  83. width: 100vw;
  84. height: 100vh;
  85. max-width: 1850px;
  86. z-index: 999999;
  87. background: rgba(30,33,38,.65);
  88. }
  89. #embeddedBackgroundElem {
  90. position: fixed;
  91. display: flex;
  92. flex-direction: column;
  93. left: 50%;
  94. transform: translate(-50%, 0%);
  95. margin-top: 20px;
  96. padding: 20px;
  97. background: rgba(30,33,38,.90);
  98. border-radius: 10px;
  99. }
  100. #embeddedSubmissionImg {
  101. max-width: inherit;
  102. max-height: inherit;
  103. border-radius: 10px;
  104. }
  105. #embeddedButtonsContainer {
  106. margin-top: 20px;
  107. margin-bottom: 20px;
  108. margin-left: 20px;
  109. }
  110. .embeddedButton {
  111. margin-left: 4px;
  112. margin-right: 4px;
  113. }
  114. `;
  115. document.head.appendChild(style);
  116. }
  117.  
  118. onRemove(action) {
  119. this._onRemoveAction = action;
  120. }
  121.  
  122. remove() {
  123. this.embeddedElem.parentNode.removeChild(this.embeddedElem);
  124. if (this._onRemoveAction)
  125. this._onRemoveAction();
  126. }
  127.  
  128. createElements() {
  129. this.embeddedElem = document.createElement("div");
  130. this.embeddedElem.id = "embeddedElem";
  131. this.embeddedElem.onclick = (event) => {
  132. if (event.target == this.embeddedElem)
  133. this.remove();
  134. }
  135.  
  136. this.backgroundElem = document.createElement("div");
  137. this.backgroundElem.id = "embeddedBackgroundElem";
  138. notClosingElemsArr.push(this.backgroundElem.id);;
  139.  
  140. this.submissionContainer = document.createElement("a");
  141. this.submissionContainer.id = "embeddedSubmissionContainer";
  142. notClosingElemsArr.push(this.submissionContainer.id);
  143.  
  144. this.backgroundElem.appendChild(this.submissionContainer);
  145.  
  146. this.buttonsContainer = document.createElement("div");
  147. this.buttonsContainer.id = "embeddedButtonsContainer";
  148. notClosingElemsArr.push(this.buttonsContainer.id);
  149.  
  150. this.favButton = document.createElement("a");
  151. this.favButton.id = "embeddedFavButton";
  152. notClosingElemsArr.push(this.favButton.id);
  153. this.favButton.type = "button";
  154. this.favButton.className = "embeddedButton button standard mobile-fix";
  155. this.favButton.textContent = "-";
  156. this.buttonsContainer.appendChild(this.favButton);
  157.  
  158. this.downloadButton = document.createElement("a");
  159. this.downloadButton.id = "embeddedDownloadButton";
  160. notClosingElemsArr.push(this.downloadButton.id);
  161. this.downloadButton.type = "button";
  162. this.downloadButton.className = "embeddedButton button standard mobile-fix";
  163. this.downloadButton.textContent = "Download";
  164. this.downloadButton.target = "_blank";
  165. this.buttonsContainer.appendChild(this.downloadButton);
  166.  
  167. this.closeButton = document.createElement("a");
  168. this.closeButton.id = "embeddedCloseButton";
  169. notClosingElemsArr.push(this.closeButton.id);
  170. this.closeButton.type = "button";
  171. this.closeButton.className = "embeddedButton button standard mobile-fix";
  172. this.closeButton.textContent = "Close";
  173. this.closeButton.onclick = () => this.remove();
  174. this.buttonsContainer.appendChild(this.closeButton);
  175.  
  176. this.backgroundElem.appendChild(this.buttonsContainer);
  177.  
  178. this.embeddedElem.appendChild(this.backgroundElem);
  179.  
  180. const ddmenu = document.getElementById("ddmenu");
  181. ddmenu.appendChild(this.embeddedElem);
  182. }
  183.  
  184. async fillSubDocInfos(figure) {
  185. const sid = figure.id.split("-")[1];
  186. const ddmenu = document.getElementById("ddmenu");
  187. const doc = await requestHelper.SubmissionRequests.getSubmissionPage(sid);
  188. if (this.loadingSpinner)
  189. this.loadingSpinner.visible = false;
  190. if (doc) {
  191. this.submissionImg = doc.getElementById("submissionImg");
  192. this.submissionImg.style.maxWidth = window.innerWidth - 20 * 2 + "px";
  193. this.submissionImg.style.maxHeight = window.innerHeight - ddmenu.clientHeight - 38 * 2 - 20 * 2 - 100 + "px";
  194. this.submissionContainer.appendChild(this.submissionImg);
  195. this.submissionContainer.href = doc.querySelector('meta[property="og:url"]').content;
  196.  
  197. const result = getFavKey(doc);
  198. this.favButton.textContent = result.isFav ? "+Fav" : "-Fav";
  199. this.favButton.setAttribute("isFav", result.isFav);
  200. this.favButton.setAttribute("key", result.favKey);
  201. this.favButton.onclick = () => this.doFavRequest(sid);
  202.  
  203. this.downloadButton.href = this.submissionImg.src + "?eidownload";
  204. }
  205. }
  206.  
  207. async doFavRequest(sid) {
  208. const loadingTextSpinner = new LoadingTextSpinner(this.favButton);
  209. loadingTextSpinner.visible = true;
  210. let favKey = this.favButton.getAttribute("key");
  211. let isFav = this.favButton.getAttribute("isFav");
  212. if (isFav == "true") {
  213. favKey = await requestHelper.SubmissionRequests.favSubmission(sid, favKey);
  214. loadingTextSpinner.visible = false;
  215. if (favKey) {
  216. this.favButton.setAttribute("key", favKey);
  217. isFav = false;
  218. this.favButton.setAttribute("isFav", isFav);
  219. this.favButton.textContent = "-Fav";
  220. } else {
  221. this.favButton.textContent = "x";
  222. setTimeout(() => this.favButton.textContent = "+Fav", 1000);
  223. }
  224. } else {
  225. favKey = await requestHelper.SubmissionRequests.unfavSubmission(sid, favKey);
  226. loadingTextSpinner.visible = false;
  227. if (favKey) {
  228. this.favButton.setAttribute("key", favKey);
  229. isFav = true;
  230. this.favButton.setAttribute("isFav", isFav);
  231. this.favButton.textContent = "+Fav";
  232. } else {
  233. this.favButton.textContent = "x";
  234. setTimeout(() => this.favButton.textContent = "-Fav", 1000);
  235. }
  236. }
  237. }
  238. }
  239.  
  240. function getFavKey(doc) {
  241. const columnPage = doc.getElementById("columnpage");
  242. const navbar = columnPage.querySelector('div[class*="favorite-nav"');
  243. const buttons = navbar.querySelectorAll('a[class*="button"][href]');
  244. let favButton;
  245. for (const button of buttons) {
  246. if (button.textContent.toLowerCase().includes("fav"))
  247. favButton = button;
  248. }
  249.  
  250. if (favButton) {
  251. const favKey = favButton.href.split("?key=")[1];
  252. const isFav = !favButton.href.toLowerCase().includes("unfav");
  253. return { favKey, isFav };
  254. }
  255. }
  256.  
  257. let isShowing = false;
  258. let notClosingElemsArr = [];
  259. let embeddedImage;
  260.  
  261. addEmbedded();
  262. window.updateEmbedded = addEmbedded;
  263.  
  264. async function addEmbedded() {
  265. for (const figure of document.querySelectorAll('figure:not([embedded])')) {
  266. figure.setAttribute('embedded', true);
  267. figure.addEventListener("click", function (event) {
  268. if (!event.ctrlKey && !event.target.id.includes("favbutton") && event.target.type != "checkbox") {
  269. if (event.target.href)
  270. return;
  271. else
  272. event.preventDefault();
  273. if (!isShowing)
  274. showImage(figure);
  275. }
  276. });
  277. }
  278. }
  279.  
  280. async function showImage(figure) {
  281. isShowing = true;
  282. embeddedImage = new EmbeddedImage(figure);
  283. embeddedImage.onRemove(() => {
  284. embeddedImage = null;
  285. isShowing = false;
  286. });
  287. }
  288.  
  289. function downloadImage() {
  290. console.log("Embedded Image Viewer downloading Image...");
  291. let url = window.location.toString();
  292. if (url.includes("?")) {
  293. const parts = url.split('?');
  294. url = parts[0];
  295. }
  296. const download = document.createElement('a');
  297. download.href = url;
  298. download.download = url.substring(url.lastIndexOf("/") + 1);
  299. download.style.display = 'none';
  300. document.body.appendChild(download);
  301. download.click();
  302. document.body.removeChild(download);
  303.  
  304. window.close();
  305. }