-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
54 lines (51 loc) · 1.71 KB
/
popup.js
File metadata and controls
54 lines (51 loc) · 1.71 KB
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
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 insert(html) {
document.body.insertAdjacentHTML('beforeend', html)
}
function insertLyric(response) {
if (response === undefined) { insert('歌詞が見つかりませんでした') }
else {
for (lyric of response['lyrics']) {
if (lyric['translationType'] == 'Original') {
insert(lyric['value'].replaceAll('\n', '<br>'))
return
}
}
insert('歌詞が見つかりませんでした')
}
}
chrome.tabs.query({ 'active': true, 'currentWindow': true }, tabs => {
const url = tabs[0].url
if (url.startsWith(Config.youtube.VIDEO_BASE_URL) || url.startsWith(Config.niconico.VIDEO_BASE_URL)) {
chrome.storage.local.get(url, function (result) {
if (!Object.keys(result).length) {
fetch(Config.vocadb.API_BASE_URL + '/songs?query=' + url + '&fields=Lyrics', {
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Oneclyric Chrome Extension'
}
})
.then(response => response.json())
.then(response => response['items'][0])
.then(response => chrome.storage.local.set({ [url]: response }, function () {
insertLyric(response)
}))
}
else { insertLyric(result[url]) }
})
}
else { insert('YouTubeとニコニコ動画のみ対応しています') }
})