-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
150 lines (133 loc) · 4.71 KB
/
content.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
141
142
143
144
145
146
147
148
149
150
// content.js
function createNotification(message, type) {
const notification = document.createElement('div');
notification.style.cssText = `
position: fixed;
top: 80px;
right: 20px;
padding: 12px 20px;
border-radius: 5px;
z-index: 10000;
font-family: Arial, sans-serif;
font-size: 14px;
animation: slideIn 0.3s ease-out;
background-color: ${type === 'error' ? '#ff4444' : '#1DB954'};
color: white;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
`;
notification.textContent = message;
document.body.appendChild(notification);
const style = document.createElement('style');
style.textContent = `
@keyframes slideIn {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
@keyframes successPop {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
75% { transform: scale(0.95); }
100% { transform: scale(1); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}
`;
document.head.appendChild(style);
setTimeout(() => {
notification.style.animation = 'slideOut 0.3s ease-in';
notification.addEventListener('animationend', () => {
document.body.removeChild(notification);
});
style.textContent += `
@keyframes slideOut {
from { transform: translateX(0); opacity: 1; }
to { transform: translateX(100%); opacity: 0; }
}
`;
}, 3000);
}
function createLikeButton() {
if (document.getElementById('spotify-like-button')) return;
const button = document.createElement('button');
button.id = 'spotify-like-button';
button.innerHTML = '♥ Add to Spotify';
button.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 9999;
padding: 12px 20px;
background: #1DB954;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-family: Arial, sans-serif;
font-size: 14px;
font-weight: bold;
transition: all 0.2s ease;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
`;
button.addEventListener('mouseover', () => {
button.style.transform = 'translateY(-2px)';
button.style.boxShadow = '0 4px 8px rgba(0,0,0,0.2)';
});
button.addEventListener('mouseout', () => {
button.style.transform = 'translateY(0)';
button.style.boxShadow = '0 2px 5px rgba(0,0,0,0.1)';
});
button.addEventListener('click', async () => {
const artistElement = document.querySelector('[data-player-meta="artist_name"]');
const songElement = document.querySelector('[data-player-meta="song_title"]');
if (artistElement && songElement) {
button.disabled = true;
button.innerHTML = '<span style="display: inline-block; animation: spin 1s linear infinite">↻</span> Adding...';
const artist = artistElement.textContent.trim();
const title = songElement.textContent.trim().replace(/^[\s–-]+/, '');
chrome.runtime.sendMessage({
action: 'addToLiked',
songInfo: { artist, title }
}, response => {
if (response?.success) {
button.style.animation = 'successPop 0.5s ease-out';
button.innerHTML = '✓ Added!';
button.style.background = '#1ed760';
createNotification(`Added "${title}" by ${artist} to your Liked Songs`, 'success');
} else {
button.style.animation = 'shake 0.5s ease-in-out';
button.innerHTML = '✗ Failed';
button.style.background = '#ff4444';
const errorMessage = response?.error === 'Song not found on Spotify'
? `Couldn't find "${title}" by ${artist} on Spotify`
: 'Failed to add song. Please try again.';
createNotification(errorMessage, 'error');
}
button.disabled = false;
setTimeout(() => {
button.style.animation = '';
button.style.background = '#1DB954';
button.innerHTML = '♥ Add to Spotify';
}, 2000);
});
}
});
document.body.appendChild(button);
}
// Create button immediately
createLikeButton();
// Also create on window load
window.addEventListener('load', createLikeButton);
// Watch for changes and recreate if needed
const observer = new MutationObserver(() => {
if (!document.getElementById('spotify-like-button')) {
createLikeButton();
}
});
observer.observe(document.body, { childList: true, subtree: true });