forked from l0o0/translators_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBiliBili.js
140 lines (120 loc) · 3.68 KB
/
BiliBili.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{
"translatorID": "4424aefd-bcc9-41d7-ad2a-fb63c86b267f",
"label": "BiliBili",
"creator": "Xingzhong Lin",
"target": "^https?://([^/]+\\.)?bilibili\\.com",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gc",
"lastUpdated": "2020-11-04 14:48:26"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2020 Xingzhong Lin
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
function detectWeb(doc, url) {
if (/\/video\/(BV|bv|av)/.test(url)) {
return "videoRecording";
} else
if (getSearchResults(doc, true)) {
return "multiple";
}
return false;
}
function getSearchResults(doc, checkOnly, itemInfos) {
var items = {};
var found = false;
var rows = ZU.xpath(doc, "//ul[contains(@class, 'video-list')]/li");
for (let row of rows) {
let href = row.getElementsByTagName('a')[0].href;
let title = ZU.trimInternal(row.getElementsByTagName('a')[0].title);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
if (itemInfos) {
itemInfos[href] = row;
}
}
return found ? items : false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
var itemInfos = {};
var items = getSearchResults(doc, false, itemInfos);
Zotero.selectItems(items, function (selectedItems) {
for (url in selectedItems) {
// Z.debug(url);
selectedRow = itemInfos[url];
scrape(selectedRow, url);
}
});
} else
{
scrape(doc, url);
}
}
function scrape(doc, url) {
var item = new Zotero.Item("videoRecording");
var title = ZU.xpath(doc, "//h1[@class='video-title']");
if (!title.length) {
title = ZU.xpath(doc, ".//a[@class='title']");
}
// Z.debug(title[0]);
item.title = title[0].title;
item.url = url;
var time = ZU.xpath(doc, "//span[@class='bilibili-player-video-time-total']");
if (!time.length) {
time = ZU.xpath(doc, "./a");
}
item.runningTime = time[0].innerText;
var uploadDate = ZU.xpath(doc, "//head/meta[@itemprop='uploadDate']");
item.date = uploadDate[0].content;
var authors = ZU.xpath(doc, "//div[@class='r-con']//a[contains(@class,'username') or contains(@class,'name-text')]");
if (!authors.length) {
authors = ZU.xpath(doc, "./div/div[3]/span[4]");
}
authors.forEach(function(a){
item.creators.push({
lastName: a.innerText,
creatorType: "author",
fieldMode: 1
});
})
var description = ZU.xpath(doc, "//div[@id='v_desc']/div[contains(@class, 'info')]");
if (description.length) {
item.abstractNote = ZU.cleanTags(description[1].innerText);
}
var tags = ZU.xpath(doc, "//li[@class='tag'] ");
if (tags.length) {
item.tags = [];
for (var tag of tags) {
item.tags.push(tag.innerText);
}
}
var series = ZU.xpath(doc, "//div[@class='cur-list']");
if (series.length) {
item.seriesTitle = item.title;
var cur = ZU.xpath(doc, "//div[@class='cur-list']//li[contains(@class, 'on')]")[0];
item.title = cur.innerText;
var list = ZU.xpath(doc, "//div[@class='cur-list']//li");
item.numberOfVolumes = list.length;
item.volume = list.indexOf(cur) + 1;
}
item.complete();
}