// ==UserScript==
// @name Nico dl
// @namespace http://tampermonkey.net/
// @version 0.4
// @description 下载N站视频 | Download video from nicovideo.jp
// @author ctrn43062
// @include *//www.nicovideo.jp/watch/sm*
// @icon https://www.google.com/s2/favicons?domain=nicovideo.jp
// @grant none
// @note 2023-2-20 [fix] 修复打开视频封面失效的问题 (年了,一更)
// @note 2022-2-17 [fix] 修复右键打开视频时可能发生的无法下载视频和视频封面的 bug
// @note 2022-2-10 [feat] 添加下载视频高清封面功能
// @license MIT
// @downloadURL https://update.greasyfork.icu/scripts/439692/Nico%20dl.user.js
// @updateURL https://update.greasyfork.icu/scripts/439692/Nico%20dl.meta.js
// ==/UserScript==
const IS_HLS_DISABLED = 'DMCSource.isHLSDisabled';
const DOWNLOAD_SVG = ``
const COVER_SVG = ``
function createButton(svg, title, disabled = false) {
const button = document.createElement('button');
button.className = 'ActionButton ControllerButton PlayerRepeatOnButton';
button.innerHTML = svg;
button.setAttribute('data-title', title);
disabled && button.setAttribute('disabled', disabled);
return button;
}
function insertButtonToVideoControllBar(button) {
const playbackRateButton = document.querySelector('.ActionButton.PlaybackRateButton');
const wrapper = playbackRateButton.parentElement;
wrapper.insertBefore(button, playbackRateButton);
}
function createDownloadVideoButton() {
const downloadButton = createButton(DOWNLOAD_SVG, '下载视频', true);
insertButtonToVideoControllBar(downloadButton);
return {
setSrc: function (src) {
downloadButton.removeAttribute('disabled');
downloadButton.onclick = function () {
window.open(src)
};
},
disable: function () {
downloadButton.setAttribute('disabled', true);
}
}
}
function createDownloadCoverButton() {
const button = createButton(COVER_SVG, '下载封面');
const thumbnailSizes = ['ogp', 'largeUrl', 'middleUrl', 'url', 'player',]
insertButtonToVideoControllBar(button);
const getCoverURL = () => {
debugger
const apiDOM = document.querySelector('#js-initial-watch-data')
const thumbnail = JSON.parse(apiDOM.getAttribute('data-api-data'))['video']['thumbnail']
for (const size of thumbnailSizes) {
const url = thumbnail[size]
if (url) {
return url
}
}
}
const updateCoverURL = function () {
const cover_url = getCoverURL();
button.removeAttribute('disabled');
button.onclick = () => {
open(cover_url);
}
return cover_url;
}
return {
updateCoverURL: updateCoverURL,
disable: function () {
button.setAttribute('disabled', true);
}
}
}
(function () {
'use strict';
const isHttp = localStorage.getItem(IS_HLS_DISABLED);
if (isHttp === null || isHttp === 'false') {
localStorage.setItem(IS_HLS_DISABLED, 'true');
location.reload();
}
const downloadCoverButton = createDownloadCoverButton();
const downloadVideoButton = createDownloadVideoButton();
// 循环判断 cover_url 是否存在
const updateCoverURL = () => {
const cover_url = downloadCoverButton.updateCoverURL();
if (!cover_url) {
setTimeout(updateCoverURL, 10);
}
}
const observer = new MutationObserver(mutationsList => {
for (let mutation of mutationsList) {
const target = mutation.target;
if (mutation.attributeName === 'src') {
if (target.src) {
downloadVideoButton.setSrc(target.src);
updateCoverURL();
} else {
downloadVideoButton.disable();
downloadCoverButton.disable();
}
}
}
});
const bindVideoObserver = () => {
const video = document.querySelector('#MainVideoPlayer > video');
if (video === null) {
setTimeout(bindVideoObserver, 10);
} else {
observer.observe(video, {
attributes: true
});
}
}
bindVideoObserver();
})();