Enchoseon / gelbooru-overhaul-userscript

Various toggleable changes to Gelbooru such as enlarging the gallery, removing the sidebar, and more.
4 stars 2 forks source link

Overhauled #2

Closed PetrK39 closed 1 year ago

PetrK39 commented 2 years ago

Basically I finished refactoring, only a few key features and a final cleanup are left

Later I will add info on the internals and how to add new features Please make sure everything works correctly in all your usecases (You can now configure them via the Overhaul dialog in the top bar, including without reloading the page.)


Adding new preferences

{ name: string, items: {string, PreferenceItem} }

Where items.string is Key
Where PreferenceItem is
```js
 /**
  * @typedef  PreferenceItem
  * @type     {Object}
  * @property {number | string | boolean} value          Value of preference
  * @property {string}                    name           Displayed name in config window
  * @property {string}                    description    Description displayed in config window
  * @property {boolean}                   [locked=false] Determines if preference should be available for editing
  * /

{
    name: string
    description: string
    locked: boolean
    number | string | boolean: value
}

The keys can be any string, but I prefer to keep the category structure Category1.Item1, Category1.Item2, Category2.Item1

Preference value can be accessed using

configManager.findValueByKey(key: string)

Adding new features

You also may want to check currentPageType from utils.pageTypes

If your preference sets css variables, see applyCssVariableGoCollapseSidebar() for example on how to do it If your preference has dependent preferences see applyTweakEnlargeOnHover() for example

You can access page's thumbnails with utils.getThumbnails() which returns HTMLImageElement[] (posts for Gallery page, 'More Like This' for Post page)

Config window automatically display your new preference but there is no validation and fool proof yet Note that values updates live

For thumbnail tweaks on Gallery page there is also infiniteScrolling.addUpdateListener(listener) Where listener is

function(value: number | string | boolean, thumbs: HTMLImageElement[])

Should be applied only for new thumbs

Script starts on DOM ready so there is no need to check this


I don't urge you to take the pool requester right now, but don't forget to replace @resource css with your repository There are many improvements to the original script, something that was impossible before now works fine. You can check out the commit comments

Enchoseon commented 2 years ago

Wow, you've fixed so many undocumented bugs that it makes me feel guilty for not bothering to mention any of them anywhere. I can confirm that infinite scroll, collapsible sidebar, thumbnails are working on Tampermonkey 4.17.6161 for Firefox.

Also noticed that the tags in the additional .txt file from FastDL were missing their newlines (e.g. post id 7724955 downloaded a text file with artist:smile (qd4nsvik)series:originalmeta:highresblue skycablecloudcloudy skyfantasyglowinglandscapelight particlesmoonno humansoceanreflectionreflective watersceneryshadowskystar (sky)starry skysunrisetowertree), while doing the same on Chrome kept the newlines.

I was able to get the newlines to work in Firefox by changing line 1206 from elem.href = "data:text," + text; to elem.href = "data:text," + encodeURIComponent(text);—but I'm not sure if this would break other stuff.

In regards to complexity, what do you think about splitting things into separate files and using a bunch of @require's?

PetrK39 commented 2 years ago

There is also a rare bug where when loading the next page with infinite scrolling, the content is added twice Have you experienced this? And it seems that the "load" event does not firing on GIF images, causing them to stay animated after "background" loading in the preview

PetrK39 commented 2 years ago

Breaking up the script into separate files would be a great idea Especially separate large independent modules like ConfigManager, advanced blacklist and theme manager But, this would require me to learn more about JavaScript and meanwhile, I want to implement basic functionality

PetrK39 commented 2 years ago
Screenshots

Note that (almost) all settings are css values, and, for example, you can use gradients/images/rgb,hsv, etc. in any combination as the background color for the minimized sidebar This also means that instead of values in pixels you can use vw, vh, em, %, and any other css units. I plan to extend this to light/dark theme customization

and yes, css injections work, but I think I'll leave it as it is, I just need to add validation

PetrK39 commented 1 year ago

Note to self: in incognito mode it actually pretty fast check cache clearing, item search speed

Enchoseon commented 1 year ago

Speedup might be from indexdb getting stored in memory rather than on-disk in your browser's incognito mode.

PetrK39 commented 1 year ago

Also, I can't figure out history.pushState It doesn't work until I open any post manually. however, even so, behaviour of the history is far from expected

Enchoseon commented 1 year ago

I'm unfamiliar with history.pushState and modifying browser history, but apparently the "unused" parameter that's being fed htmlDocument.title is nonstandard and deprecated. Not sure what the intended behavior is.

Also, regarding the navbar crash on the issue tracker and homepage, should we support the different navbar format at the expense of the CSS being different? (see pending review for a screenshot) Or do we just want to skip inserting the configuration button entirely and not bother?

PetrK39 commented 1 year ago

Yes, there are several situations where the script breaks the page. Unfortunately, I don't have enough time right now to sit down and put everything back in order.

PetrK39 commented 1 year ago

About browser history. I can confirm that it is a bug in Google Chrome as I have now checked it on Mozzila Firefox and everything works exactly as expected

PetrK39 commented 1 year ago

the script is significantly struggle when working with a massive blacklistlist in addition to a large number of posts

PetrK39 commented 1 year ago

It looks like we can't optimize the blacklist any further. The checkPost function has a complexity of O(post count * blacklist entries count), which results in 16464 comparisons for 392 entries per page[^1] Since not all tags in my general blacklist belong to gelbooru I need to add an ignore list for tag namespaces The saddest part is that there is still a delay between loading posts and hiding them. This can probably be fixed by using the magic of lazy

[^1]: It takes up to 2 seconds to apply the whole blacklist to 10 pages, which to me is acceptable

PetrK39 commented 1 year ago

The blacklist now works fast enough for infinite scrolling But the namespaces for the tags are lost Trying to make mass api queries leads to a lot of net errors

PetrK39 commented 1 year ago

Since they are only used when downloading an additional tag file by RMB, they will only be resolved there, in a stable slow way

PetrK39 commented 1 year ago

I have an idea: Add feature to *star* some tags and pin them on top of the autocompletion list But this would require to copy and modify a copyrighted code Do we need this feature in the main branch? For myself, I'm mostly browse gelbooru for one tag

PetrK39 commented 1 year ago
There is copyright in the autocomplete.js Grabbed from Danbooru, modified minimally to work on Gelbooru https://github.com/danbooru/danbooru Copyright (c) 2013~2021, Danbooru Project All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.

I don't know much about software licensing, but it doesn't seem to be a big deal if we change it.

Enchoseon commented 1 year ago

I think the pinning feature is totally fine to have in the main branch. I'm planning on adding Hydrus integration whenever I have free time, so I'm lenient about cutting things for being "bloat". The only line I'll draw is on importing an external library (e.g. jquery, underscore, etc.)

For the Danbooru code, we should add something like this to the LICENSE file:

The externally maintained libraries used by GelbooruOverhaul are:

- Autocomplete.js, located at danbooru/danbooru/app/javascript/src/javascripts/autocomplete.js, is licensed as follows:
  """
    Copyright (c) 2013~2021, Danbooru Project
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    The views and conclusions contained in the software and documentation are those
    of the authors and should not be interpreted as representing official policies,
    either expressed or implied, of the FreeBSD Project.
  """

Speaking of licensing, I just realized that GelbooruOverhaul doesn't have a license at the moment and is still All Rights Reserved. What license do you think this project should have? Personally, I don't really care whether the license is copyleft or permissive, but you have contributed 99.9% of the code so I'm interested in what you think.

PetrK39 commented 1 year ago

I don't care too. Even though there is only my code, I probably would never have started a project like this if I hadn't seen your project. For integration with Hydrus I'm using Hydrus Companion. And since GelbooruOverhaul aims only at one site I don't see any reason to just copy Hudrus Companion features. But that doesn't mean we can't improve their interaction. But obviously, I didn't manage to work out the architecture for the script on the fly. You can see how much of a problem this is.

Enchoseon commented 1 year ago

Alright, I'm very sorry for being deathly inactive and stonewalling this fantastic PR with my muteness. I'm just going to straight-up merge the changes you have made--remaining issues with lazy loading and infinite scrolling--for the sake of merging them (and doing a little housekeeping) because it has been several months and either this will never be merged or the refactor will need a refactor.