-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
123 lines (108 loc) · 3.21 KB
/
Copy pathbackground.js
File metadata and controls
123 lines (108 loc) · 3.21 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
113
114
115
116
117
118
119
120
121
122
123
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "appendToSheet") {
handleAppend(message.payload, sendResponse);
return true; // Keep message channel open for async response
}
});
async function handleAppend(data, sendResponse) {
try {
const token = await getAuthToken();
const result = await appendToGoogleSheet(token, data);
sendResponse({ success: true, result });
} catch (err) {
console.error("Append error:", err);
sendResponse({ success: false, error: err.message || err.toString() });
}
}
function getAuthToken() {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({ interactive: true }, (token) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(token);
}
});
});
}
async function appendToGoogleSheet(token, data) {
const config = await loadAppendConfig(data);
const row = config.fields.map(
(field) => config.valuesByFieldId[field.id] || "",
);
const encodedRange = encodeURIComponent(`${config.tabName}!A:A`);
const url = `https://sheets.googleapis.com/v4/spreadsheets/${config.sheetId}/values/${encodedRange}:append?valueInputOption=USER_ENTERED`;
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
range: `${config.tabName}!A:A`,
majorDimension: "ROWS",
values: [row],
}),
});
if (!response.ok) {
const errBody = await response.json();
throw new Error(
errBody.error ? errBody.error.message : response.statusText,
);
}
return await response.json();
}
async function loadAppendConfig(data) {
const storedConfig = await getStoredConfig();
const sheetId = data.sheetId || storedConfig.sheetId;
const tabName = data.tabName || storedConfig.tabName;
const fields =
Array.isArray(data.fields) && data.fields.length
? data.fields
: storedConfig.fields;
const valuesByFieldId = data.valuesByFieldId || {};
if (!sheetId) {
throw new Error("Google Sheet ID is required.");
}
if (!tabName) {
throw new Error("Tab name is required.");
}
if (!Array.isArray(fields) || fields.length === 0) {
throw new Error("At least one configured field is required.");
}
return {
sheetId,
tabName,
fields,
valuesByFieldId,
};
}
function getStoredConfig() {
const configStorageKey = "jobFillConfig";
return new Promise((resolve) => {
chrome.storage.sync.get(
[configStorageKey, "sheetId", "tabName", "fields"],
(data) => {
const storedConfig = data[configStorageKey] || {};
resolve({
sheetId: storedConfig.sheetId || data.sheetId || "",
tabName: storedConfig.tabName || data.tabName || "",
fields: Array.isArray(storedConfig.fields)
? storedConfig.fields
: Array.isArray(data.fields)
? data.fields
: [],
});
},
);
});
}
if (typeof module !== "undefined") {
module.exports = {
handleAppend,
getAuthToken,
appendToGoogleSheet,
loadAppendConfig,
getStoredConfig,
};
}