forked from Decentricity/myriad-importer-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.old3
93 lines (85 loc) · 2.83 KB
/
popup.old3
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
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() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var currentUrl = tabs[0].url;
if (currentUrl.includes('twitter.com')) {
importTwitterPost(currentUrl);
} else {
createMessage('Current URL is not a Twitter URL.', true);
}
});
});
function checkLogin() {
// ...
}
function sendMagicLink(email) {
// ...
fetch(apiUrl, {
// ...
}).then(response => response.json())
.then(data => {
console.log(data);
createMessage('Magic link has been successfully sent.', false);
})
.catch((error) => {
console.error('Error:', error);
createMessage('There was an error sending the magic link: ' + error, true);
});
}
function authenticate(magicLink) {
// ...
fetch(apiUrl, {
// ...
}).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');
checkLogin();
});
createMessage('Successfully authenticated!', false);
})
.catch((error) => {
console.error('Error:', error);
createMessage('There was an error during authentication: ' + error, true);
});
}
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;
// ...
fetch(apiUrl, {
// ...
}).then(response => response.json())
.then(data => {
console.log(data);
createMessage('Post has been successfully imported!', false);
})
.catch((error) => {
console.error('Error:', error);
createMessage('There was an error during post import: ' + error, true);
});
});
}
function createMessage(text, isError = false) {
var message = document.createElement('div');
message.className = 'message';
message.textContent = text;
message.style.backgroundColor = isError ? '#ff4d4d' : '#4dff88'; // red for error, green for success
document.body.appendChild(message);
setTimeout(function() {
document.body.removeChild(message);
}, 3000); // remove message after 3 seconds
}
checkLogin();