Skip to content

Commit

Permalink
修复视频拜年祭页面问题
Browse files Browse the repository at this point in the history
  • Loading branch information
jjj201200 committed Feb 1, 2020
1 parent 526005b commit 56ba3c6
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bilibili-helper-master",
"version": "1.2.14",
"version": "1.2.16-beta.1",
"description": "bilibili-helper",
"main": "index.js",
"dependencies": {
Expand Down
37 changes: 23 additions & 14 deletions src/js/libs/messageStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ export class MessageStore {
this.has(tabId) && removeInfo.isWindowClosing && this.delete(tabId);
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
const {status, url} = changeInfo;
const {status, url, favIconUrl, audible} = changeInfo;
// 切换分P时url!==undefined,不需要清除
if (this.has(tabId) && status === 'loading' && url === undefined) { this.delete(tabId); } else if (this.has(tabId) && status === 'complete') {
if (this.has(tabId) && status === 'loading' && url === undefined && !favIconUrl && audible === undefined) {
this.delete(tabId);
} else if (this.has(tabId) && status === 'complete' && !favIconUrl && audible === undefined) {
this.createData(tabId);
}
});
Expand All @@ -49,22 +51,27 @@ export class MessageStore {
* @param id
* @return {{state, queue, data}}
*/
createData = (id) => {
if (this.has(id)) { return this.store[id]; } else {
createData = (id, frameId) => {
const storeId = this.createStoreId(id, frameId);
if (this.has(storeId)) {
return this.store[storeId];
} else {
//console.warn(`Create MessageStore on Tab ${id}`);
return this.store[id] = {
return this.store[storeId] = {
state: 0, // 初始状态 0 等待前端信号
queue: [], // 任务队列
data: {id}, // 数据对象 存储如cid之类的数据
};
}
};

has = (id) => !!this.store[id];
createStoreId = (id, frameId) => `${id}${frameId ? '-' + frameId : ''}`;

get = (id) => this.store[id];
has = storeId => !!this.store[storeId];

delete = (id) => delete this.store[id];
get = storeId => this.store[storeId];

delete = storeId => delete this.store[storeId];

doIt = (id, taskData) => {
if (!taskData) { return Promise.resolve(); }
Expand All @@ -76,15 +83,17 @@ export class MessageStore {
};

// 处理任务队列
dealWith = (id) => {
return this.dealWithOnce(id).then(() => {
this.store[id].queue.length > 0 && this.dealWithOnce(id); // 如果队列不为空
dealWith = (id, frameId) => {
const storeId = this.createStoreId(id, frameId);
return this.dealWithOnce(id, frameId).then(() => {
this.store[storeId] && this.store[storeId].queue.length > 0 && this.dealWithOnce(id); // 如果队列不为空
});
};

dealWithOnce = (id) => {
if (this.has[id] === false) { return Promise.resolve(console.error(`Invalid tab id ${id}`)); }
const {state, queue} = this.store[id];
dealWithOnce = (id, frameId) => {
const storeId = this.createStoreId(id, frameId);
if (this.has[storeId] === false) { return Promise.resolve(console.error(`Invalid store id ${storeId}`)); }
const {state, queue} = this.store[storeId];
if (state && queue.length > 0) {
return this.doIt(id, queue.shift()).catch((error) => console.error(error));
} else { return Promise.resolve(); }
Expand Down
5 changes: 5 additions & 0 deletions src/js/modules/danmu/UI/danmu.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export default () => {

componentDidMount() {
chrome.runtime.sendMessage({command: 'danmuDOMInitialized'});
$('.player-sidebar-list-item-inner, .bnj-player-single-item-mask').click(() => {
setTimeout(() => {
chrome.runtime.sendMessage({command: 'danmuDOMInitialized'});
}, 500);
});
}

addListener = () => {
Expand Down
5 changes: 2 additions & 3 deletions src/js/modules/danmu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class Danmu extends Feature {
chrome.webRequest.onSendHeaders.addListener((details) => {
const {tabId, initiator, requestHeaders} = details;
const fromHelper = !_.isEmpty(_.find(requestHeaders, ({name, value}) => name === 'From' && value === 'bilibili-helper'));
console.warn(details);
if (/^chrome-extension:\/\//.test(initiator) || fromHelper) {
return;
}
Expand Down Expand Up @@ -99,7 +98,7 @@ export class Danmu extends Feature {
const url = (window.URL ? URL : window.webkitURL).createObjectURL(new Blob([message.danmuDocumentStr], {
type: 'application/xml',
}));
const filename = `${message.filename}.${message.cid}.${message.date}.xml`;
const filename = `${message.filename ? message.filename + '.' : ''}${message.cid}.${message.date}.xml`;
chrome.downloads.download({
saveAs: true,
url,
Expand All @@ -116,7 +115,7 @@ export class Danmu extends Feature {
const url = (window.URL ? URL : window.webkitURL).createObjectURL(new Blob([assData], {
type: 'application/octet-stream',
}));
const filename = `${message.filename}.${message.cid}.${message.date}.ass`;
const filename = `${message.filename ? message.filename + '.' : ''}${message.cid}.${message.date}.ass`;
chrome.downloads.download({
saveAs: true,
url,
Expand Down
3 changes: 2 additions & 1 deletion src/js/modules/pictureInPicture/UI/PIP.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export default () => {
const that = this;
this.video = document.querySelector('#bofqi .bilibili-player-video video');
this.addListener(this.video);
document.querySelector('#bofqi').addEventListener('DOMNodeInserted', function(e) {
const player = document.querySelector('#bofqi');
player && player.addEventListener('DOMNodeInserted', function(e) {
if (e.target.localName === 'video' && that.video !== e.target) {
that.video = e.target;
that.addListener(that.video);
Expand Down
2 changes: 1 addition & 1 deletion src/js/modules/videoAnchor/UI/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class VideoAnchorUI extends UI {
'#arc_toolbar_report',
'#viewlater-app .video-toolbar-module',
];
const newPage = document.querySelector('.video-data, .stardust-player');
const newPage = document.querySelector('.video-data, .stardust-player, iframe.bnj-player-main');
const addUI = (container, callback) => {
if (document.querySelector('.bilibili-helper')) return;
const helperDOM = document.createElement('span');
Expand Down
24 changes: 14 additions & 10 deletions src/js/modules/videoDownload/UI/videoDownload.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ export default () => {
}, (settings) => {
this.setState({settings});
});
$('.player-sidebar-list-item-inner, .bnj-player-single-item-mask').click(() => {
setTimeout(() => {
chrome.runtime.sendMessage({command: 'videoDownloadDOMInitialized'});
}, 500);
});
}

getContainer = (type, currentCid, currentQuality, data) => {
Expand Down Expand Up @@ -217,11 +222,16 @@ export default () => {
sendResponse(true);
} else if (message.command === 'videoDownloadCid' && message.cid) { // 本地script加载视频数据时,需要检测cid
const {videoData} = this.state;
if (_.isEmpty(videoData) && !_.isEmpty(this.originVideoData)) {
const {quality} = this.originVideoData;
if (_.isEmpty(videoData)) {
const {quality = 80} = this.originVideoData;
this.currentAvid = message.avid;
if (this.originVideoData.dash) {

if (!_.isEmpty(this.originVideoData) && this.originVideoData.durl) {
const currentData = {...this.originVideoData};
const cidData = videoData[message.cid] || {};
cidData[quality] = currentData;
videoData[message.cid] = cidData;
this.setState({currentCid: message.cid, videoData, currentQuality: quality});
} else {
let url = null;
if (location.href.indexOf('bangumi') >= 0) {
url = new Url(bangumiFlvDownloadURL);
Expand All @@ -231,12 +241,6 @@ export default () => {
url.set('query', {cid: message.cid, avid: message.avid, qn: quality, otype: 'json'});
this.setState({currentCid: message.cid});
this.getFlvResponse('get', url.toString());
} else {
const currentData = {...this.originVideoData};
const cidData = videoData[message.cid] || {};
cidData[quality] = currentData;
videoData[message.cid] = cidData;
this.setState({currentCid: message.cid, videoData, currentQuality: quality});
}
}
sendResponse(true);
Expand Down
4 changes: 2 additions & 2 deletions src/js/modules/videoDownload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export class VideoDownload extends Feature {
'*://api.bilibili.com/pgc/player/web/playurl?avid=*', // 新番剧页面
'*://api.bilibili.com/x/player/playurl*', // 新版页面切换清晰度时调用,返回字段和上面相同

'*://interface.bilibili.com/player?id=cid:*',
'*://api.bilibili.com/x/player.so?id=cid:*',
'*://interface.bilibili.com/player?id=cid*',
'*://api.bilibili.com/x/player.so?id=cid*',
'*://api.bilibili.com/x/web-interface/view?*', // 获取cid和aid
],
};
Expand Down
5 changes: 3 additions & 2 deletions src/js/modules/videoHideDanmu/UI/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export class VideoHideDanmuUI extends UI {
}
return new Promise(resolve => {
this.hide();
new MutationObserver((mutationList) => {
const player = document.querySelector('#bofqi');
player && new MutationObserver((mutationList) => {
mutationList.forEach((mutation) => {
if (mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach((dom) => {
Expand All @@ -34,7 +35,7 @@ export class VideoHideDanmuUI extends UI {
});
}
});
}).observe(document.querySelector('#bofqi'), {
}).observe(player, {
subtree: true,
childList: true,
});
Expand Down
28 changes: 17 additions & 11 deletions src/js/modules/videoWiden/UI/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@ export class VideoWidenUI extends UI {
}

load = (containers, settings) => {
if (!settings.on) return Promise.resolve();
if (!settings.on) {
return Promise.resolve();
}
return new Promise(resolve => {
const option = settings.subPage.value;
this.setWide(option);
new MutationObserver((mutationList) => {
_.map(mutationList, (mutation) => {
if (mutation.oldValue) {
this.setWide(option);
}
const player = document.querySelector('#bofqi');
if (player) {

new MutationObserver((mutationList) => {
_.map(mutationList, (mutation) => {
if (mutation.oldValue) {
this.setWide(option);
}
});
}).observe(player, {
attributes: true,
attributeOldValue: true,
subtree: true,
});
}).observe(document.querySelector('#bofqi'), {
attributes: true,
attributeOldValue: true,
subtree: true,
});
}
resolve();
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/js/utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export const getLink = (url_name) => {
export const getFilename = (doc) => {
const partDOM = doc.querySelector('#v_multipage a.on, #multi_page .cur-list li.on a, #eplist_module .list-wrapper ul .cursor');
const partName = partDOM ? partDOM.innerText : '';
const title = doc.querySelector('#viewbox_report h1, .header-info h1, .media-wrapper > h1, h1.video-title').getAttribute('title');
return `${title}${partName ? `_${partName}` : ''}`.replace(/\s/g, '').replace(/[|"*?:<>\s~/]/g, '_');
const title = doc.querySelector('#viewbox_report h1, .header-info h1, .media-wrapper > h1, h1.video-title');
return `${title ? title.getAttribute('title') : ''}${partName ? `_${partName}` : ''}`.replace(/\s/g, '').replace(/[|"*?:<>\s~/]/g, '_');
};

/**
Expand Down
5 changes: 3 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.2.14",
"version": "1.2.16.1",
"update_url": "https://clients2.google.com/service/update2/crx",
"manifest_version": 2,
"minimum_chrome_version": "56.0.0",
Expand Down Expand Up @@ -45,7 +45,8 @@
"*://*.bilibili.com/bangumi/play/ep*",
"*://*.bilibili.com/video/av*",
"*://www.bilibili.com/watchlater/*",
"*://www.bilibili.com/medialist/*"
"*://www.bilibili.com/medialist/*",
"*://www.bilibili.com/blackboard/*"
],
"run_at": "document_end"
},
Expand Down

0 comments on commit 56ba3c6

Please sign in to comment.