Skip to content

Instantly share code, notes, and snippets.

@vitoo
Last active September 20, 2024 10:53
Show Gist options
  • Save vitoo/e83f8644e22e687150c6d3c79fa666e0 to your computer and use it in GitHub Desktop.
Save vitoo/e83f8644e22e687150c6d3c79fa666e0 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name JVC_ImageViewer
// @namespace http://tampermonkey.net/
// @version 1.38.8
// @description Naviguer entre les images d'un post sous forme de slideshow en cliquant sur une image sans ouvrir NoelShack. Ferme le visualiseur lors du clic sur le bouton retour du navigateur.
// @author HulkDu92
// @match https://*.jeuxvideo.com/forums/*
// @match https://jvarchive.com/forums/*
// @grant none
// @run-at document-end
// @license MIT
// @downloadURL https://update.greasyfork.org/scripts/508447/JVC_ImageViewer.user.js
// @updateURL https://update.greasyfork.org/scripts/508447/JVC_ImageViewer.meta.js
// ==/UserScript==
(function() {
'use strict';
class ImageViewer {
constructor() {
if (ImageViewer.instance) {
return ImageViewer.instance;
}
// ... (existing constructor code)
this.isViewerOpen = false;
this.handlePopState = this.handlePopState.bind(this);
ImageViewer.instance = this;
this.createOverlay();
this.updateImage();
}
// ... (existing methods)
openViewer(images, currentIndex) {
if (this.overlay) {
this.images = images;
this.currentIndex = currentIndex;
this.updateImage();
this.isViewerOpen = true;
this.addHistoryState();
} else {
new ImageViewer();
this.images = images;
this.currentIndex = currentIndex;
this.createOverlay();
this.updateImage();
this.isViewerOpen = true;
this.addHistoryState();
}
window.addEventListener('popstate', this.handlePopState);
}
closeViewer() {
if (this.overlay) {
document.body.removeChild(this.overlay);
this.overlay = null;
this.isViewerOpen = false;
ImageViewer.instance = null; // Réinitialise l'instance singleton
window.removeEventListener('popstate', this.handlePopState);
}
}
addHistoryState() {
history.pushState({ imageViewerOpen: true }, '');
}
handlePopState(event) {
if (this.isViewerOpen) {
event.preventDefault();
this.closeViewer();
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment