Last active
March 20, 2023 00:23
-
-
Save m417z/3c6016b0c9d42b3a8692874d28954786 to your computer and use it in GitHub Desktop.
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 Winbindex download file name helper | |
// @namespace https://winbindex.m417z.com/ | |
// @version 0.1.1 | |
// @description Gives sane names to downloaded files (https://github.com/m417z/winbindex/issues/1) | |
// @author m417z | |
// @match https://winbindex.m417z.com/* | |
// @match https://msdl.microsoft.com/?* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=m417z.com | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// https://stackoverflow.com/a/64123890 | |
async function download(url, cb) { | |
const elProgress = document.getElementById('progress'); | |
function progress({loaded, total}) { | |
const p = Math.round(loaded/total*100)+'%'; | |
elProgress.innerHTML = p; | |
document.title = `[${p}] ` + document.title.replace(/\[\d+%\] /, ''); | |
} | |
const response = await fetch(url); | |
if (!response.ok) { | |
throw new Error(`Server error: ${response.status}`); | |
} | |
const contentLength = response.headers.get('content-length'); | |
const total = parseInt(contentLength, 10); | |
let loaded = 0; | |
const res = new Response(new ReadableStream({ | |
async start(controller) { | |
const reader = response.body.getReader(); | |
for (;;) { | |
const {done, value} = await reader.read(); | |
if (done) break; | |
loaded += value.byteLength; | |
progress({loaded, total}); | |
controller.enqueue(value); | |
} | |
controller.close(); | |
}, | |
})); | |
return await res.blob(); | |
} | |
// https://stackoverflow.com/a/32261263 | |
function popupWindow(url, windowName, win, w, h) { | |
const y = win.top.outerHeight / 2 + win.top.screenY - (h / 2); | |
const x = win.top.outerWidth / 2 + win.top.screenX - (w / 2); | |
return win.open(url, windowName, `toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${w}, height=${h}, top=${y}, left=${x}`); | |
} | |
if (window.location.hostname === 'msdl.microsoft.com') { | |
const params = new URLSearchParams(window.location.search); | |
const path = params.get('path'); | |
const name = params.get('name'); | |
if (path && name) { | |
document.write(` | |
<h2>Downloading <span id="name"></span>...</h2> | |
<h3 id="progress"></h3> | |
`); | |
document.getElementById('name').textContent = name; | |
document.title = name + ' - downloading...'; | |
download(path).then(blob => { | |
const downloadLink = document.createElement('a'); | |
downloadLink.href = window.URL.createObjectURL(blob); | |
downloadLink.download = name; | |
document.body.appendChild(downloadLink); | |
downloadLink.click(); | |
window.close(); | |
}).catch(error => { | |
document.getElementById('progress').textContent = `🛑 ${error}`; | |
document.title = `[Error] ${document.title}`; | |
}); | |
} | |
return; | |
} | |
if (window.location.hostname === 'winbindex.m417z.com') { | |
document.addEventListener('click', function (e) { | |
if (e.target.tagName === 'A' && e.target.href?.startsWith('https://msdl.microsoft.com/download/symbols/')) { | |
const columns = e.target.parentElement.parentElement.children; | |
const arch = columns[3].textContent; | |
const version = columns[4].textContent; | |
const path = new URL(e.target.href).pathname; | |
const original_filename = e.target.href.replace(/^.*\//, ''); | |
const name = original_filename.replace(/\./, `_${version}_${arch}.`); | |
popupWindow( | |
'https://msdl.microsoft.com/?' + new URLSearchParams({ path, name }), | |
'_blank', | |
window, | |
400, | |
200 | |
); | |
e.stopPropagation(); | |
e.preventDefault(); | |
} | |
}, true); | |
return; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment