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. 50
      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);
});

Loading…
Cancel
Save