// ==UserScript==
// @name 度盘文件列表、分享页面显示MD5
// @author Crab
// @namespace pan@baidu.com
// @description 百度网盘文件列表、分享页面显示分享文件的MD5值。
// @include /^https?://(yun|pan)\.baidu\.com\/(s(hare)?|disk)\/*/
// @compatible firefox 34+
// @compatible Chrome 45+
// @version 0.1.1
// @grant none
// @downloadURL none
// ==/UserScript==
(function(){
'use strict';
const {cache, yunData} = window;
//单文件分享页MD5
if(yunData && yunData.FILEINFO && yunData.FILEINFO.length === 1 &&
yunData.FILEINFO[0].isdir !== 1 && yunData.FILEINFO[0].md5
){
let fTitle = document.querySelector('h2.file-name');
if(fTitle){
fTitle.parentNode.insertAdjacentHTML('beforeend',
`
- MD5:
- ${yunData.FILEINFO[0].md5}
`);
}
}
//文件列表MD5
if(!cache || !cache.list || !cache.list.data){
return;
}
document.head.appendChild(document.createElement('style')).textContent = `
@keyframes bdFileMd5 {from{opacity:.9}to{opacity:1}}
:not(.dir-small):not(.dir-large)
+ .file-name .text>a:not([data-md5]){animation:bdFileMd5 1ms}
.file-name .text>a[data-md5]{position:relative; top: -6px;}
[class*=fileicon] + .file-name .text>a[data-md5]::after,
.default-small + .file-name .text>a[data-md5]::after{
position:absolute; content:'MD5: 'attr(data-md5);
left: -80px; top: 2px; font-size: 95%; color: #aaa;
}
`;
const dirSelector = '.FuIxtL>li:last-of-type, .module-toolbar+div>div>ul>li:last-of-type',
div = document.createElement('div'),
base = '/';
let timeout, dir;
addEventListener('animationstart', e => {
if(e.animationName !== 'bdFileMd5')
return;
clearTimeout(timeout);
timeout = setTimeout(() => {dir = null}, 1000);
if(!dir){
dir = document.querySelector(dirSelector);
if(!dir) return;
dir = dir.offsetParent ? dir.lastElementChild.title.substr(4) : base;
if(cache.list.data[base]){
let bPath = cache.list.data[base].list;
if(bPath && bPath.length === 1 && bPath[0].isdir === 1){
let p = bPath[0].path.split('/');
if(p.length > 1){
if(p[0] !== '') p.unshift('');
p.pop();
dir = p.join('/') + dir;
}
}
}
}
if(!cache.list.data[dir] || !cache.list.data[dir].list)
return;
let targetName = e.target.textContent,
fileInfo = cache.list.data[dir].list.find(file => {
div.innerHTML = file.path.split('/').pop();
return div.textContent === targetName;
});
if(fileInfo){
e.target.dataset.md5 = fileInfo.md5;
}
});
})();