Skip to content

Commit

Permalink
Prepare for use of the YouTube thumbnail service
Browse files Browse the repository at this point in the history
Relates to #44.
  • Loading branch information
wheelercj committed Jun 17, 2024
1 parent f7242d9 commit e1e2153
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
8 changes: 2 additions & 6 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,10 @@ async function handleRequest(message) {
const imageMd = await md.createImage(message.srcUrl);
return await handleCopyRequest(imageMd + '\n');
case 'videoRightClick':
const videoMd = await md.createMedia(
'video', message.srcUrl, message.pageUrl
);
const videoMd = await md.createVideo(message.srcUrl, message.pageUrl);
return await handleCopyRequest(videoMd + '\n');
case 'audioRightClick':
const audioMd = await md.createMedia(
'audio', message.srcUrl, message.pageUrl
);
const audioMd = await md.createAudio(message.srcUrl, message.pageUrl);
return await handleCopyRequest(audioMd + '\n');
default:
console.error('Unknown message category:', message.category);
Expand Down
45 changes: 34 additions & 11 deletions src/md.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,46 @@ export async function createImage(url) {
}

/**
* createMedia creates markdown for video or audio. For rendering purposes, the
* resulting markdown will only start with an exclamation mark if the page URL is used.
* @param {string} altText - a description of the media to use in the markdown link.
* @param {string} srcUrl - the URL of the media. If this is falsy or starts with
* `blob:`, the page URL is used instead.
* @param {string} pageUrl - the URL of the page the media is on. This is used only if
* the source URL is falsy or starts with `blob:`.
* createVideo creates markdown of a video. The source URL is used if and only if it's
* truthy and does not start with `blob:`. For rendering purposes, the resulting
* markdown will only start with an exclamation mark if the page URL is used.
* @param {string} srcUrl - the URL of the video.
* @param {string} pageUrl - the URL of the page the video is on.
* @returns {Promise<string>}
*/
export async function createMedia(altText, srcUrl, pageUrl) {
if (srcUrl && !srcUrl.startsWith('blob:')) {
return await createLink(altText, srcUrl);
export async function createVideo(srcUrl, pageUrl) {
const usingSrcUrl = srcUrl && !srcUrl.startsWith('blob:');
const url = usingSrcUrl ? srcUrl : pageUrl;

let youtubeId; // TODO
let isYoutube = false; // TODO
const youtubeMd = await getSetting('youtubeMd', 'Obsidian & Discord');

if (isYoutube && youtubeMd === 'GitHub') {
// TODO: use fwd-microservice
} else {
return '!' + await createLink(altText, pageUrl);
const link = await createLink('video', url);
if (usingSrcUrl) {
return link;
} else {
return '!' + link;
}
}
}

/**
* createAudio creates markdown of audio. The source URL is used if and only if it's
* truthy and does not start with `blob:`.
* @param {string} srcUrl - the URL of the audio.
* @param {string} pageUrl - the URL of the page the audio is on.
* @returns {Promise<string>}
*/
export async function createAudio(srcUrl, pageUrl) {
const usingSrcUrl = srcUrl && !srcUrl.startsWith('blob:');
const url = usingSrcUrl ? srcUrl : pageUrl;
return await createLink('audio', url);
}

/**
* createTabLink creates a markdown link for a tab. Stardown does not add to, or remove
* from, the link any HTML element ID or text fragment. The tab title is used as the
Expand Down
7 changes: 7 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ <h2 style="text-align: center">

<h3 style="text-align: center">Options</h3>
<form>
<label for="youtubeMd">Optimize markdown of YouTube videos for</label>
<select id="youtubeMd" name="youtubeMd">
<option value="Obsidian & Discord">Obsidian & Discord</option>
<option value="GitHub">GitHub</option>
</select>
<br />
<br />
<input id="notifyOnSuccess" type="checkbox" />
<label for="notifyOnSuccess">Show a notification when a context menu option succeeds</label>
<br />
Expand Down
4 changes: 4 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async function saveOptions(e) {
e.preventDefault();
await browser.storage.sync.set(
{
youtubeMd: document.querySelector("#youtubeMd").value,
notifyOnSuccess: document.querySelector("#notifyOnSuccess").checked,
subBrackets: document.querySelector("#subBrackets").value,
selectionFormat: document.querySelector("#selectionFormat").value,
Expand Down Expand Up @@ -48,6 +49,9 @@ async function saveOptions(e) {

async function loadOptions() {
try {
const youtubeMd = await getSetting("youtubeMd", "Obsidian & Discord");
document.querySelector("#youtubeMd").value = youtubeMd;

const notifyOnSuccess = await getSetting("notifyOnSuccess", false);
document.querySelector("#notifyOnSuccess").checked = notifyOnSuccess;

Expand Down

0 comments on commit e1e2153

Please sign in to comment.