/
popup.js
181 lines (170 loc) · 5.68 KB
/
popup.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
// const { content, title, filename } = await singlefile.getPageData({
// removeHiddenElements: true,
// removeUnusedStyles: true,
// removeUnusedFonts: true,
// removeImports: true,
// blockScripts: true,
// blockAudios: true,
// blockVideos: true,
// compressHTML: true,
// removeAlternativeFonts: true,
// removeAlternativeMedias: true,
// removeAlternativeImages: true,
// groupDuplicateImages: true
// });
// document.addEventListener('DOMContentLoaded', async () => {
// const response = await fetch('http://127.0.0.1:8012/data');
// const data = await response.json();
// console.log(data);
// });
document.addEventListener('DOMContentLoaded', () => {
const ipElement = document.getElementById('ip')
const tokenElement = document.getElementById('token')
const showTipElement = document.getElementById('showTip')
const notebooksElement = document.getElementById('notebooks')
ipElement.addEventListener('change', () => {
chrome.storage.sync.set({
ip: ipElement.value,
})
})
tokenElement.addEventListener('change', () => {
chrome.storage.sync.set({
token: tokenElement.value,
})
getNotebooks(ipElement, tokenElement, notebooksElement)
})
showTipElement.addEventListener('change', () => {
chrome.storage.sync.set({
showTip: showTipElement.checked,
})
})
notebooksElement.addEventListener('change', () => {
notebooksElement.setAttribute("data-id", notebooksElement.value)
chrome.storage.sync.set({
notebook: notebooksElement.value,
})
})
const sendElement = document.getElementById('send')
sendElement.addEventListener('click', () => {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
chrome.scripting.executeScript({
target: {tabId: tabs[0].id},
func: siyuanGetReadability,
args: [tabs[0].id],
}, function () {
window.close();
})
});
})
chrome.storage.sync.get({
ip: 'http://127.0.0.1:6806',
showTip: true,
token: '',
notebook: '',
}, function (items) {
ipElement.value = items.ip || 'http://127.0.0.1:6806'
tokenElement.value = items.token || ''
showTipElement.checked = items.showTip
notebooksElement.setAttribute("data-id", items.notebook)
getNotebooks(ipElement, tokenElement, notebooksElement)
})
})
const getNotebooks = (ipElement, tokenElement, notebooksElement) => {
fetch(ipElement.value + '/api/notebook/lsNotebooks', {
method: 'POST',
redirect: "manual",
headers: {
'Authorization': 'Token ' + tokenElement.value,
},
body: JSON.stringify({"flashcard": false})
}).then((response) => {
if (response.status !== 200) {
document.getElementById('log').innerHTML = "Authentication failed"
} else {
document.getElementById('log').innerHTML = ""
}
return response.json()
}).then((response) => {
if (response.code === 0) {
if (!response.data.notebooks) {
document.getElementById('log').innerHTML = "Please upgrade SiYuan to v1.3.5 or above"
return
}
if (response.data.notebooks.length > 0) {
let optionsHTML = ''
response.data.notebooks.forEach(notebook => {
if (notebook.closed) {
return
}
optionsHTML += `<option value="${notebook.id}">${escapeHtml(notebook.name)}</option>`
})
notebooksElement.innerHTML = optionsHTML
notebooksElement.value = notebooksElement.getAttribute("data-id")
chrome.storage.sync.set({
notebook: notebooksElement.value,
})
} else {
document.getElementById('log').innerHTML = "Please create a notebook before"
}
} else {
document.getElementById('log').innerHTML = "Get notebooks failed"
}
})
}
const escapeHtml = (unsafe) => {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
const siyuanGetReadability = async (tabId) => {
try {
console.log("siyuanGetReadability");
siyuanShowTip('Clipping, please wait a moment...', 3 * 1000)
} catch (e) {
alert("After installing the SiYuan extension for the first time, please refresh the page before using it")
window.location.reload()
return
}
const { content, title, filename } = await singlefile.getPageData({
removeHiddenElements: true,
removeUnusedStyles: true,
removeUnusedFonts: true,
removeImports: true,
blockScripts: true,
blockAudios: true,
blockVideos: true,
compressHTML: true,
removeAlternativeFonts: true,
removeAlternativeMedias: true,
removeAlternativeImages: true,
groupDuplicateImages: true
});
// console.log(content);
// console.log(title);
// console.log(filename);
// window.scrollTo(0, document.body.scrollHeight);
scrollTo1(() => {
toggle = false
clearInterval(scrollTimer)
// window.scrollTo(0, 0);
try {
// 浏览器剪藏扩展剪藏某些网页代码块丢失注释 https://github.com/siyuan-note/siyuan/issues/5676
document.querySelectorAll(".hljs-comment").forEach(item => {
item.classList.remove("hljs-comment")
item.classList.add("hljs-cmt")
})
const article = new Readability(document.cloneNode(true), {keepClasses: true,}).parse()
const tempElement = document.createElement('div')
tempElement.innerHTML = article.content
// console.log(article)
siyuanSendUpload(content, tempElement, tabId, undefined, "article", article, window.location.href)
siyuanClearTip()
} catch (e) {
console.error(e)
siyuanShowTip(e.message, 7 * 1000)
}
})
}