FA Embedded Image Viewer

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

当前为 2024-02-16 提交的版本,查看 最新版本

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