Skip to content

Commit f6bfc58

Browse files
committed
feat(popup): linkify all urls in video descriptions
1 parent 4a64d6e commit f6bfc58

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

background/sources/collections/haloruns.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ const RECORDS_PAGE = 'https://haloruns.com/records?recent';
55
const resolveLink = ($link) => {
66
return new URL($link.getAttribute('href'), RECORDS_PAGE).href;
77
};
8+
9+
const $a = document.createElement('a');
810
const embedLink = ($link) => {
9-
const $a = document.createElement('a');
1011
$a.href = resolveLink($link);
1112
$a.target = '_blank';
1213
$a.textContent = $link.textContent;

background/sources/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,26 @@ const sources = {
4545
let videos;
4646
try {
4747
videos = await fn();
48+
for (let video of videos) {
49+
// Add links to video description.
50+
if (video.desc) {
51+
video.desc = util.embedLinks(video.desc);
52+
}
53+
}
54+
if (!isHost) {
55+
videos = await util.parallelFilter(videos, sources.addMetaToVideo);
56+
}
4857
cachedResults[type][source] = videos;
58+
4959
} catch (err) {
5060
console.warn(`Failed to get videos for ${source}: ${err.message}`);
5161
console.error(err);
5262

5363
// Fallback to cached videos from this source if getting videos fails.
5464
videos = cachedResults[type][source] || [];
5565
}
56-
if (isHost) {
57-
return { source, videos };
58-
} else {
59-
videos = await util.parallelFilter(videos, sources.addMetaToVideo);
60-
return { source, videos };
61-
}
66+
67+
return { source, videos };
6268
}));
6369
};
6470

@@ -165,6 +171,7 @@ const sources = {
165171
video[field] = meta[field];
166172
}
167173
});
174+
168175
return true;
169176
},
170177

background/util.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,20 @@ export const uniq = (arr) => {
261261
export const sleep = (ms) => {
262262
return new Promise(resolve => setTimeout(resolve, ms));
263263
};
264+
265+
/**
266+
* Converts urls in a string, that are not already html links, to html links.
267+
*
268+
* @param {string} str
269+
* @return {string}
270+
*/
271+
const $a = document.createElement('a');
272+
export const embedLinks = (str) => {
273+
return str.replace(/(href=")?(https?:\/\/[^"'()[\]{} ]+)/g, (m, p1, url) => {
274+
if (p1) return m;
275+
$a.href = url;
276+
$a.target = '_blank';
277+
$a.textContent = url;
278+
return $a.outerHTML;
279+
});
280+
};

0 commit comments

Comments
 (0)