Trying to embed a youtube video but constantly getting the “Error 153”. Tried it with both iframes and and the built-in way (“…”) but no matter what I do it shows the Error 153.
Thanks. It’s annyoying that the full folder update doesn’t work with the plugin sometimes…?
So I made this script:
import { App, Plugin, TFile } from 'obsidian';
const addYtLinkTemporarily = async (app: App): Promise<void> => {
const activeFile = app.workspace.getActiveFile();
if (!activeFile) {
console.log('Yt-Link-Adder: No active file');
return;
}
console.log('Yt-Link-Adder: Active file found:', activeFile.basename);
// Read the current content
const content = await app.vault.read(activeFile);
// Find any YouTube URL in the content
// Matches: youtube.com/watch?v=, youtu.be/, with or without https://, www., etc.
const ytRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/|v\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
const ytMatch = content.match(ytRegex);
if (!ytMatch) {
console.log('Yt-Link-Adder: No YouTube URL found in the document');
return;
}
const ytLink = ytMatch[0];
console.log('Yt-Link-Adder: Found YouTube link:', ytLink);
// Find the second "---" in the file
const separator = '---';
let firstIndex = content.indexOf(separator);
if (firstIndex === -1) {
console.log('Yt-Link-Adder: First "---" not found');
return;
}
let secondIndex = content.indexOf(separator, firstIndex + separator.length);
if (secondIndex === -1) {
console.log('Yt-Link-Adder: Second "---" not found');
return;
}
console.log('Yt-Link-Adder: Second "---" found at position:', secondIndex);
// Calculate insertion point (right after the second "---" and its newline)
const insertPosition = secondIndex + separator.length;
const imageLink = `\n`;
console.log('Yt-Link-Adder: Inserting image link...');
// Insert the image link
const newContent =
content.slice(0, insertPosition) +
imageLink +
content.slice(insertPosition);
await app.vault.modify(activeFile, newContent);
console.log('Yt-Link-Adder: Image link inserted, will remove in 4 seconds');
// Wait 4 seconds, then remove the link
setTimeout(async () => {
const currentContent = await app.vault.read(activeFile);
const updatedContent = currentContent.replace(imageLink, '');
await app.vault.modify(activeFile, updatedContent);
console.log('Yt-Link-Adder: Image link removed');
}, 4000);
};
export class YtLinkAdderPlugin extends Plugin {
async onload() {
this.addCommand({
id: 'yt-link-adder',
name: 'Add YouTube link temporarily',
callback: () => addYtLinkTemporarily(this.app)
});
}
}
export async function invoke(app: App): Promise<void> {
return addYtLinkTemporarily(app);
}
Must be used with the CodeScript Toolkit plugin. Save it with some name with .ts extension. Open a file and press the key combo you attached to the script.
Adds the embed link below the YAML block and removes it in 4 secs, enough time to trigger the Featured Image plugin to scrape the YT thumbs.
It helps if there is a unique YT link in the note, otherwise, if there is more than one URLs, it uses the first one it finds in content.