-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
72 lines (59 loc) · 2.38 KB
/
background.js
File metadata and controls
72 lines (59 loc) · 2.38 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
let previousTitle = '';
let previousArtist = '';
let currentPlaybackTime = '';
chrome.runtime.onStartup.addListener(() => {
chrome.storage.local.clear(() => {
});
});
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.clear(() => {
});
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'TRACK_INFO') {
const title = request.title;
const artist = request.artist;
if (title !== previousTitle || artist !== previousArtist) {
previousTitle = title;
previousArtist = artist;
let apiUrl = `http://ec2-15-164-11-77.ap-northeast-2.compute.amazonaws.com/lyrics?title=${encodeURIComponent(title)}&artist=${encodeURIComponent(artist)}`;
chrome.storage.local.set({ lyricsData: "로딩 중..", title: title, artist: artist });
fetch(apiUrl)
.then(response => response.json())
.then(data => {
if(data.detail == "Song not found"){
chrome.storage.local.set({ lyricsData: "😭가사 데이터가 없습니다😭"});
}
else{
chrome.storage.local.set({ lyricsData: data});
}
})
}
}
if (request.type === 'PLAYBACK_TIME') {
currentPlaybackTime = request.currentTime;
chrome.storage.local.set({ currentTime: currentPlaybackTime });
}
if (request.type === 'GET_LYRICS') {
chrome.storage.local.get(['lyricsData', 'title', 'artist', 'currentTime'], function (data) {
sendResponse({ lyrics: data.lyricsData, title: data.title, artist: data.artist, currentTime: data.currentTime });
});
}
return true; // sendResponse를 비동기적으로 호출할 때 필요합니다.
});
chrome.action.onClicked.addListener((tab) => {
chrome.windows.getCurrent((currentWindow) => {
const width = 350;
const height = 300;
const left = currentWindow.left + currentWindow.width - width - 20;
const top = currentWindow.top + currentWindow.height - height - 20;
chrome.windows.create({
url: chrome.runtime.getURL('popup.html'),
type: 'popup',
width: width,
height: height,
left: left,
top: top
});
});
});