-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
180 lines (153 loc) · 6.08 KB
/
background.js
File metadata and controls
180 lines (153 loc) · 6.08 KB
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
const b64EncodeUnicode = (str) =>
btoa(encodeURIComponent(str)
.replace(/%([0-9A-F]{2})/g,
(match, p1) => String.fromCharCode(`0x${p1}`)
));
browser.action.onClicked.addListener(async (tab) => {
browser.action.setPopup({
popup: browser.runtime.getURL('popup.html')
});
browser.action.openPopup();
browser.storage.local.clear();
const hadRemarkablePermissionPromise = browser.permissions.contains({origins: ['https://webapp-prod.cloud.remarkable.engineering/*']});
const hasRemarkablePermission = await browser.permissions.request({origins: ['https://webapp-prod.cloud.remarkable.engineering/*']});
const {deviceToken} = await browser.storage.sync.get();
if (!deviceToken) {
browser.tabs.create({
url: browser.runtime.getURL('options.html')
});
}
const hadRemarkablePermission = await hadRemarkablePermissionPromise;
if (!hasRemarkablePermission || !deviceToken || !hadRemarkablePermission) {
await browser.runtime.sendMessage({type: 'close-popup'}).catch(async () => {
await browser.action.setPopup({
popup: null
});
});
}
if (hasRemarkablePermission && deviceToken && hadRemarkablePermission) {
browser.action.setIcon({
path: {
16: 'icons/16-loading.png',
48: 'icons/48-loading.png',
128: 'icons/128-loading.png'
}
});
browser.action.setTitle({
title: 'Sending site to reMarkable'
});
await browser.scripting.executeScript({
func: async () => await import(browser.runtime.getURL('build/bundle.js')).then((module) => {
module.default()
}),
target: {tabId: tab.id},
});
}
});
browser.runtime.onMessage.addListener((message) => {
if (message.type === 'cors-help') {
return new Promise(async (resolve, reject) => {
const hasPermission = await browser.permissions.contains({origins: [message.url]});
if (!hasPermission) {
let status = 'INIT';
await browser.storage.local.set({
[message.url]: status
});
while (status === 'INIT') {
await new Promise(resolve =>
setTimeout(resolve, 100)
).then(async () => {
const statusObj = await browser.storage.local.get(message.url);
status = statusObj[message.url];
});
}
if (status !== 'GRANTED') {
reject('Granting cors permission failed');
return 'Granting cors permission failed';
}
}
fetch(message.url).then(async (response) => {
const content = await response.arrayBuffer();
const mimeHeader = response.headers.get('Content-Type');
resolve({
content,
mimeHeader
});
}).catch(reject);
});
}
if (message.type === 'upload') {
return new Promise(async (resolve) => {
const upload = async (userToken, name, body) => await fetch("https://internal.cloud.remarkable.com/doc/v2/files", {
"headers": {
"accept": "*/*",
"accept-language": "en-CA,en;q=0.9,no;q=0.8,sv;q=0.7,da;q=0.6",
"authorization": `Bearer ${userToken}`,
"content-type": "application/epub+zip",
"rm-meta": name,
"rm-source": "RoR-Browser"
},
"body": body,
"method": "POST"
}).then(response => response.status).catch(error => error.status);
const getUserToken = async (deviceToken) => {
const userTokenReq = await fetch("https://webapp-prod.cloud.remarkable.engineering/token/json/2/user/new", {
"headers": {
"accept": "*/*",
"authorization": `Bearer ${deviceToken}`,
},
"method": "POST",
});
userToken = await userTokenReq.text();
await browser.storage.sync.set({
deviceToken,
userToken
});
return userToken;
}
let {userToken, deviceToken} = await browser.storage.sync.get();
if (!userToken) {
userToken = await getUserToken(deviceToken);
}
const name = b64EncodeUnicode(JSON.stringify({
file_name: message.name
}));
let status = await upload(userToken, name, message.blob);
if (status === 401) {
userToken = await getUserToken(deviceToken);
status = await upload(userToken, name, message.blob);
}
browser.action.setIcon({
path: {
16: 'icons/16-saved.png',
48: 'icons/48-saved.png',
128: 'icons/128-saved.png'
}
});
browser.action.setTitle({
title: 'Site sent to reMarkable'
});
setTimeout(() => {
browser.action.setIcon({});
browser.action.setTitle({title: null});
}, 10000);
resolve(status);
});
}
if (message.type === 'clean-up') {
return new Promise(async (resolve) => {
await Promise.all([
browser.runtime.sendMessage({type: 'close-popup'}).catch(() => {
browser.action.setPopup({
popup: null
});
}),
browser.storage.local.clear()
]);
resolve();
})
}
if (!['cors-help', 'upload', 'clean-up'].includes(message.type)) {
throw new Error(`Unknown message type ${message.type}`);
}
});