Topic: My Greasemonkey Scripts - Large Thumbnails, Automatic Ordering, Options At The Top, & Saved Tags Search / Autotag

Posted under General

Hello everyone,

I have a few greasemonkey / tampermonkey scripts that I made for myself. Originally I was planning on making these better and then sharing, but it's been quite a while since I've worked on them and I've sort of lost interest.
I'm posting these here in case anyone else might want them, since I don't plan to develop them any further.

One note: the "include" urls are intentionally vague so it's not obvious what these are for. That means they could possibly run on other sites, although I haven't experienced this. You could update the @include to specifically target e621.net

1. Large Thumbnails:

Description:
This one automatically rescales all thumbnails to the "large" size. Could be easily modified to pick a whatever scale you want. Could be updated with an options UI.

Code:

// ==UserScript==
// @name         ReScaler
// @namespace    ReScaler
// @version      1.1
// @description  Scales up the thumbnails
// @author       ReScaler
// @include      /^https\://?.+1\.net/posts/
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js#sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=
// @run-at       document-start
// ==/UserScript==

this.$ = this.jQuery = jQuery.noConflict(true);

// Settings
const cropImages = true,
      thumbWidth = 280,
      thumbHeight = 320;

// Main Script
(function() {
    'use strict';

    // Reset the attached stylesheet
    $("<style>")
        .appendTo("head")
        .html(`
            div#posts-container {
                display: grid;
                grid-template-columns: repeat(auto-fill, ${thumbWidth}px);
                justify-content: space-between;
            }
            article.post-preview {
                width: ${thumbWidth}px;
                ${cropImages ? ("height: " + thumbHeight + "px") : ""};
            }
            article.post-preview img {
                max-width: 100%;
                max-height: 100%;
                width: ${thumbWidth}px;
                ${cropImages ? ("height: " + (thumbHeight - 16) + "px") : ""};
                object-fit: ${cropImages ? "cover" : "contain"};
            }
        `);

    window.addEventListener('DOMContentLoaded', (event) => {
        // Rewrite the thumbnail URLs
        $("article.post-preview").each((index, entry) => {
            let $thumb = $(entry);
            $thumb.find("source").remove();

            let $img = $thumb.find("img").first();
            if($thumb.attr("data-large-file-url")) { $img.attr("src", $thumb.attr("data-large-file-url")); }
        });
    });

})();

2. Automatic Ordering

Description:
This script automatically replaces any ordering with whatever ordering is set in the script. For me, it's "score." Could be easily modified to pick your favorite ordering. Could be updated with an options UI to set the default ordering.

Code:

// ==UserScript==
// @name         OrderBy
// @namespace    OrderBy
// @version      0.1
// @description  Order by score
// @author       OrderBy
// @include      /^https\://?.+1\.net/posts\?tags=/
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    // Order by score
    if ( !window.location.href.includes('+order') ){
        window.location.replace(`${window.location.href}+order%3Ascore`);
    }
})();

3. Options At The Top

Description:
This script simply moves the "Options" section in the side panel of a post to the top, above "Tags." This makes it easier to favorite something, add it to a pool, download it, etc.

Code:

// ==UserScript==
// @name         TopOptions
// @namespace    TopOptions
// @version      0.1
// @description  Move options to top of sidebar
// @author       TopOptions
// @include      /^https\://?.+1\.net/posts/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    // Re-order sidepanel
    $('#post-options').insertBefore('#tag-list');
    $('#post-options').css('padding-bottom', 10);
})();

4. Saved Tags

Description:
This script adds an "Autotag" input to the side panel in the posts section. Any comma-separated tags which are added here will be automatically appended to the search. For me, I have "animated," but this can be easily changed at any time via the side panel menu without changing the code. This will be saved as well, so you don't need to update the code at all if you just want this functionality. The interface is not polished, so to update the tags simply type them in the "Autotag" input and they will be automatically saved as they change. To apply them, refresh the page.

If you have any tags you want to always be appended, you can set them as strings in "defaultQueryTags," e.g.,

defaultQueryTags = ["animated"];

If you want to set different default tabs for the "posts" page vs the "search" page, you can set "redirectPostsPage = true" and set the default tags in defaultPostsTags.

// ==UserScript==
// @name         Autotag
// @namespace    Autotag
// @version      0.5
// @description  Automatically add tags to search
// @author       Autotag
// @include      /^https\://?.+1\.net/posts/
// @grant        none
// @run-at       document-start
// ==/UserScript==

/* Options *///////////////////////////
//~ Show auto-tag input form in sidebar
const showOptions = true;
//~ Default tags for query page (i.e., site.net/posts?tags=something)
const defaultQueryTags = [];
//~ Redirect "Posts" page to query page with search tags set to defaultPostsTags
const redirectPostsPage = false;
//~ Default tags for posts page (i.e, site.net/posts). Does nothing if redirectPostsPage !== true
const defaultPostsTags = [];
//////////////////////////////////////

// LocalStorage identifier - just to keep keys unique
const storageTag = "tm-autotag-323DDS"

// "query" or "posts"
// Set upon script launch. Do not modify.
let page = "query";

function _tmLogContext(level){
    return (msg) => {
        msg = `tm-Autotag: ${msg}`;
        if (typeof msg !== 'string') return;
        switch(level){
            case 'info':
                return console.info(msg);
            case 'warn':
                return console.warn(msg);
            case 'error':
                return console.error(msg);
            default:
                return console.log(msg);
        }
    };
}

// Hijack the console
const logger = {
    'log': _tmLogContext('log'),
    'info': _tmLogContext('info'),
    'warn': _tmLogContext('info'),
    'error': _tmLogContext('info')
};

// Trim whitespace, replace remaining whitespace with underscore, replace ':' with HTML colon
function formatTags(tags){
    tags.forEach((el, idx, arr) => {
        arr[idx] = el.toLowerCase().trim().replace(/\s*:\s*/g, '%3A').replace(/%3a/g, '%3A').replace(/\s+/g, '_');
    });
}

// Get tags from LocalStorage if available, else return defaults
// Sets tags in LocalStorage to default values if they are missing or invalid
function getTags(){
    if (!showOptions) return (page === "posts") ? defaultPostsTags : defaultQueryTags;
    let storageName = (page === "posts") ? "postsTags" : "queryTags";
    let _tags = [];
    try {
        _tags = JSON.parse(localStorage[`${storageTag}-${storageName}`]);
    } catch(err){
        logger.error(`Failed parsing setting: ${localStorage[`${storageTag}-${storageName}`]}`);
        logger.error(`Error: ${err}`);
    }
    if (!Array.isArray(_tags)){
        _tags = (page === "posts") ? defaultPostsTags : defaultQueryTags;
        localStorage[`${storageTag}-${storageName}`] = JSON.stringify(_tags);
    } else {
        formatTags(_tags);
    }
    return _tags;
}

// Sets tags in the page URL. Does NOT update tags in LocalStorage
function setTags(tags){
    if (!Array.isArray(tags)) return;
    let paramString = "";
    let location = window.location.href;
    for (let tag of tags){
        if ( !location.includes(`+${tag.split('%3A')[0]}`) && !location.includes(`tags=${tag.split('%3A')[0]}`) && !paramString.includes(`+${tag.split('%3A')[0]}`)){
            paramString += `+${tag}`;
        }
    }
    if (paramString.length > 0){
        if (page === "posts") paramString = `?tags=${paramString.slice(1)}`;
        window.location.replace(`${window.location.href}${paramString}`);
    }
}

// Inject the auto-tags input
function buildOptions(){
    let _localTags = getTags();
    let autotagshowOptions = $(`<section id="autotag-showOptions"><div>Auto-Tag</div><div><p style="font-size:10px">Comma-separated list of tags:</p><input type="text" name="autotag-tags" id="autotag-tags" value="${_localTags.join(", ").replace(/%3a/gi, ":")}" data-shortcut="a" title="Shortcut is a", style="width:100%"></div></section>`);
    autotagshowOptions.css({'padding-top': 8,
                            'padding-right': 4,
                            'padding-bottom': 8});
    autotagshowOptions.insertAfter('#search-box');
    setTags(_localTags);
    $('#autotag-tags').change(() => {
        let newTags = $('#autotag-tags').val().split(",");
        formatTags(newTags);
        let storageName = (page === "posts") ? "postsTags" : "queryTags";
        localStorage[`${storageTag}-${storageName}`] = JSON.stringify(newTags);
    });
}

(function() {
    'use strict';

    // Do nothing if we're on the posts page and redirectPostsPage is not enabled
    if (/\/posts$/mi.test(window.location.href) && redirectPostsPage !== true) return;

    // Don't run if there's an actual query, this is currently broken
    if(/\/posts\/\d+(\?|\&)q=/i.test(window.location.href)) return;

    // Set page location
    if (window.location.href.includes("?tags=")){
        page = "query";
    } else {
        page = "posts"
    }

    // Ensure default tags are valid
    if (!Array.isArray(defaultQueryTags)) defaultQueryTags = [];
    formatTags(defaultQueryTags);
    if (!Array.isArray(defaultPostsTags)) defaultPostsTags = [];
    formatTags(defaultPostsTags);

    // Create LocalStorage keys if they are missing
    if (localStorage[`${storageTag}-postsTags`] === null || localStorage[`${storageTag}-postsTags`] === undefined){
        logger.log('Creating posts page settings data');
        localStorage[`${storageTag}-postsTags`] = JSON.stringify(defaultPostsTags);
    }
    if (localStorage[`${storageTag}-queryTags`] === null || localStorage[`${storageTag}-queryTags`] === undefined){
        logger.log('Creating query page settings data');
        localStorage[`${storageTag}-queryTags`] = JSON.stringify(defaultQueryTags);
    }

    // Actually set the proper tags
    setTags(getTags());

    // If auto-tag input is enabled, inject it into the DOM once it's finished loading the page
    if (showOptions === true){
        window.addEventListener('DOMContentLoaded', (event) => {
            buildOptions();
        });
    }
})();

Let me know if this helps anyone. I may be willing to make updates / fixes but my time for this will be limited.

Cheers,
teerac

Updated

Thanks for the scripts!
I've used the rescaler one for a while now, but it seems like it recently broke. Any ideas for a fix?

203f98h2908h9f said:
Thanks for the scripts!
I've used the rescaler one for a while now, but it seems like it recently broke. Any ideas for a fix?

0 clue
It's broken for me too.

therealchud said:
0 clue
It's broken for me too.

I tried the rescaler script just now and it seems to work for me, on desktop Firefox 115.13.0, with Tampermonkey 5.1.1.

In the Tampermonkey editor, there are some warnings that one of the statements (the @include /^https\://?.+1\.net/posts/, near the top) might break in Chrome, once Chrome moves to "Manifest v3". This is Google's latest attempt to kill uBlock Origin and other ad blockers. I don't know if that's what is breaking the script, but that's my guess at what's doing it.

If it's broken in Chrome, and you install Firefox, Tampermonkey, and the rescaler script, and then it works, then that points the finger at Chrome.

I'm told that RE621 has similar functionality for displaying larger thumbnails, but I'm not too familiar with it.

kora_viridian said:
I tried the rescaler script just now and it seems to work for me, on desktop Firefox 115.13.0, with Tampermonkey 5.1.1.

In the Tampermonkey editor, there are some warnings that one of the statements (the @include /^https\://?.+1\.net/posts/, near the top) might break in Chrome, once Chrome moves to "Manifest v3". This is Google's latest attempt to kill uBlock Origin and other ad blockers. I don't know if that's what is breaking the script, but that's my guess at what's doing it.

If it's broken in Chrome, and you install Firefox, Tampermonkey, and the rescaler script, and then it works, then that points the finger at Chrome.

I'm told that RE621 has similar functionality for displaying larger thumbnails, but I'm not too familiar with it.

115.13.0 is an ESR release, so this points to the issues being some update to the standard firefox, damn

203f98h2908h9f said:
115.13.0 is an ESR release, so this points to the issues being some update to the standard firefox, damn

Special, for you, today: I downloaded desktop Firefox 129.0 and tried it there. It still works for me.

Details:
Brand-new installation of Firefox 129.0, no previous data imported. Installed Tampermonkey 5.1.1 and the rescaler script (1. Large Thumbnails in the OP). Went to https://e621.net/posts . The script worked and I got large thumbnails.

Added uBlock Origin 1.59.0, with all default settings, and let it update its filter lists. Went to a different page of the recent posts. The script worked and I got large thumbnails.

Turned Firefox's "Enhanced Tracking Protection" from "standard" (the default) up to "script", and let it reload all my tabs. Went to a different page of the recent posts. The script worked and I got large thumbnails.

One possible problem:
The rescaler script tries to load a Javascript library (jQuery) from an external site. If that fails, maybe due to some kind of privacy or security software on your machine, then the rescaler script might not work.

Test:
In a new tab, go to https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js#sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo= . You should see a bunch of code that starts with /*! jQuery v3.4.1 | (c) JS Foundation and other contributors | jquery.org/license */ on the top line. If you don't see that line, or if you get some kind of "blocked" or "not found" page, then figure out what software on your machine is doing the blocking. It could be another browser plugin, or it could be anti-virus software, or possibly other things.

kora_viridian said:
Special, for you, today: I downloaded desktop Firefox 129.0 and tried it there. It still works for me.

Details:
Brand-new installation of Firefox 129.0, no previous data imported. Installed Tampermonkey 5.1.1 and the rescaler script (1. Large Thumbnails in the OP). Went to https://e621.net/posts . The script worked and I got large thumbnails.

Added uBlock Origin 1.59.0, with all default settings, and let it update its filter lists. Went to a different page of the recent posts. The script worked and I got large thumbnails.

Turned Firefox's "Enhanced Tracking Protection" from "standard" (the default) up to "script", and let it reload all my tabs. Went to a different page of the recent posts. The script worked and I got large thumbnails.

One possible problem:
The rescaler script tries to load a Javascript library (jQuery) from an external site. If that fails, maybe due to some kind of privacy or security software on your machine, then the rescaler script might not work.

Test:
In a new tab, go to https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js#sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo= . You should see a bunch of code that starts with /*! jQuery v3.4.1 | (c) JS Foundation and other contributors | jquery.org/license */ on the top line. If you don't see that line, or if you get some kind of "blocked" or "not found" page, then figure out what software on your machine is doing the blocking. It could be another browser plugin, or it could be anti-virus software, or possibly other things.

I do applaud the effort, but just to make sure, since you only mention large thumbnails which is not the issue, you're getting high quality version of the thumbnails? Because just getting large thumbnails is not the issue; the scaling part is. We're not getting those high quality versions, instead we're just getting the original thumbnails stretched out, making them pixelated.
Just tested this on a completely fresh linux machine in firefox (both ESR and Standard) and just like my main machine, I'm still getting large thumbnails, but the subsequent rescaling fails. Starting to think I have to look at tampermonkey versions or something...

Access to the jQuery site is not the problem, but thank you for mentioning to check that as well

203f98h2908h9f said:
Because just getting large thumbnails is not the issue; the scaling part is.

The problem descriptions I was working with were "it seems like it recently broke" and "It's broken for me too". There was no description of what "broke" actually meant. There was also no browser name or version, no userscript manager name or version, no information on desktop or mobile. So I was flying mostly blind. I was interpreting "broke" to mean that the thumbnails were displayed in exactly the same way as the vanilla site, or possibly that the thumbnails didn't display at all.

We're not getting those high quality versions, instead we're just getting the original thumbnails stretched out, making them pixelated.

I was getting the stretched, pixelated versions too. Without any other information, I thought that's how the original userscript was supposed to work.

Now that we have lifted the foreskin of ignorance and applied the wire brush of enlightenment, I think I fixed it. :D

Changing the last non-parentheses-and-braces line of OP's script to

if($thumb.attr("data-large-url")) { $img.attr("src", $thumb.attr("data-large-url")); }

makes it work for me, on desktop Firefox 115.13.0, with Tampermonkey 5.1.1. The thumbnails display in a larger size and are a high-quality image - not stretched or pixelated.

This code has no warranty. If it breaks you get to keep all the pieces.

The problem is, I think, that e621's markup changed. The individual thumbnail entries don't have a data-large-file-url attribute anymore - now it's data-large-url. The data-large-url has a URL of the form https://static1.e621.net/data/sample/0d/db/0ddbeefdeadf00d.jpg . On one random image that's on the front page now, that URL goes to an 850x1097 JPEG. (The vanilla thumbnail URL for that image has /preview/ in it instead of /sample/, and goes to a 116x150 JPEG.)

To find exactly when it changed, I tried searching for those attributes in the e621 code , but I couldn't find it. (Note that code search is now privacywalled on Github - you have to log in.) Looking at the most recent post in the changelog thread here, I think it probably changed when this commit went into production. That probably happened on either 2024-07-29 (the headline in that forum post) or 2024-08-05 (the date of that forum post).

edit: fixed URL

Updated

kora_viridian said:
The problem descriptions I was working with were "it seems like it recently broke" and "It's broken for me too". There was no description of what "broke" actually meant. There was also no browser name or version, no userscript manager name or version, no information on desktop or mobile. So I was flying mostly blind. I was interpreting "broke" to mean that the thumbnails were displayed in exactly the same way as the vanilla site, or possibly that the thumbnails didn't display at all.

I was getting the stretched, pixelated versions too. Without any other information, I thought that's how the original userscript was supposed to work.

Now that we have lifted the foreskin of ignorance and applied the wire brush of enlightenment, I think I fixed it. :D

Changing the last non-parentheses-and-braces line of OP's script to

if($thumb.attr("data-large-url")) { $img.attr("src", $thumb.attr("data-large-url")); }

makes it work for me, on desktop Firefox 115.13.0, with Tampermonkey 5.1.1. The thumbnails display in a larger size and are a high-quality image - not stretched or pixelated.

This code has no warranty. If it breaks you get to keep all the pieces.

The problem is, I think, that e621's markup changed. The individual thumbnail entries don't have a data-large-file-url attribute anymore - now it's data-large-url. The data-large-url has a URL of the form https://static1.e621.net/data/sample/0d/db/0ddbeefdeadf00d.jpg . On one random image that's on the front page now, that URL goes to an 850x1097 JPEG. (The vanilla thumbnail URL for that image has /preview/ in it instead of /sample/, and goes to a 116x150 JPEG.)

To find exactly when it changed, I tried searching for those attributes in the e621 code , but I couldn't find it. (Note that code search is now privacywalled on Github - you have to log in.) Looking at the most recent post in the changelog thread here, I think it probably changed when this commit went into production. That probably happened on either 2024-07-29 (the headline in that forum post) or 2024-08-05 (the date of that forum post).

edit: fixed URL

That's on me for sure, I thought the original script description included that bit, sorry for being rude like that!!!

And yup, can confirm modifying that last line makes everything work flawless again, really appreciate it!
Also nice that you found the commit in question, just goes to show that front-end really isn't my specialty

  • 1