Created
February 26, 2020 03:07
-
-
Save Last-Order/0d7a773dafda0558e6d212116acbbd29 to your computer and use it in GitHub Desktop.
Use Mediainfo.js for Biu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Use Mediainfo.js for Biu | |
// @namespace Violentmonkey Scripts | |
// @match https://web.biu.moe/Upload | |
// @grant none | |
// @version 1.0 | |
// @author - | |
// @description 2020/2/25 下午9:57:36 | |
// ==/UserScript== | |
(() => { | |
const CHUNK_SIZE = 5 * 1024 * 1024; | |
const mediainfoScriptNode = document.createElement("script"); | |
mediainfoScriptNode.src = "https://hello-happy-1251149766.cos.ap-beijing.myqcloud.com/mediainfo.js"; | |
document.querySelector('head').appendChild(mediainfoScriptNode); | |
mediainfoScriptNode.addEventListener("load", () => { | |
let processing; | |
console.log('Mediainfo.js loaded'); | |
const miLib = MediaInfo(() => { | |
console.log('Mediainfo.js ready'); | |
mi = new miLib.MediaInfo(); | |
function parseFile(file) { | |
return new Promise((resolve) => { | |
if (processing) { | |
return; | |
} | |
processing = true; | |
var fileSize = file.size, offset = 0, state = 0, seekTo = -1, seek = null; | |
mi.open_buffer_init(fileSize, offset); | |
var processChunk = function (e) { | |
var l; | |
if (e.target.error === null) { | |
var chunk = new Uint8Array(e.target.result); | |
l = chunk.length; | |
state = mi.open_buffer_continue(chunk, l); | |
var seekTo = -1; | |
var seekToLow = mi.open_buffer_continue_goto_get_lower(); | |
var seekToHigh = mi.open_buffer_continue_goto_get_upper(); | |
if (seekToLow == -1 && seekToHigh == -1) { | |
seekTo = -1; | |
} else if (seekToLow < 0) { | |
seekTo = seekToLow + 4294967296 + (seekToHigh * 4294967296); | |
} else { | |
seekTo = seekToLow + (seekToHigh * 4294967296); | |
} | |
if (seekTo === -1) { | |
offset += l; | |
} else { | |
offset = seekTo; | |
mi.open_buffer_init(fileSize, seekTo); | |
} | |
chunk = null; | |
} else { | |
var msg = 'An error happened reading your file!'; | |
console.err(msg, e.target.error); | |
processingDone(); | |
alert(msg); | |
return; | |
} | |
// bit 4 set means finalized | |
if (state & 0x08) { | |
var result = mi.inform(); | |
mi.close(); | |
resolve(result); | |
processingDone(); | |
return; | |
} | |
seek(l); | |
}; | |
function processingDone() { | |
processing = false; | |
} | |
seek = function (length) { | |
if (processing) { | |
var r = new FileReader(); | |
var blob = file.slice(offset, length + offset); | |
r.onload = processChunk; | |
r.readAsArrayBuffer(blob); | |
} | |
else { | |
mi.close(); | |
processingDone(); | |
} | |
}; | |
// start | |
seek(CHUNK_SIZE); | |
}) | |
} | |
const getTag = (text, tagName) => { | |
const regexp = new RegExp(`<${tagName}>(.+)<\/${tagName}>`, 'i'); | |
if (text.match(regexp)) { | |
return text.match(regexp)[1].trim(); | |
} | |
return undefined; | |
} | |
// 替换函数 | |
const _go2 = unsafeWindow.go2; | |
unsafeWindow.go2 = async (file) => { | |
$('#nowfile').text(file.name); | |
$('#info11').slideDown(); | |
const r = await parseFile(file); | |
$('#info11').slideUp(); | |
$('#info2').slideDown(); | |
$('#info1').append(' 格式:' + getTag(r, 'format')); | |
$('#info1').append(' 码率:' + getTag(r, 'bit_rate')); | |
$('#title').attr('value', getTag(r, 'track_name')); | |
$('#singer').attr('value', getTag(r, 'performer') || getTag(r, 'display_artist') || getTag(r, 'albumartist')); | |
$('#album').attr('value', getTag(r, 'album')); | |
}; | |
}); | |
}) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment