Skip to content

Commit

Permalink
Add 'srcIsMediaSourceObjectURL' for media src blob url
Browse files Browse the repository at this point in the history
  • Loading branch information
sangwoo108 committed Dec 27, 2023
1 parent c043566 commit 30d27a5
Showing 1 changed file with 66 additions and 41 deletions.
107 changes: 66 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,49 @@

// This script is modified version of
// https://github.com/brave/brave-ios/blob/development/Client/Frontend/UserContent/UserScripts/Playlist.js
(function () {
medias = (async function () {

Check failure

Code scanning / ESLint

disallow the use of undeclared variables unless mentioned in `/*global */` comments Error

'medias' is not defined.
// This will be replaced by native code on demand.
const siteSpecificDetector = null

async function isMediaSourceObjectURL(src) {

Check failure

Code scanning / ESLint

enforce consistent spacing before `function` definition opening parenthesis Error

Missing space before function parentheses.
if (!src || !src.startsWith('blob:')) {
return false
}

const controller = new AbortController()
const signal = controller.signal
let timeout
const maybeAbortFetch = new Promise(resolve =>
timeout = setTimeout(() => resolve(false), 500)
)

return Promise.any([
new Promise(resolve => {
fetch(src, {
signal

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 10 spaces but found 12.
})

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 8 spaces but found 10.
.then(response => {
resolve(false)
})
.catch(error => {
resolve(true)
})

Check failure

Code scanning / ESLint

require error handling in callbacks Error

Expected error to be handled.
.finally(() => {
clearTimeout(timeout)
})
}),
maybeAbortFetch
])
}

function isHttpsScheme (url) {
if (!url || typeof url !== 'string') {
return false
}

if (url.startsWith('blob:'))
url = url.substring(5)

Check failure

Code scanning / ESLint

enforce consistent brace style for all control statements Error

Expected { after 'if' condition.

let isHttpsScheme = false
try {
// In case of http: or data: protocol, the base URL is not used
Expand All @@ -38,8 +72,9 @@
return url
}

function getNodeData (node) {
async function getNodeData (node) {
const src = fixUpUrl(node.src)
const srcIsMediaSourceObjectURL = await isMediaSourceObjectURL(src)
let mimeType = node.type
if (mimeType == null || typeof mimeType === 'undefined' || mimeType === '') {
if (node.constructor.name === 'HTMLVideoElement') {
Expand All @@ -58,51 +93,39 @@
}
}
}
const name = getMediaTitle(node)

if (src) {
return [{
'name': name,
const result = {
'name': getMediaTitle(node),

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 6 spaces but found 8.
src,

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 6 spaces but found 8.
srcIsMediaSourceObjectURL,

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 6 spaces but found 8.
'pageSrc': window.location.href,

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 6 spaces but found 8.
'pageTitle': document.title,

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 6 spaces but found 8.
'mimeType': mimeType,
mimeType,

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 6 spaces but found 8.
'duration': getMediaDurationInSeconds(node),

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 6 spaces but found 8.
'detected': true

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 6 spaces but found 8.
}]
} else {
const target = node
const sources = []
document.querySelectorAll('source').forEach(function (node) {
const src = fixUpUrl(node.src)
if (src) {
if (node.closest('video') === target) {
sources.push({
'name': name,
src,
'pageSrc': window.location.href,
'pageTitle': document.title,
'mimeType': mimeType,
'duration': getMediaDurationInSeconds(target),
'detected': true
})
}

if (node.closest('audio') === target) {
sources.push({
'name': name,
src,
'pageSrc': window.location.href,
'pageTitle': document.title,
'mimeType': mimeType,
'duration': getMediaDurationInSeconds(target),
'detected': true
})
}
}

Check failure

Code scanning / ESLint

enforce consistent indentation Error

Expected indentation of 4 spaces but found 6.

if (src) {
return [result]
}

const target = node
const sources = []
for (node of document.querySelectorAll('source')) {
const source = {...result}

Check failure

Code scanning / ESLint

enforce consistent spacing inside braces Error

A space is required after '{'.

Check failure

Code scanning / ESLint

enforce consistent spacing inside braces Error

A space is required before '}'.
source.src = fixUpUrl(node.src)
source.srcIsMediaSourceObjectURL = await isMediaSourceObjectURL(source.src)
if (source.src) {
if (node.closest('video') === target) {
sources.push(source)
}
})
return sources

if (node.closest('audio') === target) {
sources.push(source)
}
}
}
return sources
}

function getAllVideoElements () {
Expand Down Expand Up @@ -168,8 +191,10 @@
const author = getMediaAuthor()

let medias = []
videoElements.forEach(e => medias = medias.concat(getNodeData(e)))
audioElements.forEach(e => medias = medias.concat(getNodeData(e)))
for (e of [...videoElements, ...audioElements]) {

Check failure

Code scanning / ESLint

disallow the use of undeclared variables unless mentioned in `/*global */` comments Error

'e' is not defined.
const media = await getNodeData(e)

Check failure

Code scanning / ESLint

disallow the use of undeclared variables unless mentioned in `/*global */` comments Error

'e' is not defined.
medias = medias.concat(media)
}

if (medias.length) {
medias[0].thumbnail = thumbnail
Expand Down

0 comments on commit 30d27a5

Please sign in to comment.