Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name ImageFap Gallery Link Indexer + Continuous Pagination
- // @namespace http://tampermonkey.net
- // @version 3.0
- // @description Calculates the page offset to maintain strict #0, #1, #2 sequential numbering across different gallery pages
- // @author Gemini
- // @match https://www.imagefap.com/pictures/*
- // @match https://www.imagefap.com/gallery/*
- // @run-at document-end
- // @grant none
- // @license free, as is
- // ==/UserScript==
- (function() {
- 'use strict';
- let isUpdating = false;
- // Helper function to extract URL parameters
- function getUrlParameter(name, url = window.location.href) {
- name = name.replace(/[\[\]]/g, '\\$&');
- const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
- const results = regex.exec(url);
- if (!results) return null;
- if (!results[2]) return '';
- return decodeURIComponent(results[2].replace(/\+/g, ' '));
- }
- // Determine the base starting index based on the view mode and current page number
- function calculateStartingIndex() {
- const viewMode = getUrlParameter('view') || '2'; // Default to One Page (view=2) if not specified
- const pageParam = getUrlParameter('page') || '0'; // Default to first page (page=0)
- const pageNumber = parseInt(pageParam, 10) || 0;
- let itemsPerPage = 100; // Grid size for One Page mode (4x25 = 100)
- if (viewMode === '0') {
- itemsPerPage = 24; // Grid size for Detailed View mode (4x6 = 24)
- }
- // Formula: page_number * items_per_page
- return pageNumber * itemsPerPage;
- }
- function processGalleryLinks() {
- if (isUpdating) return;
- const gallery = document.getElementById('gallery');
- if (!gallery) {
- console.log("[Tampermonkey] #gallery container not found on this page.");
- return;
- }
- isUpdating = true;
- // Calculate where the count should start for this specific page
- const startingOffset = calculateStartingIndex();
- const links = gallery.querySelectorAll('a');
- let localIndex = 0;
- links.forEach((link) => {
- const rawHref = link.getAttribute('href');
- if (rawHref && rawHref.includes('/photo/')) {
- // Total global index = starting page offset + relative link order on current page
- const globalIndex = startingOffset + localIndex;
- const targetHash = '#' + globalIndex;
- if (!rawHref.endsWith(targetHash)) {
- const baseUrl = rawHref.split('#'); // Clear any existing trailing hash
- link.setAttribute('href', baseUrl + targetHash);
- }
- localIndex++;
- }
- });
- console.log(`[Tampermonkey] Page Offset: ${startingOffset}. Successfully indexed links on this page: ${localIndex}`);
- isUpdating = false;
- }
- // Initialize layout processing immediately
- processGalleryLinks();
- // Watch for infinite scroll additions or live DOM manipulations
- const observer = new MutationObserver((mutations) => {
- for (let mutation of mutations) {
- if (mutation.addedNodes.length > 0) {
- processGalleryLinks();
- break;
- }
- }
- });
- const targetNode = document.getElementById('gallery');
- if (targetNode) {
- observer.observe(targetNode, { childList: true, subtree: true });
- }
- // Fallback trigger in case layout structure renders after minor delays
- window.addEventListener('load', () => {
- setTimeout(processGalleryLinks, 300);
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment