-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
151 lines (134 loc) · 5.42 KB
/
background.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
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
//
// Copyright (c) 2024 swsychen.
// All rights reserved. Licensed under the MIT license.
// See LICENSE file in the project root for details.
//
var spreadsheetId = 'YOUR_SPREADSHEET_ID'; // Replace with your actual spreadsheet ID
var range = 'bookmarks!A:C'; // Adjust based on your needs
function getAuthToken() {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({ interactive: true }, function(token) {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(token);
}
});
});
}
// Adjusted to initiate the writing process after fetching bookmarks
function fetchAndPrintBookmarks() {
let bmaks_arr = [["ID", "Name", "URL"]];
let rowId = 1;
chrome.bookmarks.getTree(function(bookmarks) {
printBookmarks(bookmarks,bmaks_arr, rowId);
console.log("Updated bookmarks array:", bmaks_arr);
// Now, get auth token and write to sheet
getAuthToken().then(token => {
writeToSheet(bmaks_arr, token);
}).catch(error => {
console.error("Failed to get auth token:", error);
});
});
}
function printBookmarks(bookmarkNodes, bmaks_arr, rowId) {
bookmarkNodes.forEach(node => {
if (node.children) {
// Pass rowId without incrementing it here because it's not a leaf node
rowId = printBookmarks(node.children, bmaks_arr, rowId);
} else {
if(node.title && node.url) { // Check both title and URL are present
console.log(rowId, node.title, node.url);
bmaks_arr.push([rowId, node.title, node.url]);
rowId++; // Increment rowId here for each bookmark processed
}
}
});
return rowId; // Return the updated rowId for subsequent operations
}
function clearSheet(spreadsheetId, range, accessToken) {
return new Promise((resolve, reject) => {
var clearUrl = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${range}:clear`;
var params = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({})
};
fetch(clearUrl, params)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok, status: ' + response.status);
}
return response.json();
})
.then(data => {
if (data.error) {
console.error("Error clearing sheet:", data.error);
reject(data.error); // Reject the promise if there's an API error
} else {
console.log("Success clearing sheet:", data);
resolve(); // Resolve the promise upon successful clearing
}
})
.catch(error => {
console.error('Error clearing sheet:', error);
reject(error); // Reject the promise on fetch errors
});
});
}
function writeToSheet(data, accessToken) {
// First, clear the sheet
clearSheet(spreadsheetId, range, accessToken).then(() => {
// After clearing, write new data
var url = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${range}:append?valueInputOption=USER_ENTERED`;
var params = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({ "values": data })
};
fetch(url, params)
.then(response => {
// Check if the response is ok (status in the range 200-299)
if (!response.ok) {
// If not, throw an error that includes the status
throw new Error('Network response was not ok, status: ' + response.status);
}
return response.json(); // Parse JSON only if response is ok
})
.then(data => {
// Check for errors in the data object, as Google APIs might return error details in the body
if (data.error) {
console.error("Error writing to sheet:", data.error);
return; // Prevent further processing
}
console.log("Success writing to sheet:", data);
})
.catch(error => {
// This catches network errors and errors thrown from the first .then() block
console.error('Error writing to sheet:', error);
});
}).catch(error => {
console.error("Failed to clear sheet before writing:", error);
});
}
// Adding event listeners for bookmark creation, removal, and changes
chrome.bookmarks.onCreated.addListener((id, bookmark) => {
console.log('Bookmark Created:', bookmark.title);
fetchAndPrintBookmarks();
});
chrome.bookmarks.onRemoved.addListener((id, removeInfo) => {
console.log('Bookmark Removed');
fetchAndPrintBookmarks();
});
chrome.bookmarks.onChanged.addListener((id, changeInfo) => {
console.log('Bookmark Changed:', changeInfo.title);
fetchAndPrintBookmarks();
});
// Initial fetch and print of bookmarks
fetchAndPrintBookmarks();