forked from siyuan-note/siyuan-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
166 lines (156 loc) · 4.83 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
chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({
id: 'copy-to-siyuan',
title: 'Copy to SiYuan',
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)
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': 'tip',
'msg': 'Invalid API token',
'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'
title = title.replaceAll("/", "")
const siteName = requestData.siteName
const excerpt = requestData.excerpt
const href = requestData.href
let linkText = href
if ("" !== siteName) {
linkText += " - " + siteName
}
let markdown = "---\n\n* " + "[" + linkText + "](" + href + ")\n"
if ("" !== excerpt) {
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,
'path': title,
'markdown': markdown,
}),
}).then((response) => {
return response.json()
}).then((response) => {
if (0 === response.code) {
chrome.tabs.sendMessage(requestData.tabId, {
'func': 'tip',
'msg': "Create article successfully",
'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.warn(e)
})
})
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;
}