mirror of
https://github.com/stashapp/CommunityScripts.git
synced 2026-04-28 17:47:44 -05:00
Add Random Button plugin to CommunityScripts (#565)
This commit is contained in:
9
plugins/StashRandomButton/LICENSE.md
Normal file
9
plugins/StashRandomButton/LICENSE.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# MIT License
|
||||
|
||||
Copyright (c) 2025 Nightyonlyy
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
38
plugins/StashRandomButton/README.md
Normal file
38
plugins/StashRandomButton/README.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Stash Random Button Plugin
|
||||
|
||||
Adds a "Random" button to the image & scenes page to quickly navigate to a random scene.
|
||||
|
||||
## Features
|
||||
- Adds a "Random" button to the Stash UI.
|
||||
- Selects a random scene via GraphQL query.
|
||||
- Lightweight, no external dependencies.
|
||||
|
||||
## Installation
|
||||
|
||||
1. **Download the Plugin**
|
||||
```bash
|
||||
git clone https://github.com/Nightyonlyy/StashRandomButton.git
|
||||
```
|
||||
|
||||
2. **Copy to Stash Plugins Folder**
|
||||
- Move the `StashRandomButton` folder to:
|
||||
- Windows: `%USERPROFILE%\.stash\plugins\`
|
||||
- Linux/Mac: `~/.stash/plugins/`
|
||||
- Ensure it contains:
|
||||
- `random-button.js`
|
||||
- `random-button.yml`
|
||||
- `random_button.css`
|
||||
|
||||
3. **Reload Plugins**
|
||||
- In Stash, go to `Settings > Plugins` and click "Reload Plugins".
|
||||
- The button should appear on those pages.
|
||||
|
||||
## Usage
|
||||
Click the "Random" button in the navigation bar to jump to a random image or scene depending on the tab.
|
||||
|
||||
## Requirements
|
||||
- Stash version v0.27.2 or higher.
|
||||
|
||||
## Development
|
||||
- Written in JavaScript using the Stash Plugin API.
|
||||
- Edit `random-button.js` to customize and reload plugins in Stash.
|
||||
11
plugins/StashRandomButton/random_button.css
Normal file
11
plugins/StashRandomButton/random_button.css
Normal file
@@ -0,0 +1,11 @@
|
||||
.random-btn {
|
||||
background-color: #ff0052;
|
||||
border-color: #ff0052;
|
||||
color: #fff;
|
||||
margin-left: 10px;
|
||||
}
|
||||
.random-btn:hover {
|
||||
background-color: #a10134;
|
||||
border-color: #a10134;
|
||||
color: #fff;
|
||||
}
|
||||
182
plugins/StashRandomButton/random_button.js
Normal file
182
plugins/StashRandomButton/random_button.js
Normal file
@@ -0,0 +1,182 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
function addRandomButton() {
|
||||
const existingButton = document.querySelector('.random-btn');
|
||||
if (existingButton) {
|
||||
const styles = window.getComputedStyle(existingButton);
|
||||
return true;
|
||||
}
|
||||
|
||||
const navContainer = document.querySelector('.navbar-buttons.flex-row.ml-auto.order-xl-2.navbar-nav');
|
||||
if (!navContainer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const randomButtonContainer = document.createElement('div');
|
||||
randomButtonContainer.className = 'mr-2';
|
||||
randomButtonContainer.innerHTML = `
|
||||
<a href="javascript:void(0)">
|
||||
<button type="button" class="btn btn-primary random-btn" style="display: inline-block !important; visibility: visible !important;">Random</button>
|
||||
</a>
|
||||
`;
|
||||
randomButtonContainer.querySelector('button').addEventListener('click', loadRandomContent);
|
||||
|
||||
|
||||
if (window.location.pathname.match(/^\/(scenes|images)(?:$|\?)/)) {
|
||||
let refButton = document.querySelector('a[href="/scenes/new"]');
|
||||
if (window.location.pathname.includes('/images')) {
|
||||
refButton = document.querySelector('a[href="/stats"]');
|
||||
}
|
||||
if (!refButton) {
|
||||
refButton = navContainer.querySelector('a[href="https://opencollective.com/stashapp"]');
|
||||
}
|
||||
if (refButton) {
|
||||
refButton.parentElement.insertAdjacentElement('afterend', randomButtonContainer);
|
||||
} else {
|
||||
navContainer.appendChild(randomButtonContainer);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (window.location.pathname.match(/\/(scenes|images)\/\d+/)) {
|
||||
const refButton = navContainer.querySelector('a[href="https://opencollective.com/stashapp"]');
|
||||
if (refButton) {
|
||||
refButton.insertAdjacentElement('afterend', randomButtonContainer);
|
||||
} else {
|
||||
const firstLink = navContainer.querySelector('a');
|
||||
if (firstLink) {
|
||||
firstLink.parentElement.insertAdjacentElement('afterend', randomButtonContainer);
|
||||
} else {
|
||||
navContainer.appendChild(randomButtonContainer);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function getParentHierarchy(element) {
|
||||
const hierarchy = [];
|
||||
let current = element;
|
||||
while (current && current !== document.body) {
|
||||
hierarchy.push(current.tagName + (current.className ? '.' + current.className.split(' ').join('.') : ''));
|
||||
current = current.parentElement;
|
||||
}
|
||||
return hierarchy.join(' > ');
|
||||
}
|
||||
|
||||
async function loadRandomContent() {
|
||||
try {
|
||||
const isScenes = window.location.pathname.includes('/scenes');
|
||||
const isImages = window.location.pathname.includes('/images');
|
||||
const type = isScenes ? 'scenes' : isImages ? 'images' : 'scenes';
|
||||
|
||||
const countQuery = `
|
||||
query Find${type.charAt(0).toUpperCase() + type.slice(1)}($filter: FindFilterType) {
|
||||
find${type.charAt(0).toUpperCase() + type.slice(1)}(filter: $filter) {
|
||||
count
|
||||
}
|
||||
}
|
||||
`;
|
||||
const countVariables = { filter: { per_page: 1 } };
|
||||
|
||||
const countResponse = await fetch('/graphql', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ query: countQuery, variables: countVariables })
|
||||
});
|
||||
|
||||
const countResult = await countResponse.json();
|
||||
if (countResult.errors) {
|
||||
return;
|
||||
}
|
||||
|
||||
const totalCount = countResult.data[`find${type.charAt(0).toUpperCase() + type.slice(1)}`].count;
|
||||
if (totalCount === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const randomIndex = Math.floor(Math.random() * totalCount);
|
||||
const itemQuery = `
|
||||
query Find${type.charAt(0).toUpperCase() + type.slice(1)}($filter: FindFilterType) {
|
||||
find${type.charAt(0).toUpperCase() + type.slice(1)}(filter: $filter) {
|
||||
${type} {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
const itemVariables = {
|
||||
filter: { per_page: 1, page: Math.floor(randomIndex / 1) + 1 }
|
||||
};
|
||||
|
||||
const itemResponse = await fetch('/graphql', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ query: itemQuery, variables: itemVariables })
|
||||
});
|
||||
|
||||
const itemResult = await itemResponse.json();
|
||||
if (itemResult.errors) {
|
||||
return;
|
||||
}
|
||||
|
||||
const items = itemResult.data[`find${type.charAt(0).toUpperCase() + type.slice(1)}`][type];
|
||||
if (items.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const itemId = items[0].id;
|
||||
window.location.href = `/${type}/${itemId}`;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
addRandomButton();
|
||||
});
|
||||
|
||||
document.addEventListener('click', (event) => {
|
||||
const target = event.target.closest('a');
|
||||
if (target && target.href) {
|
||||
setTimeout(() => {
|
||||
addRandomButton();
|
||||
}, 1500);
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('popstate', () => {
|
||||
setTimeout(() => {
|
||||
addRandomButton();
|
||||
}, 1500);
|
||||
});
|
||||
|
||||
window.addEventListener('hashchange', () => {
|
||||
setTimeout(() => {
|
||||
addRandomButton();
|
||||
}, 1500);
|
||||
});
|
||||
|
||||
const navContainer = document.querySelector('.navbar-buttons.flex-row.ml-auto.order-xl-2.navbar-nav');
|
||||
if (navContainer) {
|
||||
const observer = new MutationObserver((mutations) => {
|
||||
mutations.forEach(m => {
|
||||
});
|
||||
if (!document.querySelector('.random-btn')) {
|
||||
addRandomButton();
|
||||
}
|
||||
});
|
||||
observer.observe(navContainer, { childList: true, subtree: true });
|
||||
} else {
|
||||
}
|
||||
|
||||
|
||||
let intervalAttempts = 0;
|
||||
setInterval(() => {
|
||||
intervalAttempts++;
|
||||
addRandomButton();
|
||||
}, intervalAttempts < 60 ? 500 : 2000);
|
||||
})();
|
||||
10
plugins/StashRandomButton/random_button.yml
Normal file
10
plugins/StashRandomButton/random_button.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
name: RandomButton
|
||||
description: Adds a button to quickly switch to a random scene or image on both overview and detail pages
|
||||
version: 1.1.0
|
||||
url: https://example.com
|
||||
ui:
|
||||
requires: []
|
||||
javascript:
|
||||
- random_button.js
|
||||
css:
|
||||
- random_button.css
|
||||
Reference in New Issue
Block a user