This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
51 lines (45 loc) · 1.57 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
function postData(url, data) {
return fetch(url, {
body: JSON.stringify(data),
headers: {
'accept': 'application/json',
'content-type': 'application/json',
'user-agent': 'Zepto Chrome Extension',
},
method: 'POST',
mode: 'cors',
})
.then(response => response.json());
}
function init() {
const longurl = document.getElementById('longurl');
const shortlink = document.getElementById('shortlink');
shortlink.innerHTML = 'Shortening...';
chrome.tabs.query({ 'active': true, 'lastFocusedWindow': true }, function (tabs) {
if (!tabs.length) {
return;
}
var url = tabs[0].url;
longurl.value = url;
longurl.blur();
chrome.storage.sync.get({
api_key: '',
}, function (items) {
if (!items.api_key) {
shortlink.innerHTML = 'Error: Zepto API Key required (see options)';
return;
}
postData('https://zepl.ink/api/links?api_token=' + items.api_key, { url: url })
.then(function (data) {
shortlink.innerHTML = data.data.shorturl;
shortlink.focus();
window.getSelection().selectAllChildren(shortlink);
})
.catch(function (error) {
shortlink.innerHTML = 'Error';
console.error(error);
});
});
});
}
document.addEventListener('DOMContentLoaded', init);