Uthar commited on
Commit
2f228e5
·
verified ·
1 Parent(s): f0197f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -118,22 +118,11 @@ def add_gallery(image, model_str, gallery):
118
  JS="""
119
  <script>
120
 
121
- /*
122
- function simulateButtonPress_() {
123
- const button = document.getElementById('simulate-button');
124
- if (button) {
125
- button.click(); // Simulate the button press
126
- console.log('Button Pressed!');
127
- }
128
- }
129
- */
130
-
131
- function simulateButtonPress() {
132
- console.log('Button Pressed!');
133
- }
134
-
135
  // Function to monitor image src changes and automatically download the image
136
  function monitorImageSrcChanges() {
 
 
 
137
  // Create a MutationObserver instance
138
  const observer = new MutationObserver((mutationsList, observer) => {
139
  // Loop through all mutations
@@ -181,8 +170,17 @@ function monitorImageSrcChanges() {
181
  srcObserver.observe(img, { attributes: true, attributeFilter: ['src'] });
182
  }
183
 
184
- // Function to download an image automatically
185
  function downloadImage(src) {
 
 
 
 
 
 
 
 
 
186
  const link = document.createElement('a');
187
  link.href = src;
188
  link.download = src.split('/').pop(); // Use the file name from the URL (last part of the src)
@@ -190,6 +188,11 @@ function monitorImageSrcChanges() {
190
  document.body.appendChild(link);
191
  link.click(); // Trigger the download
192
  document.body.removeChild(link); // Clean up the DOM by removing the link after download
 
 
 
 
 
193
  }
194
  }
195
 
 
118
  JS="""
119
  <script>
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  // Function to monitor image src changes and automatically download the image
122
  function monitorImageSrcChanges() {
123
+ // Set of recently downloaded image URLs to avoid re-triggering the download
124
+ const downloadedImages = new Set();
125
+
126
  // Create a MutationObserver instance
127
  const observer = new MutationObserver((mutationsList, observer) => {
128
  // Loop through all mutations
 
170
  srcObserver.observe(img, { attributes: true, attributeFilter: ['src'] });
171
  }
172
 
173
+ // Function to download an image automatically with a cooldown to prevent multiple downloads
174
  function downloadImage(src) {
175
+ // Check if the image has been downloaded recently
176
+ if (downloadedImages.has(src)) {
177
+ return; // Prevent duplicate downloads
178
+ }
179
+
180
+ // Add the image src to the set of downloaded images
181
+ downloadedImages.add(src);
182
+
183
+ // Trigger the download
184
  const link = document.createElement('a');
185
  link.href = src;
186
  link.download = src.split('/').pop(); // Use the file name from the URL (last part of the src)
 
188
  document.body.appendChild(link);
189
  link.click(); // Trigger the download
190
  document.body.removeChild(link); // Clean up the DOM by removing the link after download
191
+
192
+ // Set a cooldown to allow the download to be triggered again after a delay (e.g., 500ms)
193
+ setTimeout(() => {
194
+ downloadedImages.delete(src); // Remove from the set after the cooldown
195
+ }, 500); // 500ms cooldown (adjust as needed)
196
  }
197
  }
198