-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
120 lines (101 loc) · 3.44 KB
/
script.js
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
// Content script
function addButton() {
// Select all the existing download buttons
let downloadButtons = document.querySelectorAll(
'.mantine-UnstyledButton-root.mantine-Button-root.mantine-4fe1an'
);
// Loop through each download button
downloadButtons.forEach((downloadButton) => {
// Get the parent container of the download button
const buttonContainer = downloadButton.parentNode;
const downloadParams = downloadButton.href.split("?")[1];
// Check if the "Download with Stability Matrix" button already exists
const existingButton = buttonContainer.parentNode.querySelector(
'a[href^="stabilitymatrix://downloadCivitModel"]'
);
// If the button already exists, skip creating a new one
if (existingButton) {
return;
}
// Check if the create button exists
if (downloadParams) {
// Create a new button element
const newButton = document.createElement("a");
newButton.className =
"mantine-UnstyledButton-root mantine-Button-root mantine-4fe1an";
newButton.style.backgroundColor = "#68339f";
newButton.style.border = "none";
newButton.style.borderRadius = "4px";
newButton.style.color = "#fff";
newButton.style.cursor = "pointer";
// Get the IDs from the URL
const url = new URL(window.location.href);
let modelId = url.pathname.split("/").pop();
// Check if the modelId is an integer, and if not, pop again
let isNum = /^\d+$/.test(modelId);
if (!isNum) {
modelId = url.pathname.split("/")[2];
}
const modelVersionId = url.searchParams.get("modelVersionId");
// Create the link based on the IDs
const link = `stabilitymatrix://downloadCivitModel?modelid=${modelId}${
modelVersionId
? `&modelVersionId=${modelVersionId}&${downloadParams}`
: `&${downloadParams}`
}`;
newButton.href = link;
// Create the inner content of the button
const buttonInner = document.createElement("div");
buttonInner.className = "mantine-3xbgk5 mantine-Button-inner";
const buttonLabel = document.createElement("span");
buttonLabel.className = "mantine-qo1k2 mantine-Button-label";
buttonLabel.textContent = "Download with Stability Matrix";
buttonInner.appendChild(buttonLabel);
newButton.appendChild(buttonInner);
// Insert the new button after the button container
buttonContainer.parentNode.insertBefore(
newButton,
buttonContainer.previousSibling
);
}
});
}
function waitForElement(selector, callback) {
const observer = new MutationObserver((mutations, me) => {
const element = document.querySelector(selector);
if (element && element.href) {
callback(element);
me.disconnect(); // stop observing
}
});
observer.observe(document, {
childList: true,
subtree: true,
});
}
waitForElement(
'.mantine-UnstyledButton-root.mantine-Button-root.mantine-4fe1an',
(element) => {
addButton();
}
);
navigation.addEventListener("navigate", async (event) => {
if (!event.canIntercept) {
return;
}
const url = new URL(event.destination.url);
if (event.downloadRequest !== null) {
return;
}
if (
!url.href.includes("stabilitymatrix://") &&
!url.href.includes("/images/")
) {
waitForElement(
'.mantine-UnstyledButton-root.mantine-Button-root.mantine-4fe1an',
(element) => {
addButton();
}
);
}
});