-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
50 lines (47 loc) · 1.45 KB
/
background.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
const Config = {
vocadb: new function () {
this.BASE_URL = 'https://vocadb.net/'
this.API_BASE_URL = this.BASE_URL + 'api'
},
youtube: new function () {
this.BASE_URL = 'https://www.youtube.com/'
this.VIDEO_BASE_URL = this.BASE_URL + 'watch'
},
niconico: new function () {
this.BASE_URL = 'https://www.nicovideo.jp/'
this.VIDEO_BASE_URL = this.BASE_URL + 'watch'
},
}
function setBadge(tab) {
chrome.action.setBadgeText({ text: "x", tabId: tab.id })
}
chrome.action.onClicked.addListener((tab) => {
if (tab.url.startsWith(Config.youtube.VIDEO_BASE_URL) || tab.url.startsWith(Config.niconico.VIDEO_BASE_URL)) {
fetch(Config.vocadb.API_BASE_URL + '/songs?query=' + tab.url + '&fields=PVs', {
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Oneclink Chrome Extension'
}
})
.then(response => response.json())
.then(response => response['items'][0])
.then(response => redirect(tab, response))
}
else { setBadge(tab) }
})
function redirect(tab, response) {
if (response === undefined) { setBadge(tab) }
else {
if (tab.url.startsWith(Config.youtube.VIDEO_BASE_URL)) { var service = "NicoNicoDouga" }
else { var service = "Youtube" }
for (pv of response["pvs"]) {
if (pv["pvType"] == "Original") {
if (pv["service"] == service) {
chrome.tabs.update(tab.id, { url: pv["url"] })
return
}
}
}
setBadge(tab)
}
}