Skip to content

Instantly share code, notes, and snippets.

@Gaubee
Created July 20, 2025 06:44
Show Gist options
  • Save Gaubee/cb3ff4b5250b2c5e30e23c348f07c102 to your computer and use it in GitHub Desktop.
Save Gaubee/cb3ff4b5250b2c5e30e23c348f07c102 to your computer and use it in GitHub Desktop.
sync-google-aistudio.js
//@ts-check
{
// --- 必要的准备工作 ---
const styles = {
header: "color: #4CAF50; font-size: 18px; font-weight: bold; border-bottom: 2px solid #4CAF50; padding-bottom: 5px;",
info: "color: #2196F3; font-style: italic;",
success: "color: #8BC34A; font-weight: bold;",
error: "color: #F44336; font-weight: bold;",
code: "background-color: #f0f0f0; color: #333; padding: 2px 4px; border-radius: 3px; font-family: monospace;",
warn: "color: #FFC107;",
};
// 修改前端 get code 按钮 和 面板
const styleEle = document.createElement("style");
const headEle = document.querySelector("head");
const css = String.raw;
// 隐藏 GetCode 面板,禁止 GetCode 按钮被用户点击
styleEle.innerText = css`
.cdk-overlay-container:has(.get-code-dialog) {
display: none;
}
button[aria-label="Get code"] {
pointer-events: none;
}
`.replaceAll("\n", "");
headEle.appendChild(styleEle);
//////
let rootDirHandle;
const prepareDirHandle = async () => {
if (rootDirHandle) {
return;
}
// 1. 请求用户选择一个 *根* 文件夹
console.log("%c awaiting user action: Please select a parent directory in the dialog.", styles.info);
rootDirHandle = await window.showDirectoryPicker();
console.log(`%c✅ 根文件夹已选择: %c${rootDirHandle.name}`, styles.success, styles.code);
};
let writting = false;
async function runFileCreationInSubfolder(b, targetName = location.pathname.split("/").at(-1)) {
if (writting) {
return;
}
writting = true;
try {
await prepareDirHandle();
const fileHandle = await rootDirHandle.getFileHandle(targetName + ".json", {
create: true,
});
const writable = await fileHandle.createWritable();
console.log("%c - 创建可写流成功。", styles.info);
await writable.write(JSON.stringify(b));
console.log("%c - 数据写入中...", styles.info);
await writable.close();
console.log(`%c - ✅ 文件写入并关闭成功: %c${name}`, styles.success, styles.code);
console.log("------------------------------------");
console.log(`%c✨ 全部文件已在文件夹 "${targetName}" 中创建完毕!`, styles.header);
} catch (error) {
if (error.name === "AbortError") {
console.warn("%c⚠️ 用户取消了文件夹选择操作。流程已中止。", styles.warn);
} else {
console.error("%c❌ 发生意外错误:", styles.error, error);
}
}
writting = false;
}
// 篡改 get-code 最后的render函数
const findMustacheKey = () => {
for (const key in default_MakerSuite) {
const render = default_MakerSuite[key]?.render;
if (render && typeof render === "function") {
return key;
}
}
};
const waitMustacheKey = async () => {
while (true) {
const key = findMustacheKey();
if (key) {
return key;
}
await new Promise((c) => setTimeout(c, 200));
}
};
async function startInject() {
const key = await waitMustacheKey();
const render = default_MakerSuite[key].render;
// 请求用户选择一个文件夹
default_MakerSuite[key].render = (...args) => {
const b = args[1];
void runFileCreationInSubfolder(b);
// 这里不再调用原来的render,减少性能损耗
// return render(...args);
return "";
};
}
// 打开 GetCode 面板且不再关闭
if (!document.querySelector(".cdk-overlay-container:has(.get-code-dialog)")) {
const findBtn = () => document.querySelector(`button[aria-label="Get code"]`);
const waitBtn = async () => {
while (true) {
const btn = findBtn();
if (btn) {
return btn;
}
await new Promise((cb) => setTimeout(cb, 200));
}
};
waitBtn().then((getCodeButtonEle) => {
getCodeButtonEle.click();
requestAnimationFrame(startInject);
});
} else {
startInject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment