-
Notifications
You must be signed in to change notification settings - Fork 68
/
background.js
192 lines (182 loc) · 6.9 KB
/
background.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
chrome.contextMenus.removeAll(function () {
const title = chrome.i18n.getMessage("copy_to_siyuan");
chrome.contextMenus.create({
id: 'copy-to-siyuan',
title: title,
contexts: ['selection', 'image'],
})
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === 'copy-to-siyuan') {
chrome.tabs.sendMessage(tab.id, {
'func': 'copy',
'tabId': tab.id,
'srcUrl': info.srcUrl,
})
}
})
})
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
if (request.func !== 'upload-copy') {
return
}
const jsonBlob = await fetch(request.dataURL).then(r => r.blob())
const requestData = JSON.parse(await jsonBlob.text())
const fetchFileErr = requestData.fetchFileErr
const dom = requestData.dom
const files = requestData.files
const formData = new FormData()
formData.append('dom', dom)
for (const key of Object.keys(files)) {
const data = files[key].data
const base64Response = await fetch(data)
const blob = base64Response.blob()
formData.append(key, await blob)
}
formData.append("notebook", requestData.notebook)
formData.append("parentID", requestData.parentDoc)
formData.append("parentHPath", requestData.parentHPath)
formData.append("href", requestData.href)
formData.append("tags", requestData.tags)
fetch(requestData.api + '/api/extension/copy', {
method: 'POST',
headers: {
'Authorization': 'Token ' + requestData.token,
},
body: formData,
}).then((response) => {
if (response.redirected) {
chrome.tabs.sendMessage(requestData.tabId, {
'func': 'tipKey',
'msg': 'tip_token_invalid',
'tip': 'tip',
})
}
return response.json()
}).then((response) => {
if (response.code < 0) {
chrome.tabs.sendMessage(requestData.tabId, {
'func': 'tip',
'msg': response.msg,
'tip': requestData.tip,
})
return
}
chrome.tabs.sendMessage(requestData.tabId, {
'func': 'copy2Clipboard',
'data': response.data.md,
})
if ('' !== response.msg && requestData.type !== 'article') {
chrome.tabs.sendMessage(requestData.tabId, {
'func': 'tip',
'msg': response.msg,
'tip': requestData.tip,
})
}
if (requestData.type === 'article') {
let title = requestData.title ? requestData.title : 'Untitled'
let markdown = "---\n\n* " + title
title = title.replaceAll("/", "")
const siteName = requestData.siteName
if ("" !== siteName) {
markdown += " - " + siteName
}
markdown += "\n"
const href = requestData.href
let linkText = href
try {
linkText = decodeURIComponent(linkText)
} catch (e) {
console.warn(e)
}
markdown += "* " + "[" + linkText + "](" + href + ")\n"
let excerpt = requestData.excerpt.trim()
if ("" !== excerpt) {
// 将连续的三个换行符替换为两个换行符
excerpt = excerpt.replace(/\n{3,}/g, "\n\n")
// 从第二行开始,每行前面加两个空格 https://github.com/siyuan-note/siyuan/issues/11315
excerpt = excerpt.replace(/\n/g, "\n ")
excerpt = excerpt.trim()
markdown += "* " + excerpt + "\n"
} else {
markdown += "\n"
}
markdown += "* " + getDateTime() + "\n\n---\n\n" + response.data.md
fetch(requestData.api + '/api/filetree/createDocWithMd', {
method: 'POST',
headers: {
'Authorization': 'Token ' + requestData.token,
},
body: JSON.stringify({
'notebook': requestData.notebook,
'parentID': requestData.parentDoc,
'tags': requestData.tags,
'path': requestData.parentHPath + "/" + title,
'markdown': markdown,
'withMath': response.data.withMath,
'clippingHref': requestData.href,
}),
}).then((response) => {
return response.json()
}).then((response) => {
if (0 === response.code) {
chrome.tabs.sendMessage(requestData.tabId, {
'func': 'tipKey',
'msg': "tip_clip_ok",
'tip': requestData.tip,
})
if (fetchFileErr) {
// 可能因为跨域问题导致下载图片失败,这里调用内核接口 `网络图片转换为本地图片` https://github.com/siyuan-note/siyuan/issues/7224
fetch(requestData.api + '/api/format/netImg2LocalAssets', {
method: 'POST',
headers: {
'Authorization': 'Token ' + requestData.token,
},
body: JSON.stringify({
'id': response.data,
'url': requestData.href, // 改进浏览器剪藏扩展转换本地图片成功率 https://github.com/siyuan-note/siyuan/issues/7464
}),
})
}
} else {
chrome.tabs.sendMessage(requestData.tabId, {
'func': 'tip',
'msg': response.msg,
'tip': requestData.tip,
})
}
})
}
}).catch((e) => {
console.error(e)
chrome.tabs.sendMessage(requestData.tabId, {
'func': 'tipKey',
'msg': "tip_siyuan_kernel_unavailable",
'tip': "tip",
});
})
})
function getDateTime() {
const now = new Date();
const year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();
let hour = now.getHours();
let minute = now.getMinutes();
let second = now.getSeconds();
if (month.toString().length === 1) {
month = '0' + month;
}
if (day.toString().length === 1) {
day = '0' + day;
}
if (hour.toString().length === 1) {
hour = '0' + hour;
}
if (minute.toString().length === 1) {
minute = '0' + minute;
}
if (second.toString().length === 1) {
second = '0' + second;
}
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}