Skip to content

Instantly share code, notes, and snippets.

@spacemeowx2
Created September 19, 2017 08:40
Show Gist options
  • Save spacemeowx2/00bc4e3eef955b778e53ab00a1095ad8 to your computer and use it in GitHub Desktop.
Save spacemeowx2/00bc4e3eef955b778e53ab00a1095ad8 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Stream Test
// @namespace http://imspace.cn/gms
// @match *://*/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
function getBodyChecker () {
let c = 0;
let ret = function (buffer) {
const u8 = new Uint8Array(buffer)
for (let i of u8) {
const e = c & 0xFF
if (i !== e) {
throw new Error(`Wrong, expecting ${e}, but got ${i}`)
}
c++
}
ret.count = c
}
ret.count = 0
return ret
}
function initUI () {
let start = false
const div = document.createElement('div')
const btn = document.createElement('button')
const progress = document.createElement('progress')
btn.addEventListener('click', () => {
if (start) return
startTest((p) => progress.value = p)
start = true
})
btn.innerText = 'Start Test'
progress.value = 0
progress.max = 100
div.appendChild(btn)
div.appendChild(progress)
div.style.position = 'fixed'
div.style.top = '0'
div.style.left = '0'
document.body.appendChild(div)
}
function startTest (cb) {
const checker = getBodyChecker()
const stop = 1024 * 1024
let alerted = false
let conf = {}
let xhr
conf.method = 'GET'
conf.url = 'https://github.com/spacemeowx2/100mb/raw/master/100mb.bin'
conf.responseType = 'moz-chunked-arraybuffer'
conf.onprogress = ({response}) => {
try {
checker(response)
} catch (e) {
xhr.abort()
if (!alerted) {
alert('Wrong data')
alerted = true
}
}
cb(checker.count / stop * 100)
if (checker.count > stop) {
xhr.abort()
if (!alerted) {
alert('Support!')
alerted = true
}
}
}
xhr = GM_xmlhttpRequest(conf)
}
initUI()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment