Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> [Script] Random button for galleries list

 
post Nov 20 2021, 00:57
Post #1
uareader



Critter
*********
Group: Catgirl Camarilla
Posts: 5,592
Joined: 1-September 14
Level 500 (Ponyslayer)


I don't like web development, javascript and all these things, but I wanted that feature for so long, I finally made it myself (IMG:[invalid] style_emoticons/default/anime_cry.gif)
Hopefully, I didn't screw anything in HTML, javascript, or tampermonkey (that's what I made this in, in case it's incompatible with other scripts enabling thingies), but it worked for me.
Not a good presentation:
- I wanted to put the button next to the others next to the search bar, but it's all inside a form that cause the page to reload, and it was too much of a pain for me to try to get around this stuff if it's even possible
- <br> is overkill, but styling already drive me crazy enough when I have to customize that damn Firefox, won't try.

Hopefully, even if some parts suck, it can be a base for others to make something better thanks to part of the job being done.
Anyway, here's the code:

CODE

// ==UserScript==
// @name         Random button for e-hentai
// @namespace    uareader_random_button
// @version      1.2
// @description  *fap* *fap* *fap*
// @author       uareader
// @match        https://e-hentai.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var buttonRandom;
    var galleries = document.getElementsByClassName("gl1t");
    var galleriesLength = galleries.length;
    var galleriesContainer;

    // create a button
    if(galleriesLength > 1) {

        let buttonRandom_insertion_area = document.querySelector("form[action='" + window.location.origin + window.location.pathname + "']");

        if(buttonRandom_insertion_area != null) {

            buttonRandom_insertion_area = buttonRandom_insertion_area.parentElement;

            buttonRandom = document.createElement("input");
            buttonRandom.value = "Randomize";
            buttonRandom.type = "button";
            buttonRandom.addEventListener('click',randomizeSearchResults,false);

            buttonRandom_insertion_area.appendChild(document.createElement("br"));
            buttonRandom_insertion_area.appendChild(buttonRandom);

            galleriesContainer = galleries[0].parentElement;

        }

    }

    // function that randomize the galleries results
    function randomizeSearchResults(click_event) {

        buttonRandom.removeEventListener('click',randomizeSearchResults,false);

        var indexesList = [];
        var tempValue;
        var randomIndex;

        // create an array of indexes
        for(let i = 0; i < galleriesLength; i++) {
            indexesList.push(i);
        }

        // swap each index with another from after within the array
        for(let i= 0; i < galleriesLength - 1; i++) {

            tempValue = indexesList[i];
            randomIndex = Math.floor(Math.random() * (galleriesLength - (i+1)) + (i+1));

            indexesList[i] = indexesList[randomIndex];
            indexesList[randomIndex] = tempValue;

        }

        // swap the last index with another from before within the array
        tempValue = indexesList[galleriesLength-1];
        randomIndex = Math.floor(Math.random() * (galleriesLength-1));

        indexesList[galleriesLength-1] = indexesList[randomIndex];
        indexesList[randomIndex] = tempValue;

        // finally randomize the galleries
        for(let i= 0; i < galleriesLength; i++) {
            galleriesContainer.insertBefore(galleriesContainer.childNodes[i], galleriesContainer.childNodes[indexesList[i]]);
        }

        buttonRandom.addEventListener('click',randomizeSearchResults,false);

    }

})();


edit: changed code to version 1.1 to handle favorites (more important than normal search actually)
I do think I screwed up somewhere and it's not as random as it should (IMG:[invalid] style_emoticons/default/unsure.gif)

edit: changed code to version 1.2

This post has been edited by uareader: Dec 5 2021, 11:05
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

 
post Nov 20 2021, 04:14
Post #2
kzmkzmkzmkzm



Newcomer
*
Group: Members
Posts: 31
Joined: 18-October 10
Level 390 (Destined)


If you just want to change the display order, you can also write it as follows.
I hope you will find it useful.

CODE
(() => {
  // 1) Add shuffle button
  const buttonContainerElement = 'p.nopm'; // '.itc tr:last-child';
  document.querySelector(buttonContainerElement)?.insertAdjacentHTML('beforeend', '<td><div id="shuffle" class="cs" style="background:#111;">Shuffle !</div></td>');

  // 2) Add click event to shuffle button
  document.querySelector('#shuffle').addEventListener('click', () => {
    // Add a random order property to the style of all gallery elements
    document.querySelectorAll('.gl1t').forEach(e => e.style.order = `${parseInt(Math.random() * 1000)}`);
  });
})();

Child elements of flex or grid container elements can change the "display order" of the elements with the order property.
[developer.mozilla.org] order - CSS: Cascading Style Sheets | MDN
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

 
post Dec 5 2021, 11:07
Post #3
uareader



Critter
*********
Group: Catgirl Camarilla
Posts: 5,592
Joined: 1-September 14
Level 500 (Ponyslayer)


Version 1.2:
  • Use the same code whether in favorites or global galleries viewing
  • The button is always centered
  • No longer display the button if only 1 gallery is found
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

 
post Dec 5 2021, 12:06
Post #4
Mayriad



SUPER ★ BUSY ★ TIME
*******
Group: Global Mods
Posts: 2,061
Joined: 18-December 10
Level 135 (Lord)


Added to userscript index.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

 
post Dec 15 2021, 09:54
Post #5
balloondraw



Newcomer
**
Group: Members
Posts: 79
Joined: 15-October 10


Feel like maybe you wanted a button like this?
CODE
(function() {
    'use strict';

    var buttonRandom;
    var galleries = document.getElementsByClassName("gl1t");
    var galleriesLength = galleries.length;
    var galleriesContainer;

    //Creates a button
    if(galleriesLength > 1) {

        let buttonRandom_insertion_area = document.querySelector("form[action='" + window.location.origin + window.location.pathname + "']");

        if(buttonRandom_insertion_area != null) {

            buttonRandom_insertion_area = buttonRandom_insertion_area.parentElement;

            buttonRandom = document.createElement("input");
            buttonRandom.value = "Random Gallery";
            buttonRandom.type = "button";
            buttonRandom.addEventListener('click',randomizeSearchResults,false);

            buttonRandom_insertion_area.appendChild(document.createElement("br"));
            buttonRandom_insertion_area.appendChild(buttonRandom);

            galleriesContainer = galleries[0].parentElement;

        }

    }

    //Opens one of the random gallery urls in a new tab
    function randomizeSearchResults(click_event) {
        var links = document.getElementsByClassName('gl4t')
        var idx = Math.floor(Math.random() * links.length)
        var poop = links[idx].getElementsByTagName('a')[0]
        window.open(poop, '_blank');




    }

})();
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 1st April 2025 - 12:47