// ==UserScript== // @name 阿里云盘标清替换成最高画质 // @namespace http://tampermonkey.net/ // @version 1.0.5.2 // @description 通过阿里云盘开放平台将原标清替换成最高画质(支持分享链接),打开视频自动播放,视频前进后退5秒,更多倍速播放以及记忆倍速,视频下载,播放器工具栏置底并设置透明度,快捷键:home上一集,end下一集,enter全屏 // @author bygavin // @match https://www.aliyundrive.com/* // @match https://www.alipan.com/* // @icon https://img.alicdn.com/imgextra/i1/O1CN01JDQCi21Dc8EfbRwvF_!!6000000000236-73-tps-64-64.ico // @license MIT // @grant unsafeWindow // @grant GM_setValue // @grant GM_getValue // @downloadURL none // ==/UserScript== // 将localStorage内容转移到油猴脚本,以便两个域名共用,下个版本移除该段代码 ['openapitoken', 'playbackRate', 'newurls', 'saveinfo', 'openapiclient_id'].forEach((el) => { const key = el.toString() const elvalue = localStorage.getItem(key) localStorage.removeItem(key) if (elvalue) { GM_setValue(key, elvalue) } }) let openapiclient_id = GM_getValue('openapiclient_id') || '55091393987b4cc090b090ee17e85e0a'//该client_id是官方文档出现的,测试可用,也可自行申请替换 const openapicode_verifier = generateUUID(12, '-') var newurl, sdlurl, isfullscreen, time, sinfo var device_list = {}; var oldxhr = unsafeWindow.XMLHttpRequest function newobj() { } if (openapiclient_id) { //劫持xhr unsafeWindow.XMLHttpRequest = function () { let tagetobk = new newobj(); tagetobk.oldxhr = new oldxhr(); let handle = { get: function (target, prop, receiver) { if (prop === 'oldxhr') { return Reflect.get(target, prop); } if (typeof Reflect.get(target.oldxhr, prop) === 'function') { if (Reflect.get(target.oldxhr, prop + 'proxy') === undefined) { target.oldxhr[prop + 'proxy'] = new Proxy(Reflect.get(target.oldxhr, prop), { apply: function (target, thisArg, argumentsList) { return Reflect.apply(target, thisArg.oldxhr, argumentsList); } }); } return Reflect.get(target.oldxhr, prop + 'proxy') } const responseURL = target.oldxhr.responseURL if (responseURL.indexOf("/file/get_video_preview_play_info_by_share") > 0 && prop.indexOf('response') !== -1) {//播放分享链接 const response = target.oldxhr?.response || target.oldxhr?.responseText const share_token = target.oldxhr.__reqCtx__.headers["x-share-token"] var shareinfo = JSON.parse(response); sinfo = { share_token: share_token, file_id: shareinfo.file_id } sdlurl = '' newurl = getsetnewurl(shareinfo.file_id) if (newurl) { if (newurl.length > 1) { sdlurl = newurl[1] } newurl = newurl[0] } else { const saveinfo = savefile(shareinfo.file_id, share_token).responses[0].body newurl = getVideoPreviewPlayInfo(saveinfo.drive_id, saveinfo.file_id) deletefile(saveinfo.file_id) newurl && getsetnewurl(shareinfo.file_id, [newurl]) } newurl && (shareinfo.video_preview_play_info.live_transcoding_task_list[1].url = newurl) shareinfo = JSON.stringify(shareinfo); if (newurl) return shareinfo; } else if (responseURL.indexOf("/file/get_video_preview_play_info") > 0 && prop.indexOf('response') !== -1) {//播放自己云盘内容 const response = target.oldxhr?.response || target.oldxhr?.responseText var res = JSON.parse(response); sdlurl = '' newurl = getsetnewurl(res.file_id) if (newurl) { if (newurl.length > 1) { sdlurl = newurl[1] } newurl = newurl[0] } else { newurl = getVideoPreviewPlayInfo(res.drive_id, res.file_id) newurl && getsetnewurl(res.file_id, [newurl]) } newurl && (res.video_preview_play_info.live_transcoding_task_list[0].url = newurl) res = JSON.stringify(res); if (newurl) return res; } else if (responseURL.indexOf("get_video_preview_play_info") > 0 && prop === "statusText") {//预览链接加载完成 morerate(); } else if (responseURL.endsWith("/user/get") && prop.indexOf('response') !== -1) {//获取自己云盘的设备id const response = target.oldxhr?.response || target.oldxhr?.responseText const res = JSON.parse(response) device_list.resource_drive_id = res.resource_drive_id device_list.backup_drive_id = res.backup_drive_id } return Reflect.get(target.oldxhr, prop); }, set(target, prop, value) { return Reflect.set(target.oldxhr, prop, value); }, has(target, key) { return Reflect.has(target.oldxhr, key); } } let ret = new Proxy(tagetobk, handle); return ret; } } //通过阿里云盘开放平台获取视频信息 function getVideoPreviewPlayInfo(drive_id, file_id) { const access_token = getopenapitoken(); var xhr = new oldxhr(); xhr.open("POST", "https://openapi.aliyundrive.com/adrive/v1.0/openFile/getVideoPreviewPlayInfo", false); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.setRequestHeader("Authorization", access_token); xhr.send(JSON.stringify({ "drive_id": drive_id, "file_id": file_id, "category": "live_transcoding", "url_expire_sec": 14400 })); if (xhr.status === 200) { return (JSON.parse(xhr.responseText)).video_preview_play_info?.live_transcoding_task_list.pop().url; } else { return (xhr.statusText); } } //通过阿里云盘开放平台获取视频下载链接 function getDownloadUrl(drive_id, file_id) { const access_token = getopenapitoken(); var xhr = new oldxhr(); xhr.open("POST", "https://openapi.aliyundrive.com/adrive/v1.0/openFile/getDownloadUrl", false); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.setRequestHeader("Authorization", access_token); xhr.send(JSON.stringify({ "drive_id": drive_id, "file_id": file_id, "expire_sec": 14400 })); if (xhr.status === 200) { return (JSON.parse(xhr.responseText)).url; } else { return (xhr.statusText); } } //转存文件到备份盘的根目录 function savefile(sharefileid, share_token) { const token = JSON.parse(localStorage.getItem('token')) const getsaveinfo = GM_getValue('saveinfo'); let saveinfo = getsaveinfo || {} if (getsaveinfo && typeof getsaveinfo === "string") { saveinfo = JSON.parse(getsaveinfo) GM_setValue('saveinfo', saveinfo) } const shareid = location.pathname.split('/')[2] const savedata = { "requests": [ { "body": { "file_id": sharefileid, "share_id": shareid, "auto_rename": true, "to_parent_file_id": saveinfo?.savefile_id || 'root', "to_drive_id": saveinfo?.savedevice_id || token.default_drive_id }, "headers": { "Content-Type": "application/json" }, "id": "0", "method": "POST", "url": "/file/copy" } ], "resource": "file" } var xhr = new oldxhr(); xhr.open("POST", "https://api.aliyundrive.com/adrive/v3/batch", false); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.setRequestHeader("Authorization", token.access_token); xhr.setRequestHeader("X-Share-Token", share_token); xhr.send(JSON.stringify(savedata)); if (xhr.status === 200) { return (JSON.parse(xhr.responseText)); } else { return (xhr.statusText); } } //删除刚刚转存的文件 function deletefile(file_id) { const token = JSON.parse(localStorage.getItem('token')) const saveinfo = GM_getValue('saveinfo') || {} const deleteinfo = { "requests": [ { "body": { "drive_id": saveinfo?.savedevice_id || token.default_drive_id, "file_id": file_id }, "headers": { "Content-Type": "application/json" }, "id": file_id, "method": "POST", "url": "/file/delete" } ], "resource": "file" } var xhr = new oldxhr(); xhr.open("POST", "https://api.aliyundrive.com/adrive/v3/batch", false); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.setRequestHeader("Authorization", token.access_token); xhr.send(JSON.stringify(deleteinfo)); if (xhr.status === 200) { return (JSON.parse(xhr.responseText)); } else { return (xhr.statusText); } } //获取阿里云盘开放平台code function getopenapicode() { const accesstk = JSON.parse(localStorage.getItem('token')).access_token var xhr = new oldxhr(); var url = 'https://open.aliyundrive.com/oauth/users/authorize'; var data = { scope: 'user:base,file:all:read,file:all:write', authorize: 1, drives: ['backup', 'resource'] }; var params = '?client_id=' + openapiclient_id + '&redirect_uri=oob&scope=user:base,file:all:read,file:all:write&code_challenge=' + openapicode_verifier + '&code_challenge_method=plain'; xhr.open('POST', url + params, false); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.setRequestHeader('authorization', 'Bearer ' + accesstk); xhr.send(JSON.stringify(data)); if (xhr.status === 200) { const codeurl = JSON.parse(xhr.responseText).redirectUri return codeurl.split('=')[1] } else { return (xhr.statusText); } } //获取阿里云盘开放平台token function getopenapitoken() { const nowtime = new Date().getTime(); const openapitoken = GM_getValue('openapitoken') || {} if (openapitoken?.access_token) { if (openapitoken.createdate + openapitoken.expires_in > nowtime) { return openapitoken.access_token } } const openapicode = getopenapicode() var xhr = new oldxhr(); var url = 'https://openapi.aliyundrive.com/oauth/access_token'; var data = { client_id: openapiclient_id, grant_type: 'authorization_code', code: openapicode, code_verifier: openapicode_verifier }; xhr.open('POST', url, false); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.send(JSON.stringify(data)); if (xhr.status === 200) { const tokejson = JSON.parse(xhr.responseText) tokejson.createdate = nowtime GM_setValue('openapitoken', tokejson) return tokejson.access_token; } else { return (xhr.statusText); } } //单双击功能 function clickanddbclick() { const video = document.querySelector("video"); let timer = 0; let delay = 200; let prevent = false; video.addEventListener('click', function (event) { timer = setTimeout(function () { if (!prevent) { document.querySelector('.video-player--k1J-M .btn--UrTVT').click() } prevent = false; }, delay); }); video.addEventListener('dblclick', function (event) { clearTimeout(timer); prevent = true; document.querySelector('.action--HeWYA:not([data-active])').click() }); } //全屏以及自动播放 function setfullscreen() { if (isfullscreen && document.fullscreenElement === null) { document.querySelector('.action--HeWYA:not([data-active])').click() } document.querySelector('.video-player--k1J-M .btn--UrTVT').click() var elements = document.querySelectorAll('.list--5o17x li, .next--k9RTS'); elements.forEach(function (element) { element.addEventListener('click', function () { isfullscreen = document.fullscreenElement !== null }); }); function videoEndHandler() { changevideo(1) } const video = document.querySelector("video"); video.removeEventListener('ended', videoEndHandler, false); video.addEventListener('ended', videoEndHandler, false); } //更多播放倍率 function morerate() { let playbackRate = GM_getValue("playbackRate") || 1; let video = document.querySelector("video"); if (video) { video.onplay = function () { video.playbackRate = playbackRate; }; } const ul = document.querySelector('div[class^="drawer-list-grid"]'); if (!ul || !video) { return; } else if (ul.childNodes.length > 7) {//确保只执行一次 return } adddownloadbut(); setfullscreen(); clickanddbclick(); const close = ul.parentElement.parentElement.previousElementSibling.children.item(1); let firstChild = [...ul.children].find( (el) => el.firstChild.textContent === "1.5 倍" ); let secendChild = [...ul.children].find( (el) => el.firstChild.textContent === "0.75 倍" ); const originChild = firstChild; const rates = ["12", "10", "8", "6", "5", "4", "3", "2.5", "2", "0.5", "0.25", "0.1"]; rates.forEach((rate) => { if (parseInt(rate) < 2) { const cloneNode = secendChild.cloneNode(true); cloneNode.firstChild.innerHTML = `${rate} 倍`; ul.insertBefore(cloneNode, secendChild); secendChild = cloneNode; } else { const cloneNode = firstChild.cloneNode(true); cloneNode.firstChild.innerHTML = `${rate} 倍`; ul.insertBefore(cloneNode, firstChild); firstChild = cloneNode; } }); ul.insertBefore(originChild, firstChild); const backRateNodes = [...ul.children]; const changeSelectColor = (select) => { setTimeout(() => { backRateNodes.forEach((item) => { item.dataset.isCurrent = "false"; }); if (!select.dataset.isCurrent) { select.parentElement.dataset.isCurrent = "true"; } else { select.dataset.isCurrent = "true"; } }); }; const currentTarget = [...ul.children].find( (el) => el.firstChild.textContent === `${playbackRate} 倍` ); changeSelectColor(currentTarget); const tagNodes = [...ul.children]; for (let i = 0; i < 3; i++) { const node = tagNodes[tagNodes.length - 1 - i]; if (node && node.parentNode) { node.parentNode.removeChild(node); } } ul.addEventListener("click", (e) => { const target = e.target; playbackRate = target.textContent.replace(" 倍", ""); video.playbackRate = playbackRate; GM_setValue("playbackRate", playbackRate); changeSelectColor(target); close.click(); }); isChanged = true; } //添加下载按钮 function adddownloadbut() { const selffile = location.pathname.startsWith('/drive/file') const div = document.querySelector('.actions--YfXrK') div.insertAdjacentHTML('beforeend', '