IdlecloudX commited on
Commit
c4ebff6
·
verified ·
1 Parent(s): 800b58e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -13
app.py CHANGED
@@ -166,10 +166,36 @@ custom_css = """
166
 
167
  _js_functions = """
168
  function copyToClipboard(text) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  navigator.clipboard.writeText(text).then(() => {
170
- // console.log('Tag copied to clipboard: ' + text);
171
  const feedback = document.createElement('div');
172
- feedback.textContent = '已复制: ' + text.substring(0,30) + (text.length > 30 ? '...' : ''); // Show part of copied text
 
173
  feedback.style.position = 'fixed';
174
  feedback.style.bottom = '20px';
175
  feedback.style.left = '50%';
@@ -178,7 +204,7 @@ function copyToClipboard(text) {
178
  feedback.style.color = 'white';
179
  feedback.style.padding = '10px 20px';
180
  feedback.style.borderRadius = '5px';
181
- feedback.style.zIndex = '10000'; // Ensure it's on top
182
  feedback.style.transition = 'opacity 0.5s ease-out';
183
  document.body.appendChild(feedback);
184
  setTimeout(() => {
@@ -188,8 +214,28 @@ function copyToClipboard(text) {
188
  }, 500);
189
  }, 1500);
190
  }).catch(err => {
191
- console.error('Failed to copy tag: ', err);
192
- alert('复制失败: ' + err); // Fallback for browsers that might block it
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  });
194
  }
195
  """
@@ -249,18 +295,20 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
249
  return "<p>暂无标签</p>"
250
 
251
  html = '<div class="label-container">'
252
-
253
- if not isinstance(translations_list, list):
254
- translations_list = []
255
-
256
  tag_keys = list(tags_dict.keys())
257
-
258
- for i, tag in enumerate(tag_keys):
259
  score = tags_dict[tag]
260
- escaped_tag = tag.replace("'", "\\'") # Escape for JS
 
 
 
 
 
 
261
 
262
  html += '<div class="tag-item">'
263
- tag_display_html = f'<span class="tag-en" onclick="copyToClipboard(\'{escaped_tag}\')">{tag}</span>'
264
 
265
  if show_translation_in_list and i < len(translations_list) and translations_list[i]:
266
  tag_display_html += f'<span class="tag-zh">({translations_list[i]})</span>'
 
166
 
167
  _js_functions = """
168
  function copyToClipboard(text) {
169
+ console.log("copyToClipboard called with text:", text, "Type:", typeof text);
170
+
171
+ if (typeof text !== 'string') {
172
+ console.error('copyToClipboard Error: text is not a string. Received type:', typeof text, 'value:', text);
173
+ const feedback = document.createElement('div');
174
+ feedback.textContent = '复制错误: 无效的标签数据 (类型: ' + typeof text + ')';
175
+ feedback.style.position = 'fixed';
176
+ feedback.style.bottom = '20px';
177
+ feedback.style.left = '50%';
178
+ feedback.style.transform = 'translateX(-50%)';
179
+ feedback.style.backgroundColor = '#D32F2F';
180
+ feedback.style.color = 'white';
181
+ feedback.style.padding = '10px 20px';
182
+ feedback.style.borderRadius = '5px';
183
+ feedback.style.zIndex = '10000';
184
+ feedback.style.transition = 'opacity 0.5s ease-out';
185
+ document.body.appendChild(feedback);
186
+ setTimeout(() => {
187
+ feedback.style.opacity = '0';
188
+ setTimeout(() => {
189
+ document.body.removeChild(feedback);
190
+ }, 500);
191
+ }, 3000);
192
+ return;
193
+ }
194
+
195
  navigator.clipboard.writeText(text).then(() => {
 
196
  const feedback = document.createElement('div');
197
+ const displayCopiedText = text.length > 40 ? text.substring(0, 37) + '...' : text;
198
+ feedback.textContent = '已复制: ' + displayCopiedText;
199
  feedback.style.position = 'fixed';
200
  feedback.style.bottom = '20px';
201
  feedback.style.left = '50%';
 
204
  feedback.style.color = 'white';
205
  feedback.style.padding = '10px 20px';
206
  feedback.style.borderRadius = '5px';
207
+ feedback.style.zIndex = '10000';
208
  feedback.style.transition = 'opacity 0.5s ease-out';
209
  document.body.appendChild(feedback);
210
  setTimeout(() => {
 
214
  }, 500);
215
  }, 1500);
216
  }).catch(err => {
217
+ console.error('Failed to copy tag: ', text, err);
218
+ let errorMsg = '复制失败';
219
+ if (err.name === 'NotAllowedError') {
220
+ errorMsg = '复制失败: 未授予剪贴板权限。请检查浏览器设置。';
221
+ } else if (err.message && (err.message.includes("Clipboard write was denied") || err.message.includes("transient activation"))) {
222
+ errorMsg = '复制失败: 请先点击页面任意位置以激活,然后再试。';
223
+ } else if (err.message) {
224
+ errorMsg = `复制失败: ${err.message.substring(0, 70)}`;
225
+ }
226
+ const feedback = document.createElement('div');
227
+ feedback.textContent = errorMsg;
228
+ feedback.style.position = 'fixed';
229
+ feedback.style.bottom = '20px';
230
+ feedback.style.left = '50%';
231
+ feedback.style.transform = 'translateX(-50%)';
232
+ feedback.style.backgroundColor = '#D32F2F';
233
+ feedback.style.color = 'white';
234
+ feedback.style.padding = '10px 20px';
235
+ feedback.style.borderRadius = '5px';
236
+ feedback.style.zIndex = '10000';
237
+ document.body.appendChild(feedback);
238
+ setTimeout(() => { document.body.removeChild(feedback); }, 3000);
239
  });
240
  }
241
  """
 
295
  return "<p>暂无标签</p>"
296
 
297
  html = '<div class="label-container">'
 
 
 
 
298
  tag_keys = list(tags_dict.keys())
299
+
300
+ for i, tag in enumerate(tag_keys): # tag 应该是字符串
301
  score = tags_dict[tag]
302
+
303
+ # 改进的转义方法:
304
+ if isinstance(tag, str):
305
+ js_safe_tag_content = json.dumps(tag)[1:-1]
306
+ else:
307
+ js_safe_tag_content = "INVALID_TAG_DATA"
308
+ print(f"警告: 在format_tags_html中遇到非字符串标签: {tag} (类型: {type(tag)})")
309
 
310
  html += '<div class="tag-item">'
311
+ tag_display_html = f'<span class="tag-en" style="cursor:pointer;" onclick="copyToClipboard(\'{js_safe_tag_content}\')">{tag}</span>'
312
 
313
  if show_translation_in_list and i < len(translations_list) and translations_list[i]:
314
  tag_display_html += f'<span class="tag-zh">({translations_list[i]})</span>'