抖音网页版视频下载
// ==UserScript==
// @name 抖音网页版视频下载
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.2.0
// @description 抖音网页版视频添加下载按钮
// @author 昊色居士
// @match *://www.douyin.com/video/*
// ==/UserScript==
(function () {
'use strict';
function getVideoUrl() {
let video_url = document.querySelector(".xg-video-container video>source").getAttribute("src");
video_url = video_url.startsWith("//") ? "https:" + video_url : video_url;
return video_url
}
function getVideoName() {
const filename = document.querySelector("title").innerText;
return `${filename}.mp4`
}
function download(url) {
return fetch(url, {
headers: new Headers({
Origin: location.origin,
}),
mode: 'cors',
}).then(res => {
return res.blob()
})
}
function main() {
let videoUrl = getVideoUrl()
let videoName = getVideoName()
let douyinHeader = document.querySelector("#douyin-header-menuCt pace-island > div")
let copyDom = douyinHeader.childNodes[1]
let dlDom = copyDom.cloneNode(true);
let dlA = dlDom.querySelector("a")
let dlBtn = dlA.querySelector("p")
dlBtn.textContent = "下视频"
download(videoUrl).then(blob => {
const blobUrl = URL.createObjectURL(blob)
dlA.download = videoName
dlA.href = blobUrl
})
douyinHeader.insertBefore(dlDom, copyDom)
}
// 等待网页完成加载
window.addEventListener('load', function () {
const intervalId = setInterval(() => {
let dom1 = document.querySelector(".xg-video-container video>source")
let dom2 = document.querySelectorAll("#douyin-header-menuCt pace-island > div > div")[1].querySelector("p")
if (dom1 && dom2 && dom2.textContent === "客户端") {
clearInterval(intervalId)
main()
}
}, 500)
}, false);
})();