Repository navigation
Expand file tree
/
Copy pathgemini-git-push.user.js
More file actions
394 lines (333 loc) · 15.4 KB
/
Copy pathgemini-git-push.user.js
File metadata and controls
394 lines (333 loc) · 15.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// ==UserScript==
// @name Gemini to GitHub Pusher
// @namespace https://spacebarlabs.com/
// @version 18.2
// @description Syncs Gemini to GitHub. Fixes <ol> lists and properly escapes HTML entities in user content.
// @author Benjamin Oakes
// @license GPLv3
// @match https://gemini.google.com/*
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
// ==/UserScript==
(function() {
'use strict';
// --- ⚙️ TIMING CONFIGURATION ---
const URL_CHECK_INTERVAL = 2000;
const CONTENT_CHECK_INTERVAL = 20000;
const PAGE_LOAD_DELAY = 4000;
let lastUrl = window.location.href;
let isPushing = false;
let remoteFileStatus = 'unknown';
// --- CONFIG UI ---
GM_config.init({
id: 'GeminiGHConfig',
title: 'Gemini to GitHub Settings',
fields: {
'GITHUB_TOKEN': { label: 'GitHub Token', type: 'text', default: '' },
'REPO_OWNER': { label: 'Repo Owner', type: 'text', default: '' },
'REPO_NAME': { label: 'Repo Name', type: 'text', default: '' },
'FOLDER_PATH': { label: 'Folder Path', type: 'text', default: 'gemini' },
'BRANCH': { label: 'Branch', type: 'text', default: 'main' }
},
css: '#GeminiGHConfig_field_GITHUB_TOKEN { width: 300px; }'
});
GM_registerMenuCommand("Configure GitHub Settings", () => GM_config.open());
// --- LOOPS ---
setTimeout(() => { createUI(); checkRemoteStatus(); }, 2000);
setInterval(() => {
if (!document.getElementById('gemini-gh-container')) createUI();
updateViewButtonVisuals();
if (window.location.href !== lastUrl) {
console.log("GeminiGH: 🧭 New chat detected.");
lastUrl = window.location.href;
remoteFileStatus = 'unknown';
updateButtonStatus('⏳ Loading...', '#586069');
checkRemoteStatus();
setTimeout(() => trySync(true), PAGE_LOAD_DELAY);
}
}, URL_CHECK_INTERVAL);
setInterval(() => {
if (window.location.href.includes('/app/') && !isPushing) {
trySync(true);
}
}, CONTENT_CHECK_INTERVAL);
// --- HELPERS ---
function getChatId() {
return window.location.pathname.split('/app/')[1] || null;
}
async function checkRemoteStatus() {
if (remoteFileStatus === 'checking' || remoteFileStatus === 'exists') return;
const TOKEN = GM_config.get('GITHUB_TOKEN');
const OWNER = GM_config.get('REPO_OWNER');
const REPO = GM_config.get('REPO_NAME');
const BRANCH = GM_config.get('BRANCH');
const FOLDER = GM_config.get('FOLDER_PATH');
if (!TOKEN || !OWNER) return;
const chatId = getChatId();
if (!chatId) return;
remoteFileStatus = 'checking';
updateViewButtonVisuals();
const filename = `${chatId}.md`;
const filePath = FOLDER ? `${FOLDER}/${filename}` : filename;
const url = `https://api.github.com/repos/${OWNER}/${REPO}/contents/${filePath}`;
try {
const sha = await getFileSha(url, TOKEN, BRANCH);
remoteFileStatus = sha ? 'exists' : 'missing';
} catch (e) {
remoteFileStatus = 'missing';
}
updateViewButtonVisuals();
}
async function trySync(isAuto) {
if (isPushing) return;
const TOKEN = GM_config.get('GITHUB_TOKEN');
if (!TOKEN) return;
try {
const chatId = getChatId();
if (!chatId) return;
const content = extractPageContent(chatId);
if (!content || content.length < 50) return;
const currentHash = simpleHash(content);
const savedHashKey = `gemini_hash_${chatId}`;
const lastSavedHash = GM_getValue(savedHashKey, null);
if (currentHash === lastSavedHash && remoteFileStatus === 'exists') {
if(!isAuto) updateButtonStatus('✅ Synced', '#28a745');
return;
}
isPushing = true;
updateButtonStatus(isAuto ? '☁️ Auto-saving...' : '☁️ Pushing...', '#dbab09');
const timestamp = new Date().toISOString().split('T')[0];
const filename = `${chatId}.md`;
const folderPath = GM_config.get('FOLDER_PATH');
const filePath = folderPath ? `${folderPath}/${filename}` : filename;
const finalFileContent = `**Synced:** ${timestamp}\n**Source:** [Gemini Link](${window.location.href})\n\n---\n\n${content}`;
await pushToGitHub(filePath, finalFileContent);
GM_setValue(savedHashKey, currentHash);
remoteFileStatus = 'exists';
updateButtonStatus('✅ Synced', '#28a745');
updateViewButtonVisuals();
setTimeout(() => {
updateButtonStatus('☁️ GitHub Sync', '#24292e');
}, 4000);
} catch (error) {
console.error("GeminiGH Error:", error);
updateButtonStatus('❌ Error', '#d73a49');
} finally {
isPushing = false;
}
}
// --- UI ---
function createUI() {
if (document.getElementById('gemini-gh-container')) return;
const container = document.createElement('div');
container.id = 'gemini-gh-container';
Object.assign(container.style, {
position: 'fixed', bottom: '20px', right: '20px', zIndex: '9999', display: 'flex', gap: '5px'
});
const btn = document.createElement('button');
btn.innerText = '☁️ GitHub Sync';
btn.id = 'gemini-gh-btn';
Object.assign(btn.style, {
padding: '8px 12px', backgroundColor: '#24292e', color: 'white', border: 'none',
borderRadius: '5px', cursor: 'pointer', fontFamily: 'sans-serif', fontSize: '12px', fontWeight: 'bold'
});
btn.onclick = () => trySync(false);
const viewBtn = document.createElement('button');
viewBtn.innerText = 'Checking...';
viewBtn.id = 'gemini-gh-view-btn';
Object.assign(viewBtn.style, {
padding: '8px 12px', backgroundColor: '#dbab09', color: 'black', border: 'none',
borderRadius: '5px', cursor: 'pointer', fontFamily: 'sans-serif', fontSize: '12px', fontWeight: 'bold'
});
const settingsBtn = document.createElement('button');
settingsBtn.innerText = '⚙️';
Object.assign(settingsBtn.style, {
padding: '8px', backgroundColor: '#444d56', color: 'white', border: 'none',
borderRadius: '5px', cursor: 'pointer', fontSize: '12px'
});
settingsBtn.onclick = () => GM_config.open();
container.appendChild(btn);
container.appendChild(viewBtn);
container.appendChild(settingsBtn);
document.body.appendChild(container);
}
function updateViewButtonVisuals() {
const viewBtn = document.getElementById('gemini-gh-view-btn');
if (!viewBtn) return;
const OWNER = GM_config.get('REPO_OWNER');
const REPO = GM_config.get('REPO_NAME');
const BRANCH = GM_config.get('BRANCH');
const FOLDER = GM_config.get('FOLDER_PATH');
const chatId = getChatId();
if (!chatId) return;
const filename = `${chatId}.md`;
const fullPath = FOLDER ? `${FOLDER}/${filename}` : filename;
const ghUrl = `https://github.com/${OWNER}/${REPO}/blob/${BRANCH}/${fullPath}`;
const makeClickable = () => {
viewBtn.style.cursor = 'pointer';
viewBtn.onclick = () => window.open(ghUrl, '_blank');
};
if (remoteFileStatus === 'checking' || remoteFileStatus === 'unknown') {
viewBtn.innerText = '⏳ Checking...';
viewBtn.style.backgroundColor = '#dbab09';
viewBtn.style.color = 'black';
makeClickable();
} else if (remoteFileStatus === 'exists') {
viewBtn.innerText = 'GitHub ↗';
viewBtn.style.backgroundColor = '#0366d6';
viewBtn.style.color = 'white';
makeClickable();
} else if (remoteFileStatus === 'missing') {
viewBtn.innerText = 'Not Synced';
viewBtn.style.backgroundColor = '#6a737d';
viewBtn.style.color = 'white';
viewBtn.style.cursor = 'default';
viewBtn.onclick = null;
}
}
function updateButtonStatus(text, color) {
const btn = document.getElementById('gemini-gh-btn');
if (btn) {
btn.innerText = text;
btn.style.backgroundColor = color;
}
}
function simpleHash(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash |= 0;
}
return hash.toString();
}
async function pushToGitHub(path, content) {
const OWNER = GM_config.get('REPO_OWNER');
const REPO = GM_config.get('REPO_NAME');
const TOKEN = GM_config.get('GITHUB_TOKEN');
const BRANCH = GM_config.get('BRANCH');
const url = `https://api.github.com/repos/${OWNER}/${REPO}/contents/${path}`;
let sha = null;
try { sha = await getFileSha(url, TOKEN, BRANCH); } catch (e) {}
return new Promise((resolve, reject) => {
const contentBase64 = btoa(unescape(encodeURIComponent(content)));
const payload = { message: `Update ${path}`, content: contentBase64, branch: BRANCH };
if (sha) payload.sha = sha;
GM_xmlhttpRequest({
method: "PUT",
url: url,
headers: { "Authorization": `token ${TOKEN}`, "Content-Type": "application/json" },
data: JSON.stringify(payload),
onload: (res) => (res.status >= 200 && res.status < 300) ? resolve() : reject(new Error(res.statusText)),
onerror: reject
});
});
}
function getFileSha(url, token, branch) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
url: `${url}?ref=${branch}`,
headers: { "Authorization": `token ${token}`, "Cache-Control": "no-cache" },
onload: (res) => {
if (res.status === 200) resolve(JSON.parse(res.responseText).sha);
else if (res.status === 404) resolve(null);
else reject(new Error(res.statusText));
},
onerror: reject
});
});
}
// --- EXTRACTION ---
function extractPageContent(chatId) {
const KNOWN_LANGUAGES = new Set([
"python", "py", "javascript", "js", "typescript", "ts", "java", "cpp", "c++", "c",
"c#", "csharp", "ruby", "rb", "html", "css", "scss", "bash", "sh", "shell", "zsh",
"json", "xml", "yaml", "yml", "sql", "php", "go", "rust", "kotlin", "swift", "dart",
"r", "lua", "perl", "objective-c", "markdown", "md", "dockerfile", "latex", "tex",
"powershell", "ps1", "vb", "vbnet", "scala", "groovy", "haskell", "elixir", "clojure"
]);
function htmlToMarkdown(node) {
// 1. Text Node Handling: Escape HTML entities
if (node.nodeType === Node.TEXT_NODE) {
return node.textContent.replace(/</g, '<').replace(/>/g, '>');
}
if (node.nodeType !== Node.ELEMENT_NODE) return "";
const tagName = node.tagName.toLowerCase();
if (tagName === 'table') {
const rows = Array.from(node.querySelectorAll('tr'));
if (!rows.length) return "";
let md = "\n";
rows.forEach((row, i) => {
const cells = Array.from(row.querySelectorAll('td, th')).map(c => htmlToMarkdown(c).trim().replace(/\n/g, '<br>'));
md += `| ${cells.join(' | ')} |\n`;
if (i === 0) md += `| ${cells.map(() => '---').join(' | ')} |\n`;
});
return md + "\n";
}
// 2. Code Block: Do NOT escape content (stop recursion)
if (tagName === 'code') return `\`${node.textContent}\``;
// 3. Pre Block: Do NOT escape content
if (tagName === 'pre') return `\n\`\`\`${node.dataset.language || ''}\n${node.innerText}\n\`\`\`\n\n`;
// Process children for other tags
let children = Array.from(node.childNodes).map(htmlToMarkdown).join("");
if (['h1','h2','h3'].includes(tagName)) return `\n${'#'.repeat(parseInt(tagName[1]))} ${children}\n\n`;
if (tagName === 'p') return `\n${children}\n\n`;
if (tagName === 'li') {
const parent = node.parentNode;
const parentTag = parent ? parent.tagName.toLowerCase() : '';
if (parentTag === 'ol') {
let index = 1;
let sibling = node.previousElementSibling;
while (sibling) {
if (sibling.tagName.toLowerCase() === 'li') index++;
sibling = sibling.previousElementSibling;
}
return `${index}. ${children.trim()}\n`;
}
return `- ${children.trim()}\n`;
}
if (tagName === 'a') return `[${children}](${node.getAttribute('href')})`;
if (tagName === 'img') return `})`;
if (tagName === 'strong' || tagName === 'b') return `**${children}**`;
if (tagName === 'em' || tagName === 'i') return `*${children}*`;
return children;
}
const elements = document.querySelectorAll('user-query-content, structured-content-container');
if (!elements.length) return "";
let md = "";
elements.forEach(el => {
const clone = el.cloneNode(true);
clone.querySelectorAll('pre').forEach(pre => {
const prev = pre.previousElementSibling;
if (prev && prev.innerText.match(/copy/i)) pre.dataset.language = prev.innerText.replace(/copy code|copy/gi,'').trim();
});
clone.querySelectorAll('button, svg, .edit-button, .feedback-container').forEach(e => e.remove());
let text = htmlToMarkdown(clone).trim();
// Fix for fake code blocks in output
text = text.replace(/(\n|^)([a-zA-Z0-9+#\-\.]+)(\n+)```/gm, (match, prefix, word, newlines) => {
if (KNOWN_LANGUAGES.has(word.toLowerCase())) {
return `${prefix}\`\`\`${word}`;
}
return match;
});
// Handle User Content specific formatting
if (el.tagName.toLowerCase().includes('user')) {
const firstNewLine = text.indexOf('\n');
if (firstNewLine === -1) {
md += `<div align="right">\n${text}\n</div>\n\n---\n\n`;
} else {
const summary = text.substring(0, firstNewLine);
const body = text.substring(firstNewLine + 1);
md += `<div align="right">\n<details>\n<summary>${summary}</summary>\n\n${body}\n</details>\n</div>\n\n---\n\n`;
}
} else {
md += `${text}\n\n---\n\n`;
}
});
return md;
}
})();