-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadButton.js
More file actions
156 lines (137 loc) · 4.53 KB
/
Copy pathDownloadButton.js
File metadata and controls
156 lines (137 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// ==UserScript==
// @name Civitai Download Button
// @namespace https://github.com/CeeVeeR/monkeycivit
// @version 1.1
// @description Adds a button to copy wget command for Civitai models
// @author CeeVeer
// @match https://civitai.com/models/*
// @grant GM_setClipboard
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
class Config {
static DEFAULT_API_KEY = 'REDACTED';
static DOWNLOAD_PATH = '/teamspace/studios/this_studio/ComfyUI/models/loras';
static API_KEY_STORAGE_KEY = 'civitai_api_key'; //placeholder, dont recommend hardcoding
}
class ApiKeyManager {
static getApiKey() {
let apiKey = GM_getValue(Config.API_KEY_STORAGE_KEY);
if (!apiKey) {
apiKey = this.promptForApiKey();
if (apiKey) {
GM_setValue(Config.API_KEY_STORAGE_KEY, apiKey);
}
}
return apiKey;
}
static promptForApiKey() {
return prompt('Please enter your Civitai API key (it will be saved for future use):',
Config.DEFAULT_API_KEY);
}
}
class DownloadUrlBuilder {
static addApiKeyToUrl(downloadUrl, apiKey) {
const separator = downloadUrl.includes('?') ? '&' : '?';
return `${downloadUrl}${separator}token=${apiKey}`;
}
static extractFilenameFromUrl(url) {
const urlParts = url.split('/');
const modelId = urlParts[urlParts.length - 1].split('?')[0];
return `model_${modelId}.safetensors`;
}
static buildWgetCommand(downloadUrl, apiKey) {
const urlWithToken = this.addApiKeyToUrl(downloadUrl, apiKey);
const filename = this.extractFilenameFromUrl(downloadUrl);
return `wget -O "${Config.DOWNLOAD_PATH}/${filename}" "${urlWithToken}"`;
}
}
class CopyButton {
static createButton() {
const button = document.createElement('button');
button.innerHTML = '📋 Copy wget Command';
this.applyButtonStyles(button);
return button;
}
static applyButtonStyles(button) {
button.style.cssText = `
padding: 8px 16px;
margin-left: 10px;
border-radius: 8px;
background-color: #2d2d2d;
color: white;
border: none;
cursor: pointer;
`;
this.addHoverEffects(button);
}
static addHoverEffects(button) {
button.addEventListener('mouseover', () => {
button.style.backgroundColor = '#3d3d3d';
});
button.addEventListener('mouseout', () => {
button.style.backgroundColor = '#2d2d2d';
});
}
}
class ClipboardManager {
static copyToClipboard(command) {
GM_setClipboard(command);
this.showCopyConfirmation();
}
static showCopyConfirmation() {
alert('wget command copied to clipboard!');
}
}
class DownloadManager {
constructor() {
this.observer = null;
}
handleCopyClick(downloadUrl) {
const apiKey = ApiKeyManager.getApiKey();
if (!apiKey) {
alert('No API key provided!');
return;
}
const wgetCommand = DownloadUrlBuilder.buildWgetCommand(downloadUrl, apiKey);
ClipboardManager.copyToClipboard(wgetCommand);
}
addCopyButtons() {
const downloadButtons = document.querySelectorAll('a[href*="/api/download/models/"]');
downloadButtons.forEach(downloadButton => {
if (downloadButton.nextSibling?.classList?.contains('copy-url-button')) {
return;
}
const copyButton = CopyButton.createButton();
copyButton.classList.add('copy-url-button');
copyButton.addEventListener('click', (e) => {
e.preventDefault();
this.handleCopyClick(downloadButton.href);
});
downloadButton.parentNode.insertBefore(copyButton, downloadButton.nextSibling);
});
}
initializeObserver() {
this.observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
this.addCopyButtons();
}
});
});
this.observer.observe(document.body, {
childList: true,
subtree: true
});
}
initialize() {
this.addCopyButtons();
this.initializeObserver();
}
}
// Main execution
(function() {
'use strict';
const downloadManager = new DownloadManager();
downloadManager.initialize();
})();