From 2fcde2d7b9bee8cd53a705202280ed749f52ca9c Mon Sep 17 00:00:00 2001 From: ussaohelcim <57050328+ussaohelcim@users.noreply.github.com> Date: Mon, 4 Jul 2022 16:04:31 -0300 Subject: [PATCH] Updated to use mimetype instead of file extension Also fixed everything that Tom asked, i hope. --- gulp/res/js/ptchina-playlist.js | 50 ++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/gulp/res/js/ptchina-playlist.js b/gulp/res/js/ptchina-playlist.js index ba92f9eb..e38cdbd5 100644 --- a/gulp/res/js/ptchina-playlist.js +++ b/gulp/res/js/ptchina-playlist.js @@ -1,26 +1,26 @@ //https://github.com/ussaohelcim/ptchina-playlist/tree/bookmarklet-let -'use strict'; async function threadToPlaylist(board, postId) { async function getThread() { let link = `${window.location.origin}/${board}/thread/${postId}.json`; - return await fetch(link).then(async (res) => { - const thread = await res.json(); - return thread; - }); + return await fetch(link).then(res => res.json()); + } + function isAudioOrVideo(file) { + const mimeTypes = ['video', 'audio']; + const fileType = file.mimetype.split('/')[0]; + return fileType === mimeTypes[0] || fileType === mimeTypes[1]; } async function getMedia(thread) { - const fileTypes = ['.mp4', '.mp3', '.webm']; const files = []; - thread.files?.forEach((f) => { - if (fileTypes.includes(f.extension)) { - files.push(f); + thread.files.forEach((file) => { + if (isAudioOrVideo(file)) { + files.push(file); } }); for (let i = 0; i < thread.replies.length; i++) { - const element = thread.replies[i].files; - element?.forEach((f) => { - if (fileTypes.includes(f.extension)) { - files.push(f); + const replyFiles = thread.replies[i].files; + replyFiles.forEach((file) => { + if (isAudioOrVideo(file)) { + files.push(file); } }); } @@ -49,17 +49,21 @@ async function threadToPlaylist(board, postId) { a.click(); document.body.removeChild(a); } - const thread = await getThread(); - const files = await getMedia(thread); - const playlist = await createPlaylist(files); - if (playlist.split('\n').length > 1) { //playlist.split('\n').length === 1 means only "#EXTM3U" inside the string - downloadPlaylist(`${thread.board}-${thread.postId}.m3u`, playlist); + try { + const thread = await getThread(); + const files = await getMedia(thread); + const playlist = await createPlaylist(files); + if (playlist.split('\n').length > 1) { + downloadPlaylist(`${thread.board}-${thread.postId}.m3u`, playlist); + } + else { + console.log('No video/audio files in this thread.'); + } } - else { - console.log('No video/audio files in this thread.'); + catch (error) { + console.log(error); } } - -window.addEventListener('createPlaylist',(e)=>{ - threadToPlaylist(e.detail.board,e.detail.postId); +window.addEventListener('createPlaylist', (e) => { + threadToPlaylist(e.detail.board, e.detail.postId); });