-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
112 lines (100 loc) · 3.4 KB
/
popup.js
File metadata and controls
112 lines (100 loc) · 3.4 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
document.addEventListener("DOMContentLoaded", function () {
var saveButton = document.getElementById("save");
saveButton.addEventListener("click", saveCredentials);
});
function saveCredentials() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
chrome.storage.sync.set({ username: username, password: password }, function () {
console.log("Credentials saved successfully.");
saveFile(username, password);
});
}
function saveFile(username, password) {
chrome.identity.getAuthToken({ interactive: true }, function (token) {
var headers = new Headers({
Authorization: "Bearer " + token,
});
// Checking if the file already exists
fetch(
"https://www.googleapis.com/drive/v3/files?q=name%20%3D%20'passwords.txt'%20and%20'root'%20in%20parents",
{
method: "GET",
headers: headers,
}
)
.then(function (response) {
return response.json();
})
.then(function (data) {
if (data.files.length > 0) {
// File already exists, update the content
updateFileContent(data.files[0].id, username, password, headers);
} else {
// File doesn't exist, create a new one
createNewFile(username, password, headers);
}
})
.catch(function (error) {
console.error("Error checking file existence: ", error);
});
});
}
function updateFileContent(fileId, username, password, headers) {
// Fetch the existing content of the file
fetch(`https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`, {
method: "GET",
headers: headers,
})
.then(function (response) {
return response.text();
})
.then(function (content) {
// Append the new credential to the existing content
var fileContent = content + "\n" + username + ":" + password;
// Update the file with the new content
fetch(`https://www.googleapis.com/upload/drive/v3/files/${fileId}?uploadType=media`, {
method: "PATCH",
headers: headers,
body: fileContent,
})
.then(function (response) {
console.log("File content updated successfully.");
})
.catch(function (error) {
console.error("Error updating file content: ", error);
});
})
.catch(function (error) {
console.error("Error fetching file content: ", error);
});
}
function createNewFile(username, password, headers) {
var metadata = {
name: "passwords.txt",
mimeType: "text/plain",
parents: ["root"],
};
var fileContent = "";
chrome.identity.getProfileUserInfo(function (userInfo) {
var userEmail = userInfo.email;
fileContent = userEmail + ":" + password;
var form = new FormData();
form.append(
"metadata",
new Blob([JSON.stringify(metadata)], { type: "application/json" })
);
form.append("file", new Blob([fileContent], { type: "text/plain" }));
fetch("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", {
method: "POST",
headers: headers,
body: form,
})
.then(function (response) {
console.log("File saved successfully.");
})
.catch(function (error) {
console.error("Error saving file: ", error);
});
});
}