import "@johnlindquist/kit";
import { readdirSync, readFileSync } from "fs";
let SNIPPETS_FOLDER = await env("SNIPPET_FOLDER", {
hint: md(
`Path to folder e.g. /Users/jacobcofman/Documents/chrome-snippets`
),
ignoreBlur: true,
secret: false,
})
let snippetsFolder = await env("SNIPPET_FOLDER")
if (!(await isDir(snippetsFolder))) {
mkdir("-p", snippetsFolder)
}
const getSnippets = (folder) => {
const snippets = [];
const files = readdirSync(folder);
files.forEach((file) => {
if (file.endsWith(".js")) {
snippets.push(file);
}
});
return snippets;
};
let snippetsToRun = getSnippets(SNIPPETS_FOLDER);
let snippetFileToRun = await arg(
"Select Snippet to run:",
snippetsToRun.map((file) => file)
);
const snippetFileContentToRun = readFileSync(
SNIPPETS_FOLDER + "/" + snippetFileToRun,
"utf8"
);
const runSnippet = async () => {
await applescript(String.raw`
tell application "Google Chrome"
execute front window's active tab javascript "${snippetFileContentToRun}"
end tell
`);
}
await runSnippet();