-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
154 lines (132 loc) · 4.44 KB
/
Copy pathbackground.js
File metadata and controls
154 lines (132 loc) · 4.44 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Blip background service worker
// Handles GitHub API calls on behalf of the content script.
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "GITHUB_FETCH") {
githubFetchFile(message.config)
.then((result) => sendResponse({ success: true, data: result }))
.catch((err) => sendResponse({ success: false, error: err.message }));
return true; // keep channel open for async response
}
if (message.type === "GITHUB_LIST_FILES") {
githubListFiles(message.config)
.then((result) => sendResponse({ success: true, data: result }))
.catch((err) => sendResponse({ success: false, error: err.message }));
return true;
}
if (message.type === "GITHUB_COMMIT") {
githubCommitFile(message.config, message.content, message.sha)
.then((result) => sendResponse({ success: true, data: result }))
.catch((err) => sendResponse({ success: false, error: err.message }));
return true;
}
});
async function githubListFiles(config) {
const { owner, repo, branch, token } = config;
const url = `https://api.github.com/repos/${owner}/${repo}/contents/?ref=${branch}`;
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github.v3+json",
},
});
if (!response.ok) {
const body = await response.text();
throw new Error(`GitHub list failed (${response.status}): ${body}`);
}
const data = await response.json();
// Return only files (not directories), with name and path
return data
.filter((item) => item.type === "file")
.map((item) => ({ name: item.name, path: item.path, size: item.size }));
}
async function githubFetchFile(config) {
const { owner, repo, branch, filePath, token } = config;
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${filePath}?ref=${branch}`;
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github.v3+json",
},
});
if (!response.ok) {
const body = await response.text();
throw new Error(`GitHub fetch failed (${response.status}): ${body}`);
}
const data = await response.json();
const content = atob(data.content.replace(/\n/g, ""));
// Handle UTF-8 properly
const decoded = new TextDecoder().decode(
Uint8Array.from(content, (c) => c.charCodeAt(0)),
);
return {
content: decoded,
sha: data.sha,
size: data.size,
path: data.path,
};
}
async function githubCommitFile(config, newContent, sha) {
const { owner, repo, branch, filePath, token } = config;
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${filePath}`;
// Encode content to base64 (UTF-8 safe)
const base64 = btoa(unescape(encodeURIComponent(newContent)));
const timestamp = new Date().toISOString().replace("T", " ").slice(0, 19);
const commitMessage = `Blip edit: ${timestamp}`;
const response = await fetch(url, {
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github.v3+json",
"Content-Type": "application/json",
},
body: JSON.stringify({
message: commitMessage,
content: base64,
sha: sha,
branch: branch,
}),
});
if (!response.ok) {
const body = await response.text();
throw new Error(`GitHub commit failed (${response.status}): ${body}`);
}
const data = await response.json();
return {
sha: data.content.sha,
commitSha: data.commit.sha,
commitMessage: commitMessage,
commitUrl: data.commit.html_url,
};
}
chrome.action.onClicked.addListener(async (tab) => {
if (!tab.id) return;
try {
// Try to toggle if content scripts are already injected
await chrome.tabs.sendMessage(tab.id, { action: "toggle_blip_sidebar" });
} catch (err) {
// Content scripts not yet injected on this tab - inject them now
try {
await chrome.scripting.insertCSS({
target: { tabId: tab.id },
files: ["content.css"],
});
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: [
"config.js",
"github.js",
"file-resolver.js",
"mapping.js",
"edit-history.js",
"text-diff.js",
"local-fs.js",
"content.js",
],
});
} catch (injectErr) {
console.log(
"Blip: Cannot inject on this page (likely a chrome:// restricted URL).",
);
}
}
});