Skip to content

Commit

Permalink
Address code review
Browse files Browse the repository at this point in the history
  • Loading branch information
sangwoo108 committed Jan 3, 2024
1 parent 1f2c652 commit 0fbfa71
Showing 1 changed file with 78 additions and 26 deletions.
104 changes: 78 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ medias = (async function () {
// This will be replaced by native code on demand.
const siteSpecificDetector = null

/**
* Returns a Promise that resolves with a boolean argument indicating whether

Check failure

Code scanning / ESLint

disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
* the `src` URL could be a blob pointing at a MediaSource object.
* @param {string} src
* @returns {Promise}
*/
async function isMediaSourceObjectURL (src) {
if (!src || !src.startsWith('blob:')) {
return false
Expand All @@ -18,25 +24,27 @@ medias = (async function () {
const signal = controller.signal
let timeout

Check failure

Code scanning / ESLint

disallow unused variables Error

'timeout' is assigned a value but never used.
const maybeAbortFetch = new Promise(resolve =>
timeout = setTimeout(() => resolve(false), 500)
timeout = setTimeout(() => {

Check failure

Code scanning / ESLint

disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
resolve(false)

Check failure

Code scanning / ESLint

disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
controller.abort()
}, 500)
)

return Promise.any([
new Promise(resolve => {
fetch(src, {
signal
}).then(response => {
resolve(false)
}).catch(() => {
resolve(true)
}).finally(() => {
clearTimeout(timeout)
})
fetch(src, { signal })
.then(() => false)

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 10 spaces but found 8.
.catch(() => true)

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 10 spaces but found 8.
}),
maybeAbortFetch
])
}

/**
* Returns whether given `url` has https protocol.
* @param {string} url
* @returns {boolean}
*/
function isHttpsScheme (url) {
if (!url || typeof url !== 'string') {
return false
Expand All @@ -57,19 +65,52 @@ medias = (async function () {
return isHttpsScheme
}

/**
* Returns absolute path of given `url`. Note that returns null if it's not
* url nor https scheme.
* @param {string} url
* @returns {?string}
*/
function fixUpUrl (url) {
if (!isHttpsScheme(url)) {
return null
}

if (!url.startsWith('https://')) {
// Fix up relative path to absolute path
url = new URL(url, window.location.origin).href
url = new URL(url, window.location).href
}

return url
}

/**
* MediaItem will be parsed into C++ object representing PlaylistItem in

Check failure

Code scanning / ESLint

disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
* {
"detected": boolean,
"mimeType": "video" | "audio",
"name": string,
"pageSrc": url,
"pageTitle": string
"src": url
"thumbnail": url | undefined
}
* @typedef MediaItem
* @type {object}
* @property {string} name
* @property {"video" | "audio"} mimeType
* @property {string} pageSrc - page url
* @property {string} pageTitle - page title
* @property {string} src - media url
* @property {?string} thumbnail - thumbnail url
* @property {boolean} detected
*/

/**
* Get all media items(video or audio) from the given HTMLMediaElement `node`.

Check failure

Code scanning / ESLint

disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
* @param {HTMLMediaElement} node
* @returns {MediaItem[]}
*/
async function getNodeData (node) {
const src = fixUpUrl(node.src)
const srcIsMediaSourceObjectURL = await isMediaSourceObjectURL(src)
Expand All @@ -93,14 +134,14 @@ medias = (async function () {
}

const result = {
'name': getMediaTitle(node),
name: getMediaTitle(node),
src,
srcIsMediaSourceObjectURL,
'pageSrc': window.location.href,
'pageTitle': document.title,
pageSrc: window.location.href,
pageTitle: document.title,
mimeType,
'duration': getMediaDurationInSeconds(node),
'detected': true
duration: getMediaDurationInSeconds(node),
detected: true
}

if (src) {
Expand All @@ -126,14 +167,10 @@ medias = (async function () {
return sources
}

function getAllVideoElements () {
return document.querySelectorAll('video')
}

function getAllAudioElements () {
return document.querySelectorAll('audio')
}

/**
* Returns thumbnail url from this page.
* @returns {?url}
*/
function getThumbnail () {
const isThumbnailValid = (thumbnail) => { return thumbnail && thumbnail !== '' }

Expand All @@ -145,6 +182,11 @@ medias = (async function () {
return fixUpUrl(thumbnail)
}

/**
* Returns title of media `node`
* @param {HTMLMediaElement} node
* @returns {?string}
*/
function getMediaTitle (node) {
const isTitleValid = (title) => { return title && title !== '' }

Expand All @@ -158,6 +200,11 @@ medias = (async function () {
return title
}

/**
* Returns the author of given media `node`
* @param {HTMLMediaElement} node
* @returns {?string}
*/
function getMediaAuthor (node) {
// TODO(sko) Get metadata of author in more general way
let author = null
Expand All @@ -167,6 +214,11 @@ medias = (async function () {
return author
}

/**
* Returns duration of given media `node` in seconds

Check failure

Code scanning / ESLint

disallow trailing whitespace at the end of lines Error

Trailing spaces not allowed.
* @param {HTMLMediaElement} node
* @returns {number}
*/
function getMediaDurationInSeconds (node) {
const clampDuration = (value) => {
if (Number.isFinite(value) && value >= 0) return value
Expand All @@ -181,8 +233,8 @@ medias = (async function () {
return clampDuration(duration)
}

const videoElements = getAllVideoElements() ?? []
const audioElements = getAllAudioElements() ?? []
const videoElements = document.querySelectorAll('video') ?? []
const audioElements = document.querySelectorAll('audio') ?? []
// TODO(sko) These data could be incorrect when there're multiple items.
// For now we're assuming that the first media is a representative one.
const thumbnail = getThumbnail()
Expand Down

0 comments on commit 0fbfa71

Please sign in to comment.