-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup-Copy1.js
96 lines (81 loc) · 2.64 KB
/
popup-Copy1.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
document.getElementById('send_magic_link').addEventListener('click', function() {
var email = document.getElementById('email').value;
sendMagicLink(email);
});
document.getElementById('submit_magic_link').addEventListener('click', function() {
var magicLink = document.getElementById('magic_link').value;
authenticate(magicLink);
});
document.getElementById('import_post').addEventListener('click', function() {
var twitterUrl = document.getElementById('twitter_url').value;
importTwitterPost(twitterUrl);
});
function sendMagicLink(email) {
var apiUrl = "https://api.myriad.social/authentication/otp/email";
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"email": email,
"callbackURL": "https://app.myriad.social/login"
})
}).then(response => response.json())
.then(data => console.log(data))
.catch((error) => {
console.error('Error:', error);
});
}
function authenticate(magicLink) {
var callbackUrl = "https://app.myriad.social/login";
var token = magicLink.replace(callbackUrl+"?token=", "");
var apiUrl = "https://api.myriad.social/authentication/login/otp";
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"token": token
})
}).then(response => response.json())
.then(data => {
var accessToken = data.token.accessToken;
var username = data.user.username;
// store accessToken and username in local storage
chrome.storage.local.set({"accessToken": accessToken, "username": username}, function() {
console.log('Access Token and Username are set in local storage');
});
})
.catch((error) => {
console.error('Error:', error);
});
}
function importTwitterPost(twitterUrl, selectedTimelineIds = []) {
// retrieve access token and username from local storage
chrome.storage.local.get(["accessToken", "username"], function(items) {
var at = items.accessToken;
var un = items.username;
var apiUrl = "https://api.myriad.social/user/posts/import";
var headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + at,
};
var data = {
"url": twitterUrl,
"importer": un,
"selectedTimelineIds": selectedTimelineIds,
};
fetch(apiUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify(data)
}).then(response => response.json())
.then(data => console.log(data))
.catch((error) => {
console.error('Error:', error);
});
});
}