diff --git a/src/AugmentedCanvasPlugin.ts b/src/AugmentedCanvasPlugin.ts index ab91b0e..ba7d15d 100644 --- a/src/AugmentedCanvasPlugin.ts +++ b/src/AugmentedCanvasPlugin.ts @@ -41,6 +41,7 @@ import { insertSystemPrompt } from "./actions/commands/insertSystemPrompt"; import { runPromptFolder } from "./actions/commands/runPromptFolder"; import { InputModal } from "./modals/InputModal"; import { runYoutubeCaptions } from "./actions/commands/youtubeCaptions"; +import { insertWebsiteContent } from "./actions/commands/websiteContent"; export default class AugmentedCanvasPlugin extends Plugin { triggerByPlugin: boolean = false; @@ -312,6 +313,35 @@ export default class AugmentedCanvasPlugin extends Plugin { addCommands() { const app = this.app; + // * Website to MD + // this.addCommand({ + // id: "insert-website-content", + // name: "Insert the content of a website as markdown", + // checkCallback: (checking: boolean) => { + // if (checking) { + // // console.log({ checkCallback: checking }); + // if (!getActiveCanvas(app)) return false; + + // return true; + // } + + // new InputModal( + // app, + // { + // label: "Enter a website url", + // buttonLabel: "Get website content", + // }, + // (videoUrl: string) => { + // new Notice(`Scraping website content`); + + // insertWebsiteContent(app, this.settings, videoUrl); + // } + // ).open(); + // }, + // // callback: () => {}, + // }); + + // * Youtube captions // this.addCommand({ // id: "insert-youtube-caption", // name: "Insert captions of a Youtube video", diff --git a/src/actions/commands/websiteContent.ts b/src/actions/commands/websiteContent.ts new file mode 100644 index 0000000..9c701d4 --- /dev/null +++ b/src/actions/commands/websiteContent.ts @@ -0,0 +1,11 @@ +import { App } from "obsidian"; +import { AugmentedCanvasSettings } from "src/settings/AugmentedCanvasSettings"; +import { getWebsiteContent } from "src/utils/websiteContentUtils"; + +export const insertWebsiteContent = ( + app: App, + settings: AugmentedCanvasSettings, + url: string +) => { + // const { textContent } = getWebsiteContent(url); +}; diff --git a/src/actions/commands/youtubeCaptions.ts b/src/actions/commands/youtubeCaptions.ts index 4d21ee2..6101834 100644 --- a/src/actions/commands/youtubeCaptions.ts +++ b/src/actions/commands/youtubeCaptions.ts @@ -8,51 +8,45 @@ async function getVideoSubtitles( settings: AugmentedCanvasSettings, videoId: string ): Promise { - // TODO Convert to Oauth - const youtube = google.youtube({ - version: "v3", - auth: settings.youtubeApiKey, // Replace with your API key - }); - - try { - const response = await youtube.captions.list({ - part: ["snippet"], - videoId: videoId, - }); - console.log({ response }); - - const items = response.data.items; - if (items) { - const subtitles = []; - for await (const caption of items) { - console.log({ caption }); - try { - const response = await youtube.captions.download( - { - id: caption.id!, - tfmt: "ttml", // This specifies the format of the captions file. Options are 'ttml' or 'vtt' for example. - }, - { - responseType: "text", - } - ); - - // The caption content will be in the response body as a string - subtitles.push(response.data as string); - } catch (error) { - console.error("Error downloading caption:", error); - } - } - - return subtitles; - } - - return []; - } catch (error) { - console.error("Error fetching video captions:", error); - - return []; - } + // // TODO Convert to Oauth + // const youtube = google.youtube({ + // version: "v3", + // auth: settings.youtubeApiKey, // Replace with your API key + // }); + // try { + // const response = await youtube.captions.list({ + // part: ["snippet"], + // videoId: videoId, + // }); + // console.log({ response }); + // const items = response.data.items; + // if (items) { + // const subtitles = []; + // for await (const caption of items) { + // console.log({ caption }); + // try { + // const response = await youtube.captions.download( + // { + // id: caption.id!, + // tfmt: "ttml", // This specifies the format of the captions file. Options are 'ttml' or 'vtt' for example. + // }, + // { + // responseType: "text", + // } + // ); + // // The caption content will be in the response body as a string + // subtitles.push(response.data as string); + // } catch (error) { + // console.error("Error downloading caption:", error); + // } + // } + // return subtitles; + // } + // return []; + // } catch (error) { + // console.error("Error fetching video captions:", error); + // return []; + // } } export const runYoutubeCaptions = async ( @@ -60,10 +54,9 @@ export const runYoutubeCaptions = async ( settings: AugmentedCanvasSettings, videoUrl: string ) => { - const videoId = getYouTubeVideoId(videoUrl); - console.log({ videoId }); - if (!videoId) return; - - const subtitles = await getVideoSubtitles(settings, videoId); - console.log({ subtitles }); + // const videoId = getYouTubeVideoId(videoUrl); + // console.log({ videoId }); + // if (!videoId) return; + // const subtitles = await getVideoSubtitles(settings, videoId); + // console.log({ subtitles }); }; diff --git a/src/utils/websiteContentUtils.ts b/src/utils/websiteContentUtils.ts new file mode 100644 index 0000000..7a138b9 --- /dev/null +++ b/src/utils/websiteContentUtils.ts @@ -0,0 +1,96 @@ +export const getWebsiteContent = async (url: string) => { + // // console.log({ getWebsiteContent2: true }) + // const content = await fetch(url, { + // // mode: "no-cors", + // }); + // console.log({ content, body: content.body }); + // return {}; + // const getMDForTagName = (tagName: string) => { + // if (tagName === "h1") { + // return "#"; + // } else if (tagName === "h2") { + // return "##"; + // } else if (tagName === "h3") { + // return "###"; + // } else if (tagName === "h4") { + // return "####"; + // } else if (tagName === "h5") { + // return "#####"; + // } else if (tagName === "h6") { + // return "######"; + // } + // }; + // // let count = 0; + // let textContent = ""; + // // const selectors = []; + // // function fullPath(el) { + // // var names = []; + // // while (el.parentNode) { + // // if (el.id) { + // // names.unshift("#" + el.id); + // // break; + // // } else { + // // if (el == el.ownerDocument.documentElement) + // // names.unshift(el.tagName); + // // else { + // // for ( + // // var c = 1, e = el; + // // e.previousElementSibling; + // // e = e.previousElementSibling, c++ + // // ); + // // names.unshift(el.tagName + ":nth-child(" + c + ")"); + // // } + // // el = el.parentNode; + // // } + // // } + // // return names.join(" > "); + // // } + // // Function to traverse all elements in the DOM + // function traverseDOM(element: Element): void { + // // Process the current element + // // console.log(element.tagName); + // const includedTags = ["p", "h1", "h2", "h3", "h4", "h5", "h6"]; + // // const excludedTags = ["script", "button"] + // if ( + // includedTags.includes(element.tagName.toLowerCase()) + // // && + // // element.textContent.split(" ").length > 5 + // ) { + // const text = element.textContent + // ?.replace(/\n/g, " ") + // .replace(/\\n/g, "") + // .replace(/\t/g, "") + // .replace(/\\t/g, "") + // .trim(); + // // console.log({ text, tagName: element.tagName }) + // // * Example: 1. ### Title + // textContent += + // "\n\n" + + // // `${count}.` + + // (element.tagName.toLowerCase() !== "p" + // ? getMDForTagName(element.tagName.toLowerCase()) + " " + // : "") + + // text; + // // count++; + // // const path = fullPath(element); + // // selectors.push(path); + // // document.querySelector(path).scrollIntoView() + // } + // // Recursively traverse child elements + // Array.from(element.children).forEach((child) => { + // traverseDOM(child); + // }); + // } + // // Example usage + // // document.addEventListener('DOMContentLoaded', () => { + // traverseDOM(document.documentElement); + // // }); + // console.log({ + // // selectors, + // textContent, + // }); + // return { + // textContent, + // // selectors, + // }; +};