-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathcontent.js
707 lines (628 loc) · 26.5 KB
/
content.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
document.addEventListener('DOMContentLoaded', function () {
chrome.runtime.onMessage.addListener(
async (request, sender, sendResponse) => {
if ('tip' === request.func && request.tip) {
siyuanShowTip(request.msg, request.timeout)
return
}
if ('tipKey' === request.func && request.tip) {
siyuanShowTipByKey(request.msg, request.timeout)
return
}
if ('copy2Clipboard' === request.func) {
await copyToClipboard(request.data)
return
}
if ('reload' === request.func) {
window.location.reload()
return
}
if ('copy' !== request.func) {
return
}
siyuanShowTipByKey("tip_clipping")
const selection = window.getSelection()
if (selection && 0 < selection.rangeCount) {
const range = selection.getRangeAt(0)
const tempElement = document.createElement('div')
tempElement.appendChild(range.cloneContents())
siyuanSendUpload(tempElement, request.tabId, request.srcUrl, "part")
}
})
const copyToClipboard = async (textToCopy) => {
// 修复无焦点的未捕获异常:https://github.com/siyuan-note/siyuan/issues/13208
await new Promise(resolve => requestAnimationFrame(resolve));
if (navigator.clipboard && window.isSecureContext) {
try {
return await navigator.clipboard.writeText(textToCopy);
} catch (error) {
//console.warn('Failed to copy text: ', error);
}
}
let textArea = document.createElement('textarea')
textArea.value = textToCopy
textArea.style.position = 'fixed'
textArea.style.left = '-999999px'
textArea.style.top = '-999999px'
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
return new Promise((res, rej) => {
document.execCommand('copy') ? res() : rej()
textArea.remove()
})
}
})
let tipTimeoutId
const siyuanShowTip = (msg, timeout) => {
let messageElement = document.getElementById('siyuanmessage')
if (!messageElement) {
document.body.insertAdjacentHTML('afterend', `<div style=" position:fixed;top: 0;z-index: 999999999;transform: translate3d(0, -100px, 0);opacity: 0;transition: opacity 0.15s cubic-bezier(0, 0, 0.2, 1) 0ms, transform 0.15s cubic-bezier(0, 0, 0.2, 1) 0ms;width: 100%;align-items: center;justify-content: center;height: 0;display: flex;" id="siyuanmessage">
<div style="line-height: 20px;border-radius: 4px;padding: 8px 16px;color: #fff;font-size: inherit;background-color: #4285f4;box-sizing: border-box;box-shadow: 0 3px 5px -1px rgba(0, 0, 0, 0.2), 0 6px 10px 0 rgba(0, 0, 0, 0.14), 0 1px 18px 0 rgba(0, 0, 0, 0.12);transition: transform 0.15s cubic-bezier(0, 0, 0.2, 1) 0ms;transform: scale(0.8);top: 16px;position: absolute;word-break: break-word;max-width: 80vw;"></div></div>`)
messageElement = document.getElementById('siyuanmessage')
}
messageElement.style.transform = 'translate3d(0, 0, 0)'
messageElement.style.opacity = '1'
messageElement.firstElementChild.innerHTML = msg
if (!timeout) {
timeout = 5000
}
if (tipTimeoutId) {
clearTimeout(tipTimeoutId);
}
tipTimeoutId = setTimeout(() => {
siyuanClearTip();
}, timeout);
}
// Add i18n support https://github.com/siyuan-note/siyuan/issues/13559
const siyuanShowTipByKey = (msgKey, timeout) => {
siyuanShowTip(chrome.i18n.getMessage(msgKey), timeout);
}
const siyuanClearTip = () => {
let messageElement = document.getElementById('siyuanmessage')
if (!messageElement) {
return
}
messageElement.style.transform = 'translate3d(0, -100px, 0)'
messageElement.style.opacity = '0'
}
const siyuanConvertBlobToBase64 = (blob) => new Promise((resolve, reject) => {
const reader = new FileReader
reader.onerror = reject
reader.onload = () => resolve(reader.result)
reader.readAsDataURL(blob)
})
// 网页换行用 span 样式 word-break 的特殊处理 https://github.com/siyuan-note/siyuan/issues/13195
// 递归查找父元素直到找到 pre、code、span、math 或 math相关标签
function isIgnoredElement(element) {
// 递归查找父元素直到找到 pre、code、span、math 或 math相关标签
while (element) {
let tagName = element.tagName.toLowerCase();
const className = element.className.toLowerCase();
if (tagName === 'math' ||
className.includes('math') || className.includes('mathjax') || className.includes('latex') ||
className.includes('katex') || className.includes('mjx') || className.includes('mathml') ||
className.includes('equation') || className.includes('formula')) {
return true;
}
element = element.parentElement; // 移动到父元素
if (!element) {
break;
}
tagName = element.tagName.toLowerCase();
// 如果父元素是 pre、code、span、math 或与数学相关的类名
if (tagName === 'pre' || tagName === 'code' || tagName === 'span' || tagName === 'section') {
return true;
} else if (tagName === 'div' || tagName === 'p') {
return false; // 找到 div、p 直接返回
}
}
return false; // 没找到时返回 false
}
// 处理会换行的 span 后添加 <br>,让内核能识别到换行
function siyuanSpansAddBr(tempElement) {
const spans = tempElement.querySelectorAll('span');
if (!spans || spans.length === 0) {
console.log('No span elements found.');
return;
}
// 用于存储符合条件的 span 元素
const matchedSpans = [];
spans.forEach((span) => {
const style = window.getComputedStyle(span);
// 现有的条件判断,判断是否满足换行条件
if (
(style.whiteSpace.trim().toLowerCase() === 'normal' || style.whiteSpace.trim().toLowerCase() === 'pre-wrap') &&
(style.wordWrap.trim().toLowerCase() === 'break-word' || style.overflowWrap.trim().toLowerCase() === 'break-word' || style.wordBreak.trim().toLowerCase() === 'break-word')
) {
// 检查父元素是否是 pre、code 或 span
if (isIgnoredElement(span)) {
console.log('Skipping span due to parent being pre, code or span.');
return; // 如果父元素是 pre、code 或 span 或者数学公式,跳过该 span
}
const br = document.createElement('br'); // 修正为从 document 创建元素
br.setAttribute('data-added-by-siyuan', 'true');
span.parentNode.insertBefore(br, span.nextSibling);
// 添加到符合条件的数组中
matchedSpans.push(span);
}
});
if (matchedSpans.length > 0) {
console.log(`Added <br> for ${matchedSpans.length} span elements.(Total span: ${spans.length})`);
console.log('Matched span elements:', matchedSpans);
} else {
console.log('No span elements matched the criteria.');
}
};
// 网页换行用 span 样式 word-break 的特殊处理 https://github.com/siyuan-note/siyuan/issues/13195
// 移除由 span_add_br 添加的 <br>,还原原有样式
function siyuanSpansDelBr(tempElement) {
const brs = tempElement.querySelectorAll('br[data-added-by-siyuan="true"]');
if (!brs || brs.length === 0) {
return;
}
brs.forEach((br) => br.parentNode.removeChild(br));
console.log(`siyuanSpansDelBr Removed ${brs.length} <br> elements.`);
};
// 替换粗体样式为内核可识别<b>标签 https://github.com/siyuan-note/siyuan/issues/13306
function siyuanProcessBoldStyle(tempElement) {
// 获取所有应用了 font-weight: bold 的元素
const boldElements = tempElement.querySelectorAll('*');
boldElements.forEach(element => {
const style = window.getComputedStyle(element);
if (element.tagName === 'B' || element.tagName === 'STRONG') {
return; // 如果元素本身是 <b> 或 <strong> 标签,跳过
}
if (parentContainsBold(element)) {
return; // 如果元素的父元素是 <b> 或 <strong> 标签,跳过
}
// 判断是否具有 font-weight: bold
if (style.fontWeight === 'bold' || style.fontWeight === '700') { // '700' 是 bold 的常见数值
// 将 element 中的各个元素使用 <b> 标签包裹
const children = element.childNodes;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.nodeType === Node.TEXT_NODE) {
// 如果是文本节点,直接包裹在 <b> 标签中
const text = child.nodeValue;
const textElement = document.createElement('b');
textElement.setAttribute('b-added-by-siyuan', 'true');
textElement.textContent = text;
element.replaceChild(textElement, child);
} else if (child.nodeType === Node.ELEMENT_NODE) {
// 如果是元素节点,递归处理
const childElement = child;
const childTagName = childElement.tagName.toLowerCase();
if (childTagName === 'b' || childTagName === 'strong') {
continue; // 如果是 <b> 或 <strong> 标签,跳过
}
if (parentContainsBold(childElement)) {
continue; // 如果元素的父元素是 <b> 或 <strong> 标签,跳过
}
// 递归处理
siyuanProcessBoldStyle(childElement);
}
}
}
});
}
function parentContainsBold(element) {
let parent = element.parentElement;
while (parent) {
if (parent.tagName === 'B' || parent.tagName === 'STRONG' ||
parent.tagName === 'H1' || parent.tagName === 'H2' || parent.tagName === 'H3' || parent.tagName === 'H4' || parent.tagName === 'H5' || parent.tagName === 'H6') {
return true;
}
parent = parent.parentElement;
}
return false;
}
function revertBoldStyles(tempElement) {
// 获取所有带有 b-added-by-siyuan="true" 的 <b> 标签
const elements = tempElement.querySelectorAll('b[b-added-by-siyuan="true"]');
elements.forEach(element => {
// 将包裹的 <b> 标签移除,恢复原本的元素
const parent = element.parentNode;
parent.replaceChild(document.createTextNode(element.textContent), element);
});
console.log(`revertBoldStyles reverted ${elements.length} <br> elements.`);
}
// 替换斜体样式为内核可识别 <i> 标签 https://github.com/siyuan-note/siyuan/issues/13306
function siyuanProcessItalicStyle(tempElement) {
// 获取所有元素
const allElements = tempElement.querySelectorAll('*');
allElements.forEach(element => {
const style = window.getComputedStyle(element);
if (element.tagName === 'I' || element.tagName === 'EM') {
return; // 如果元素本身是 <i> 或 <em> 标签,跳过
}
if (parentContainsItalic(element)) {
return; // 如果元素的父元素是 I 或 EM 标签,跳过
}
// 判断是否具有 font-style: italic
if (style.fontStyle === 'italic') {
// 将 element 中的各个元素使用 <i> 标签包裹
const children = element.childNodes;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.nodeType === Node.TEXT_NODE) {
// 如果是文本节点,直接包裹在 <i> 标签中
const text = child.nodeValue;
const textElement = document.createElement('i');
textElement.setAttribute('i-added-by-siyuan', 'true');
textElement.textContent = text;
element.replaceChild(textElement, child);
} else if (child.nodeType === Node.ELEMENT_NODE) {
// 如果是元素节点,递归处理
const childElement = child;
const childTagName = childElement.tagName.toLowerCase();
if (childTagName === 'i' || childTagName === 'em') {
continue; // 如果是 <i> 或 <em> 标签,跳过
}
if (parentContainsItalic(childElement)) {
continue; // 如果元素的父元素是 I 或 EM 标签,跳过
}
// 递归处理
siyuanProcessItalicStyle(childElement);
}
}
}
});
}
function revertItalicStyles(tempElement) {
// 获取所有带有 b-added-by-siyuan="true" 的 <b> 标签
const elements = tempElement.querySelectorAll('i[i-added-by-siyuan="true"]');
elements.forEach(element => {
// 将包裹的 <i> 标签移除,恢复原本的元素
const parent = element.parentNode;
parent.replaceChild(document.createTextNode(element.textContent), element);
});
console.log(`revertItalicStyles reverted ${elements.length} <br> elements.`);
}
function parentContainsItalic(element) {
let parent = element.parentElement;
while (parent) {
if (parent.tagName === 'I' || parent.tagName === 'EM') {
return true;
}
parent = parent.parentElement;
}
return false;
}
function simplifyNestedTags(root, tagName) {
let elements = root.querySelectorAll(tagName);
let hasNested = true;
while (hasNested) {
hasNested = false;
elements.forEach(element => {
if (simplifyElement(element, tagName)) {
hasNested = true;
}
});
elements = root.querySelectorAll(tagName);
}
function simplifyElement(element, tagName) {
let nestedFound = false;
if (element.hasChildNodes()) {
element.childNodes.forEach(child => {
if (child.nodeType === Node.ELEMENT_NODE) {
if (child.tagName === tagName) {
nestedFound = true;
while (child.firstChild) {
element.insertBefore(child.firstChild, child);
}
child.remove();
} else {
nestedFound = nestedFound || simplifyElement(child, tagName);
}
}
});
}
return nestedFound;
}
}
// 移除图片链接 https://github.com/siyuan-note/siyuan/issues/13941
function siyuanRemoveImgLink(tempElement) {
const images = tempElement.querySelectorAll('img');
images.forEach(image => {
const parent = image.parentElement;
if (!parent) {
return;
}
if (parent.tagName === 'A') {
const grandParent = parent.parentElement;
if (!grandParent) {
return;
}
grandParent.insertBefore(image, parent);
parent.remove();
}
});
}
function adaptMSN(tempDoc) {
if (tempDoc.documentURI.indexOf("msn.cn") !== -1) {
// 删除掉其他不相关文章
const articles = document.querySelectorAll(".consumption-page-gridarea_content");
articles.forEach(article => {
const shadowHost = article.querySelector("views-header-wc");
if (!shadowHost) {
return;
}
if (!shadowHost.shadowRoot) {
return;
}
const titleEle = shadowHost.shadowRoot.querySelector("h1");
if (!titleEle) {
return;
}
if (titleEle.innerText.indexOf(tempDoc.title) === -1) {
article.remove();
}
});
// 将 Shadow DOM 展开
const shadowHosts = document.querySelectorAll('cp-article');
shadowHosts.forEach(element => {
const shadowRoot = element.shadowRoot;
if (!shadowRoot) {
return;
}
const slots = shadowRoot.querySelectorAll('slot');
slots.forEach(slot => {
const slotName = slot.getAttribute('name');
element.querySelectorAll(`[slot="${slotName}"]`).forEach(slotElement => {
const imgEle = slotElement.querySelector("cp-article-image");
if (!imgEle) {
return;
}
const imgShadowRoot = imgEle.shadowRoot;
if (!imgShadowRoot) {
return;
}
const imgs = imgShadowRoot.querySelectorAll('img');
if (!imgs || imgs.length === 0) {
return;
}
slotElement.innerHTML = ""
imgs.forEach(img => {
slotElement.appendChild(img.cloneNode(true));
});
slot.innerHTML = slotElement.innerHTML;
});
});
const shadowContent = shadowRoot.innerHTML;
const newDiv = document.createElement('div');
newDiv.innerHTML = shadowContent;
element.parentNode.replaceChild(newDiv, element);
});
}
}
// 重构并合并 Readability 前处理 https://github.com/siyuan-note/siyuan/issues/13306
async function siyuanGetCloneNode(tempDoc) {
let items;
try {
items = await new Promise((resolve, reject) => {
chrome.storage.sync.get({
expSpan: false,
expBold: false,
expItalic: false,
expRemoveImgLink: false,
}, (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(result);
}
});
});
} catch (error) {
console.error("获取失败,错误信息:", error);
items = {
expSpan: false,
expBold: false,
expItalic: false,
expRemoveImgLink: false,
};
}
// 适配 MSN 页面 https://github.com/siyuan-note/siyuan/issues/14197
adaptMSN(tempDoc);
// 前处理,增加可识别样式
if (items.expBold) {
// 替换粗体样式为内核可识别 <b> 标签 https://github.com/siyuan-note/siyuan/issues/13306
siyuanProcessBoldStyle(tempDoc);
}
if (items.expItalic) {
// 替换斜体样式为内核可识别 <i> 标签 https://github.com/siyuan-note/siyuan/issues/13306
siyuanProcessItalicStyle(tempDoc);
}
if (items.expRemoveImgLink) {
// 移除图片链接 https://github.com/siyuan-note/siyuan/issues/13941
siyuanRemoveImgLink(tempDoc);
}
if (items.expSpan) {
// 网页换行用 span 样式 word-break 的特殊处理 https://github.com/siyuan-note/siyuan/issues/13195
// 处理会换行的 span 后添加 <br>,让内核能识别到换行
siyuanSpansAddBr(tempDoc);
}
// 合并嵌套的标签
simplifyNestedTags(tempDoc, 'STRONG');
simplifyNestedTags(tempDoc, 'B');
simplifyNestedTags(tempDoc, 'I');
simplifyNestedTags(tempDoc, 'EM');
// 如果公式被嵌套包裹,则去掉外层包裹 https://github.com/siyuan-note/siyuan/issues/14382
const mathElements = tempDoc.querySelectorAll('.ztext-math');
mathElements.forEach(mathElement => {
if (mathElement.parentElement.tagName === 'B' || mathElement.parentElement.tagName === 'STRONG' || mathElement.parentElement.tagName === 'I' || mathElement.parentElement.tagName === 'EM') {
const parent = mathElement.parentElement;
while (parent.firstChild) {
parent.parentNode.insertBefore(parent.firstChild, parent);
}
parent.remove();
}
});
// 如果行级标签包含了块级标签,则将该行级标签改为 div
const inlineTags = ['span', 'strong', 'b', 'i', 'em', 'a'];
const blockTags = ['div', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'table', 'tr', 'td', 'th', 'blockquote', 'pre', 'code', 'section'];
inlineTags.forEach(inlineTag => {
const elements = document.querySelectorAll(inlineTag);
elements.forEach(element => {
let containsBlock = false;
blockTags.forEach(blockTag => {
if (element.querySelector(blockTag)) {
containsBlock = true;
}
});
if (containsBlock) {
const div = document.createElement('div');
while (element.firstChild) {
div.appendChild(element.firstChild);
}
element.parentNode.replaceChild(div, element);
}
});
});
const clonedDoc = document.cloneNode(true);
// 后处理,还原样式
if (items.expBold) {
// 还原粗体样式
revertBoldStyles(tempDoc);
}
if (items.expItalic) {
// 还原斜体样式
revertItalicStyles(tempDoc);
}
if (items.expSpan) {
// 网页换行用 span 样式 word-break 的特殊处理 https://github.com/siyuan-note/siyuan/issues/13195
// 处理会换行的 span 后添加 <br>,让内核能识别到换行
siyuanSpansDelBr(tempDoc);
}
return clonedDoc;
}
const siyuanSendUpload = async (tempElement, tabId, srcUrl, type, article, href) => {
chrome.storage.sync.get({
ip: 'http://127.0.0.1:6806',
showTip: true,
token: '',
notebook: '',
parentDoc: '',
parentHPath: '',
tags: '',
assets: true,
expSpan: false,
expBold: false,
expItalic: false,
expRemoveImgLink: false,
expListDocTree: false,
}, async function (items) {
if (!items.token) {
siyuanShowTipByKey("tip_token_miss")
return
}
if (!items.notebook) {
siyuanShowTipByKey("tip_save_path_miss")
return
}
let srcList = []
if (srcUrl) {
srcList.push(srcUrl)
}
const images = tempElement.querySelectorAll('img')
images.forEach(item => {
let src = item.getAttribute('src')
if (!src) {
return
}
if (item.className.includes("emoji") && "" !== item.getAttribute("alt")) {
// 图片 Emoji 直接使用 alt https://github.com/siyuan-note/siyuan/issues/13342
return
}
// 处理使用 data-original 属性的情况 https://github.com/siyuan-note/siyuan/issues/11826
let dataOriginal = item.getAttribute('data-original')
if (dataOriginal) {
if (!src || !src.endsWith('.gif')) {
src = dataOriginal
}
}
if ('https:' === window.location.protocol) {
if (src.startsWith('http:')) {
src = src.replace('http:', 'https:')
} else if (src.startsWith('//')) {
src = 'https:' + src
}
item.setAttribute('src', src)
}
if (-1 < item.className.indexOf("ztext-gif") && -1 < src.indexOf("zhimg.com")) {
// 处理知乎动图
src = src.replace(".jpg", ".webp")
}
srcList.push(src)
})
const files = {}
srcList = [...new Set(srcList)]
if (!items.assets) { // 不剪藏资源文件 https://github.com/siyuan-note/siyuan/issues/12583
srcList = []
}
let fetchFileErr = false;
for (let i = 0; i < srcList.length; i++) {
let src = srcList[i]
siyuanShowTip(chrome.i18n.getMessage("tip_clip_img") + ' [' + i + '/' + srcList.length + ']...');
let response;
try {
// Wikipedia 使用图片原图 https://github.com/siyuan-note/siyuan/issues/11640
if (-1 !== src.indexOf('wikipedia/commons/thumb/')) {
let idx = src.lastIndexOf('.')
let ext = src.substring(idx)
if (0 < src.indexOf('.svg.png')) {
ext = '.svg'
}
idx = src.indexOf(ext + '/')
if (0 < idx) {
src = src.substring(0, idx + ext.length)
src = src.replace('/commons/thumb/', '/commons/')
}
}
response = await fetch(src, {
"headers": {
"accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
"sec-fetch-dest": "image",
},
});
} catch (e) {
console.warn("fetch [" + src + "] failed", e)
fetchFileErr = true;
continue
}
const image = await response.blob()
files[escape(src)] = {
type: image.type,
data: await siyuanConvertBlobToBase64(image),
}
}
let title = article && article.title ? article.title : "";
let siteName = article && article.siteName ? article.siteName : "";
let excerpt = article && article.excerpt ? article.excerpt : "";
const msgJSON = {
fetchFileErr,
files: files,
dom: tempElement.innerHTML,
api: items.ip,
token: items.token,
notebook: items.notebook,
parentDoc: items.parentDoc,
parentHPath: items.parentHPath.substring(items.parentHPath.indexOf('/')),
tags: items.tags,
assets: items.assets,
tip: items.showTip,
title: title,
siteName: siteName,
excerpt: excerpt,
listDocTree: items.expListDocTree,
href,
type,
tabId,
};
const jsonStr = JSON.stringify(msgJSON);
const jsonBlob = new Blob([jsonStr], {type: "application/json"});
const dataURL = URL.createObjectURL(jsonBlob);
chrome.runtime.sendMessage({func: 'upload-copy', dataURL: dataURL})
})
}