Updated to use mimetype instead of file extension

Also fixed everything that Tom asked, i hope.
indiachan-spamvector
ussaohelcim 2 years ago
parent 184ff965c6
commit 2fcde2d7b9
  1. 38
      gulp/res/js/ptchina-playlist.js

@ -1,26 +1,26 @@
//https://github.com/ussaohelcim/ptchina-playlist/tree/bookmarklet-let //https://github.com/ussaohelcim/ptchina-playlist/tree/bookmarklet-let
'use strict';
async function threadToPlaylist(board, postId) { async function threadToPlaylist(board, postId) {
async function getThread() { async function getThread() {
let link = `${window.location.origin}/${board}/thread/${postId}.json`; let link = `${window.location.origin}/${board}/thread/${postId}.json`;
return await fetch(link).then(async (res) => { return await fetch(link).then(res => res.json());
const thread = await res.json(); }
return thread; function isAudioOrVideo(file) {
}); const mimeTypes = ['video', 'audio'];
const fileType = file.mimetype.split('/')[0];
return fileType === mimeTypes[0] || fileType === mimeTypes[1];
} }
async function getMedia(thread) { async function getMedia(thread) {
const fileTypes = ['.mp4', '.mp3', '.webm'];
const files = []; const files = [];
thread.files?.forEach((f) => { thread.files.forEach((file) => {
if (fileTypes.includes(f.extension)) { if (isAudioOrVideo(file)) {
files.push(f); files.push(file);
} }
}); });
for (let i = 0; i < thread.replies.length; i++) { for (let i = 0; i < thread.replies.length; i++) {
const element = thread.replies[i].files; const replyFiles = thread.replies[i].files;
element?.forEach((f) => { replyFiles.forEach((file) => {
if (fileTypes.includes(f.extension)) { if (isAudioOrVideo(file)) {
files.push(f); files.push(file);
} }
}); });
} }
@ -49,17 +49,21 @@ async function threadToPlaylist(board, postId) {
a.click(); a.click();
document.body.removeChild(a); document.body.removeChild(a);
} }
try {
const thread = await getThread(); const thread = await getThread();
const files = await getMedia(thread); const files = await getMedia(thread);
const playlist = await createPlaylist(files); const playlist = await createPlaylist(files);
if (playlist.split('\n').length > 1) { //playlist.split('\n').length === 1 means only "#EXTM3U" inside the string if (playlist.split('\n').length > 1) {
downloadPlaylist(`${thread.board}-${thread.postId}.m3u`, playlist); downloadPlaylist(`${thread.board}-${thread.postId}.m3u`, playlist);
} }
else { else {
console.log('No video/audio files in this thread.'); console.log('No video/audio files in this thread.');
} }
}
catch (error) {
console.log(error);
}
} }
window.addEventListener('createPlaylist', (e) => {
window.addEventListener('createPlaylist',(e)=>{ threadToPlaylist(e.detail.board, e.detail.postId);
threadToPlaylist(e.detail.board,e.detail.postId);
}); });

Loading…
Cancel
Save