FA Embedded Image Viewer

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

当前为 2024-10-11 提交的版本,查看 最新版本

  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/1329447/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. // @require https://update.greasyfork.org/scripts/492931/1363921/Furaffinity-Submission-Image-Viewer.js
  11. // @grant none
  12. // @version 2.1.5
  13. // @author Midori Dragon
  14. // @description Embedds the clicked Image on the Current Site, so you can view it without loading the submission Page
  15. // @icon https://www.furaffinity.net/themes/beta/img/banners/fa_logo.png?v2
  16. // @homepageURL https://greasyfork.org/de/scripts/458971-embedded-image-viewer
  17. // @supportURL https://greasyfork.org/de/scripts/458971-embedded-image-viewer/feedback
  18. // @license MIT
  19. // ==/UserScript==
  20.  
  21. // jshint esversion: 8
  22.  
  23. CustomSettings.name = "Extension Settings";
  24. CustomSettings.provider = "Midori's Script Settings";
  25. CustomSettings.headerName = `${GM_info.script.name} Settings`;
  26. 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);
  27. 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);
  28. 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);
  29. const closeEmbedAfterOpenSetting = CustomSettings.newSetting("Close Embed after open", "Closes the current embedded Submission after it is opened in a new Tab (also for open Gallery)", SettingTypes.Boolean, "Close Embed after open", true);
  30. CustomSettings.loadSettings();
  31.  
  32. const matchList = new MatchList(CustomSettings);
  33. matchList.matches = ['net/browse', 'net/user', 'net/gallery', 'net/search', 'net/favorites', 'net/scraps', 'net/controls/favorites', 'net/controls/submissions', 'net/msg/submissions', 'd.furaffinity.net'];
  34. matchList.runInIFrame = true;
  35. if (!matchList.hasMatch())
  36. return;
  37.  
  38. const page = new CustomPage("d.furaffinity.net", "eidownload");
  39. page.onopen = (data) => {
  40. downloadImage();
  41. return;
  42. };
  43.  
  44. if (matchList.isWindowIFrame() == true)
  45. return;
  46.  
  47. const requestHelper = new FARequestHelper(2);
  48.  
  49. class EmbeddedImage {
  50. constructor(figure) {
  51. this._previewLoaded;
  52. this._imageLoaded;
  53.  
  54. this.embeddedElem;
  55. this.backgroundElem;
  56. this.submissionContainer;
  57. this.submissionImg;
  58. this.buttonsContainer;
  59. this.previewLoadingSpinnerContainer;
  60. this.favButton;
  61. this.downloadButton;
  62. this.closeButton;
  63.  
  64. this.favRequestRunning = false;
  65. this.downloadRequestRunning = false;
  66.  
  67. this._onRemoveAction;
  68.  
  69. this.createStyle();
  70. this.createElements(figure);
  71.  
  72. this.loadingSpinner = new LoadingSpinner(this.submissionContainer);
  73. this.loadingSpinner.delay = loadingSpinSpeedSetting.value;
  74. this.loadingSpinner.spinnerThickness = 6;
  75. this.loadingSpinner.visible = true;
  76.  
  77. this.previewLoadingSpinner = new LoadingSpinner(this.previewLoadingSpinnerContainer);
  78. this.previewLoadingSpinner.delay = loadingSpinSpeedSetting.value;
  79. this.previewLoadingSpinner.spinnerThickness = 4;
  80. this.previewLoadingSpinner.size = 40;
  81.  
  82. this.fillSubDocInfos(figure);
  83. }
  84.  
  85. createStyle() {
  86. if (document.getElementById("embeddedStyle")) return;
  87. const style = document.createElement("style");
  88. style.id = "embeddedStyle";
  89. style.type = "text/css";
  90. style.innerHTML = `
  91. #embeddedElem {
  92. position: fixed;
  93. width: 100vw;
  94. height: 100vh;
  95. max-width: 1850px;
  96. z-index: 999999;
  97. background: rgba(30,33,38,.65);
  98. }
  99. #embeddedBackgroundElem {
  100. position: fixed;
  101. display: flex;
  102. flex-direction: column;
  103. left: 50%;
  104. transform: translate(-50%, 0%);
  105. margin-top: 20px;
  106. padding: 20px;
  107. background: rgba(30,33,38,.90);
  108. border-radius: 10px;
  109. }
  110. .embeddedSubmissionImg {
  111. max-width: inherit;
  112. max-height: inherit;
  113. border-radius: 10px;
  114. }
  115. #embeddedButtonsContainer {
  116. position: relative;
  117. margin-top: 20px;
  118. margin-bottom: 20px;
  119. margin-left: 20px;
  120. }
  121. #embeddedButtonsWrapper {
  122. display: flex;
  123. justify-content: center;
  124. align-items: center;
  125. }
  126. #previewLoadingSpinnerContainer {
  127. position: absolute;
  128. top: 50%;
  129. right: 0;
  130. transform: translateY(-50%);
  131. }
  132. .embeddedButton {
  133. margin-left: 4px;
  134. margin-right: 4px;
  135. user-select: none;
  136. }
  137. `;
  138. document.head.appendChild(style);
  139. }
  140.  
  141. onRemove(action) {
  142. this._onRemoveAction = action;
  143. }
  144.  
  145. remove() {
  146. this.embeddedElem.parentNode.removeChild(this.embeddedElem);
  147. if (this._onRemoveAction)
  148. this._onRemoveAction();
  149. }
  150.  
  151. createElements(figure) {
  152. this.embeddedElem = document.createElement("div");
  153. this.embeddedElem.id = "embeddedElem";
  154. this.embeddedElem.onclick = (event) => {
  155. if (event.target == this.embeddedElem)
  156. this.remove();
  157. };
  158.  
  159. this.backgroundElem = document.createElement("div");
  160. this.backgroundElem.id = "embeddedBackgroundElem";
  161. notClosingElemsArr.push(this.backgroundElem.id);
  162.  
  163. this.submissionContainer = document.createElement("a");
  164. this.submissionContainer.id = "embeddedSubmissionContainer";
  165. if (openInNewTabSetting.value == true)
  166. this.submissionContainer.target = "_blank";
  167. this.submissionContainer.onclick = () => {
  168. if (closeEmbedAfterOpenSetting.value == true)
  169. this.remove();
  170. };
  171. notClosingElemsArr.push(this.submissionContainer.id);
  172.  
  173. this.backgroundElem.appendChild(this.submissionContainer);
  174.  
  175. this.buttonsContainer = document.createElement("div");
  176. this.buttonsContainer.id = "embeddedButtonsContainer";
  177. notClosingElemsArr.push(this.buttonsContainer.id);
  178.  
  179. this.buttonsWrapper = document.createElement("div");
  180. this.buttonsWrapper.id = "embeddedButtonsWrapper";
  181. notClosingElemsArr.push(this.buttonsWrapper.id);
  182. this.buttonsContainer.appendChild(this.buttonsWrapper);
  183.  
  184. this.favButton = document.createElement("a");
  185. this.favButton.id = "embeddedFavButton";
  186. notClosingElemsArr.push(this.favButton.id);
  187. this.favButton.type = "button";
  188. this.favButton.className = "embeddedButton button standard mobile-fix";
  189. this.favButton.textContent = "⠀⠀";
  190. this.buttonsWrapper.appendChild(this.favButton);
  191.  
  192. this.downloadButton = document.createElement("a");
  193. this.downloadButton.id = "embeddedDownloadButton";
  194. notClosingElemsArr.push(this.downloadButton.id);
  195. this.downloadButton.type = "button";
  196. this.downloadButton.className = "embeddedButton button standard mobile-fix";
  197. this.downloadButton.textContent = "Download";
  198. this.buttonsWrapper.appendChild(this.downloadButton);
  199.  
  200. const userLink = getByLinkFromFigcaption(figure.querySelector("figcaption"));
  201. if (userLink) {
  202. const galleryLink = trimEnd(userLink, "/").replace("user", "gallery");
  203. const scrapsLink = trimEnd(userLink, "/").replace("user", "scraps");
  204. if (!window.location.toString().includes(userLink) && !window.location.toString().includes(galleryLink) && !window.location.toString().includes(scrapsLink)) {
  205. this.openGalleryButton = document.createElement("a");
  206. this.openGalleryButton.id = "embeddedOpenGalleryButton";
  207. notClosingElemsArr.push(this.openGalleryButton.id);
  208. this.openGalleryButton.type = "button";
  209. this.openGalleryButton.className = "embeddedButton button standard mobile-fix";
  210. this.openGalleryButton.textContent = "Open Gallery";
  211. this.openGalleryButton.href = galleryLink;
  212. if (openInNewTabSetting.value == true)
  213. this.openGalleryButton.target = "_blank";
  214. this.openGalleryButton.onclick = () => {
  215. if (closeEmbedAfterOpenSetting.value == true)
  216. this.remove();
  217. };
  218. this.buttonsWrapper.appendChild(this.openGalleryButton);
  219. }
  220. }
  221.  
  222. this.openButton = document.createElement("a");
  223. this.openButton.id = "embeddedOpenButton";
  224. notClosingElemsArr.push(this.openButton.id);
  225. this.openButton.type = "button";
  226. this.openButton.className = "embeddedButton button standard mobile-fix";
  227. this.openButton.textContent = "Open";
  228. const link = figure.querySelector("a[href]");
  229. this.openButton.href = link;
  230. if (openInNewTabSetting.value == true)
  231. this.openButton.target = "_blank";
  232. this.openButton.onclick = () => {
  233. if (closeEmbedAfterOpenSetting.value == true)
  234. this.remove();
  235. };
  236. this.buttonsWrapper.appendChild(this.openButton);
  237.  
  238. this.closeButton = document.createElement("a");
  239. this.closeButton.id = "embeddedCloseButton";
  240. notClosingElemsArr.push(this.closeButton.id);
  241. this.closeButton.type = "button";
  242. this.closeButton.className = "embeddedButton button standard mobile-fix";
  243. this.closeButton.textContent = "Close";
  244. this.closeButton.onclick = () => this.remove();
  245. this.buttonsWrapper.appendChild(this.closeButton);
  246.  
  247. this.previewLoadingSpinnerContainer = document.createElement("div");
  248. this.previewLoadingSpinnerContainer.id = "previewLoadingSpinnerContainer";
  249. notClosingElemsArr.push(this.previewLoadingSpinnerContainer.id);
  250. this.previewLoadingSpinnerContainer.onclick = () => {
  251. this.previewLoadingSpinner.visible = false;
  252. };
  253. this.buttonsContainer.appendChild(this.previewLoadingSpinnerContainer);
  254.  
  255. this.backgroundElem.appendChild(this.buttonsContainer);
  256.  
  257. this.embeddedElem.appendChild(this.backgroundElem);
  258.  
  259. const ddmenu = document.getElementById("ddmenu");
  260. ddmenu.appendChild(this.embeddedElem);
  261. }
  262.  
  263. async fillSubDocInfos(figure) {
  264. const sid = figure.id.split("-")[1];
  265. const ddmenu = document.getElementById("ddmenu");
  266. const doc = await requestHelper.SubmissionRequests.getSubmissionPage(sid);
  267. if (doc) {
  268. this.submissionImg = doc.getElementById("submissionImg");
  269. const imgSrc = this.submissionImg.src;
  270. const prevSrc = this.submissionImg.getAttribute("data-preview-src");
  271. const prevPrevSrc = prevSrc.replace("@600", "@300");
  272.  
  273. const faImageViewer = new CustomImageViewer(imgSrc, prevSrc);
  274. faImageViewer.faImage.id = "embeddedSubmissionImg";
  275. faImageViewer.faImagePreview.id = "previewSubmissionImg";
  276. faImageViewer.faImage.className = faImageViewer.faImagePreview.className = "embeddedSubmissionImg";
  277. faImageViewer.faImage.style.maxWidth = faImageViewer.faImagePreview.style.maxWidth = window.innerWidth - 20 * 2 + "px";
  278. faImageViewer.faImage.style.maxHeight = faImageViewer.faImagePreview.style.maxHeight = window.innerHeight - ddmenu.clientHeight - 38 * 2 - 20 * 2 - 100 + "px";
  279. faImageViewer.onImageLoadStart = () => {
  280. this._previewLoaded = false;
  281. this._imageLoaded = false;
  282. if (this.loadingSpinner)
  283. this.loadingSpinner.visible = false;
  284. };
  285. faImageViewer.onImageLoad = () => {
  286. this._imageLoaded = true;
  287. if (this.loadingSpinner && this.loadingSpinner.visible === true)
  288. this.loadingSpinner.visible = false;
  289. if (this.previewLoadingSpinner && this.previewLoadingSpinner.visible === true)
  290. this.previewLoadingSpinner.visible = false;
  291. };
  292. faImageViewer.onPreviewImageLoad = () => {
  293. this._previewLoaded = true;
  294. if (this._imageLoaded === false)
  295. this.previewLoadingSpinner.visible = true;
  296. };
  297. faImageViewer.load(this.submissionContainer);
  298.  
  299. this.submissionContainer.href = doc.querySelector('meta[property="og:url"]').content;
  300.  
  301. const result = getFavKey(doc);
  302. this.favButton.textContent = result.isFav ? "+Fav" : "-Fav";
  303. this.favButton.setAttribute("isFav", result.isFav);
  304. this.favButton.setAttribute("key", result.favKey);
  305. this.favButton.onclick = () => {
  306. if (this.favRequestRunning == false)
  307. this.doFavRequest(sid);
  308. };
  309.  
  310. this.downloadButton.onclick = () => {
  311. if (this.downloadRequestRunning == true)
  312. return;
  313. this.downloadRequestRunning = true;
  314. const loadingTextSpinner = new LoadingTextSpinner(this.downloadButton);
  315. loadingTextSpinner.delay = loadingSpinSpeedFavSetting.value;
  316. loadingTextSpinner.visible = true;
  317. const iframe = document.createElement("iframe");
  318. iframe.style.display = "none";
  319. iframe.src = this.submissionImg.src + "?eidownload";
  320. iframe.onload = () => {
  321. this.downloadRequestRunning = false;
  322. loadingTextSpinner.visible = false;
  323. setTimeout(() => iframe.parentNode.removeChild(iframe), 100);
  324. };
  325. document.body.appendChild(iframe);
  326. };
  327. }
  328. }
  329.  
  330. async doFavRequest(sid) {
  331. this.favRequestRunning = true;
  332. const loadingTextSpinner = new LoadingTextSpinner(this.favButton);
  333. loadingTextSpinner.delay = loadingSpinSpeedFavSetting.value;
  334. loadingTextSpinner.visible = true;
  335. let favKey = this.favButton.getAttribute("key");
  336. let isFav = this.favButton.getAttribute("isFav");
  337. if (isFav == "true") {
  338. favKey = await requestHelper.SubmissionRequests.favSubmission(sid, favKey);
  339. loadingTextSpinner.visible = false;
  340. if (favKey) {
  341. this.favButton.setAttribute("key", favKey);
  342. isFav = false;
  343. this.favButton.setAttribute("isFav", isFav);
  344. this.favButton.textContent = "-Fav";
  345. } else {
  346. this.favButton.textContent = "x";
  347. setTimeout(() => this.favButton.textContent = "+Fav", 1000);
  348. }
  349. } else {
  350. favKey = await requestHelper.SubmissionRequests.unfavSubmission(sid, favKey);
  351. loadingTextSpinner.visible = false;
  352. if (favKey) {
  353. this.favButton.setAttribute("key", favKey);
  354. isFav = true;
  355. this.favButton.setAttribute("isFav", isFav);
  356. this.favButton.textContent = "+Fav";
  357. } else {
  358. this.favButton.textContent = "x";
  359. setTimeout(() => this.favButton.textContent = "-Fav", 1000);
  360. }
  361. }
  362. this.favRequestRunning = false;
  363. }
  364. }
  365.  
  366. function getByLinkFromFigcaption(figcaption) {
  367. if (figcaption) {
  368. const infos = figcaption.querySelectorAll("i");
  369. let userLink;
  370. for (const info of infos) {
  371. if (info.textContent.toLowerCase().includes("by")) {
  372. const linkElem = info.parentNode.querySelector("a[href][title]");
  373. if (linkElem)
  374. userLink = linkElem.href;
  375. }
  376. }
  377. return userLink;
  378. }
  379. }
  380.  
  381. function getFavKey(doc) {
  382. const columnPage = doc.getElementById("columnpage");
  383. const navbar = columnPage.querySelector('div[class*="favorite-nav"');
  384. const buttons = navbar.querySelectorAll('a[class*="button"][href]');
  385. let favButton;
  386. for (const button of buttons) {
  387. if (button.textContent.toLowerCase().includes("fav"))
  388. favButton = button;
  389. }
  390.  
  391. if (favButton) {
  392. const favKey = favButton.href.split("?key=")[1];
  393. const isFav = !favButton.href.toLowerCase().includes("unfav");
  394. return { favKey, isFav };
  395. }
  396. }
  397.  
  398. let isShowing = false;
  399. let notClosingElemsArr = [];
  400. let embeddedImage;
  401.  
  402. addEmbedded();
  403. window.updateEmbedded = addEmbedded;
  404.  
  405. document.addEventListener("click", (event) => {
  406. if (event.target.parentNode instanceof HTMLDocument && embeddedImage)
  407. embeddedImage.remove();
  408. });
  409.  
  410. async function addEmbedded() {
  411. for (const figure of document.querySelectorAll('figure:not([embedded])')) {
  412. figure.setAttribute('embedded', true);
  413. figure.addEventListener("click", function (event) {
  414. if (!event.ctrlKey && !event.target.id.includes("favbutton") && event.target.type != "checkbox") {
  415. if (event.target.href)
  416. return;
  417. else
  418. event.preventDefault();
  419. if (!isShowing)
  420. showImage(figure);
  421. }
  422. });
  423. }
  424. }
  425.  
  426. async function showImage(figure) {
  427. isShowing = true;
  428. embeddedImage = new EmbeddedImage(figure);
  429. embeddedImage.onRemove(() => {
  430. embeddedImage = null;
  431. isShowing = false;
  432. });
  433. }
  434.  
  435. function downloadImage() {
  436. console.log("Embedded Image Viewer downloading Image...");
  437. let url = window.location.toString();
  438. if (url.includes("?")) {
  439. const parts = url.split('?');
  440. url = parts[0];
  441. }
  442. const download = document.createElement('a');
  443. download.href = url;
  444. download.download = url.substring(url.lastIndexOf("/") + 1);
  445. download.style.display = 'none';
  446. document.body.appendChild(download);
  447. download.click();
  448. document.body.removeChild(download);
  449.  
  450. window.close();
  451. }
  452.  
  453. function trimEnd(string, toRemove) {
  454. if (string.endsWith(toRemove))
  455. string = string.slice(0, -1);
  456. return string;
  457. }