-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
130 lines (111 loc) · 4.67 KB
/
Copy pathoptions.js
File metadata and controls
130 lines (111 loc) · 4.67 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
124
125
126
127
128
129
130
document.addEventListener('DOMContentLoaded', function() {
const githubTokenInput = document.getElementById('githubToken');
const githubRepoInput = document.getElementById('githubRepo');
const githubBranchInput = document.getElementById('githubBranch');
const githubFolderInput = document.getElementById('githubFolder');
const saveNotesAndTimerInput = document.getElementById('saveNotesAndTimer');
const toggleToken = document.getElementById('toggleToken');
const testButton = document.getElementById('test');
const saveButton = document.getElementById('save');
const statusDiv = document.getElementById('status');
// Load saved settings
chrome.storage.sync.get([
'githubToken',
'githubRepo',
'githubBranch',
'githubFolder',
'saveNotesAndTimer'
], function(items) {
if (items.githubToken) githubTokenInput.value = items.githubToken;
if (items.githubRepo) githubRepoInput.value = items.githubRepo;
if (items.githubBranch) githubBranchInput.value = items.githubBranch;
if (items.githubFolder) githubFolderInput.value = items.githubFolder;
saveNotesAndTimerInput.checked = items.saveNotesAndTimer !== undefined ? items.saveNotesAndTimer : true;
});
// Toggle token password visibility
toggleToken.addEventListener('change', function() {
githubTokenInput.type = this.checked ? 'text' : 'password';
});
// Test GitHub API connection
testButton.addEventListener('click', function() {
const token = githubTokenInput.value.trim();
const repo = githubRepoInput.value.trim();
if (!token) {
showStatus('Please enter a GitHub Personal Access Token first.', 'error');
return;
}
if (!repo || !repo.includes('/')) {
showStatus('Please enter a repository in "owner/repo" format.', 'error');
return;
}
// Set loading status
showStatus('Testing connection to GitHub repository...', 'loading');
testButton.disabled = true;
saveButton.disabled = true;
chrome.runtime.sendMessage({
action: 'testConnection',
token: token,
repo: repo
}, function(response) {
testButton.disabled = false;
saveButton.disabled = false;
if (chrome.runtime.lastError) {
showStatus(`Connection failed: ${chrome.runtime.lastError.message}`, 'error');
return;
}
if (response && response.success) {
const details = response.result;
const repoType = details.private ? 'private' : 'public';
showStatus(`Success! Connected to ${repoType} repository: "${details.fullName}"`, 'success');
} else {
const errMsg = (response && response.error) ? response.error : 'Unknown error';
showStatus(`Connection failed: ${errMsg}`, 'error');
}
});
});
// Save settings
saveButton.addEventListener('click', function() {
const token = githubTokenInput.value.trim();
const repo = githubRepoInput.value.trim();
const branch = githubBranchInput.value.trim() || 'main';
const folder = githubFolderInput.value.trim();
if (!token) {
showStatus('Please enter your GitHub Personal Access Token.', 'error');
return;
}
if (!repo) {
showStatus('Please enter your GitHub repository (owner/repo).', 'error');
return;
}
if (!repo.includes('/')) {
showStatus('Repository must be in "owner/repo" format (e.g., username/leetcode-solutions).', 'error');
return;
}
saveButton.disabled = true;
chrome.storage.sync.set({
githubToken: token,
githubRepo: repo,
githubBranch: branch,
githubFolder: folder,
saveNotesAndTimer: saveNotesAndTimerInput.checked
}, function() {
saveButton.disabled = false;
showStatus('Settings saved successfully!', 'success');
// Clear success status after 3 seconds
setTimeout(function() {
// If it's still showing success, clear it
if (statusDiv.className.includes('success')) {
clearStatus();
}
}, 3000);
});
});
function showStatus(message, type) {
statusDiv.textContent = message;
statusDiv.className = 'status ' + type;
}
function clearStatus() {
statusDiv.textContent = '';
statusDiv.className = 'status';
}
});