Adds a download buttons to ThisVid video pages.
当前为
// ==UserScript==
// @name ThisVid Download Button
// @namespace https://thisvid.com/
// @version 1.0.0
// @description Adds a download buttons to ThisVid video pages.
// @author persistentScripter
// @include http*://thisvid.com/*
// @run-at document-end
// ==/UserScript==
const snakeCase = (str) => {
str = str.replace(/\W+/g, ' ').toLowerCase().split(' ').join('_');
return str;
};
// Downloads a video via fetch() so we can save the file with the appropriate file name.
let getVid = async (fileURL) => {
let controller = new AbortController();
let {signal} = controller;
const {title} = document;
document.title = `[↓] ${title.replace(/\[↓\]/g, '')}`;
let fileName = snakeCase(title.replace(/ThisVid\.com|at ThisVid tube/, '').trim());
let resp = await fetch(fileURL, {
method: 'GET',
redirect: 'follow',
signal,
});
// First request will just be a redirect. Catch it, abort, and continue.
if (resp.redirected) {
controller.abort();
controller = new AbortController();
signal = controller.signal;
resp = await fetch(resp.url, {
method: 'GET',
signal,
});
}
let blob = await resp.blob();
try {
url = window.URL.createObjectURL(blob);
} catch (e) {
url = resp.url;
return url;
}
const a = document.createElement('a');
document.title = `[✓] ${title.replace(/\[✓\]/g, '')}`;
a.style.display = 'none';
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
return true;
};
window.addEventListener('load', () => {
let flagContainer = document.querySelector('#flagging_container');
if (!flagContainer) return;
let fileURL = document.querySelector('video').src;
let li = document.createElement('li');
let a = document.createElement('a');
let span = document.createElement('span');
li.classList.add('share_button');
li.appendChild(a);
a.classList.add('__dl');
a.href = fileURL;
Object.assign(span.style, {
color: '#fff',
fontSize: '32px',
position: 'absolute',
});
a.innerHTML += `
<span class="tooltip">download</span>
`;
span.innerText = '↓';
a.appendChild(span);
a.addEventListener('click', async (e) => {
e.preventDefault();
e.stopPropagation();
try {
await getVid(fileURL);
} catch (e) {
let {title} = document;
document.title = `[✗] ${title.replace(/\[✗\]/g, '')}`;
window.open(fileURL);
}
});
flagContainer.appendChild(li);
});