forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
125 lines (102 loc) · 2.87 KB
/
popup.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
$(function() {
'use strict';
// trick to get current tab ID
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
var tab = tabs[0];
onTabReady(tab.id);
});
/**
* Tab ID is resolved callback
*/
function onTabReady(tabId) {
// ask background for current song info
chrome.runtime.sendMessage({
type: 'v2.getSong',
tabId: tabId
}, function(response) {
// false means legacy mode
if (response === false) {
legacy();
} else {
onSongLoaded(tabId, response);
}
});
}
/**
* Song data from background script are loaded
*/
function onSongLoaded(tabId, song) {
// no current song - should not happen, because page action with popup shows
// only when there is a song that can be corrected
if (song === null) {
return;
}
$('#artist').val(song.processed.artist || song.parsed.artist);
$('#track').val(song.processed.track || song.parsed.track);
$('#submit').click(function () {
$(this).attr('disabled', true);
$(this).siblings().remove();
var data = {
artist: $('#artist').val(),
track: $('#track').val()
};
chrome.runtime.sendMessage({
type: 'v2.correctSong',
tabId: tabId,
data: data
});
// show green tick even if the song may not be valid - we have no way of knowing yet
$('#form').fadeOut(0);
$('#valid').fadeIn(0);
});
}
/**
* Legacy code for cases when legacy controller is loaded and active
*/
function legacy() {
var popupApi = chrome.extension.getBackgroundPage().popupApi;
var song = popupApi.getSong();
if (song.artist !== '') {
$('#artist').val(song.artist);
}
if (song.track !== '') {
$('#track').val(song.track);
}
$('#submit').click(function () {
$(this).attr('disabled', true);
$(this).siblings().remove();
var artist = $('#artist').val();
var track = $('#track').val();
var validateCB = function (res) {
if (res === false) {
$(this).attr('disabled', false);
$(this).siblings().remove();
$(this).parent().append('<span class="note">Not valid</span>');
} else {
// did the content script recognize at least the duration?
if (!song.duration) {
song.duration = res.duration;
}
// fill other data
song.artist = res.artist;
song.track = res.track;
//chrome.extension.getBackgroundPage().song.album = res.album;
// make the connection to last.fm service to notify
popupApi.nowPlaying(song);
// The minimum time is 240 seconds or half the track's total length
// minus the time that already past
var past = parseInt(new Date().getTime() / 1000.0) - song.startTime;
var min_time = Math.min(240, song.duration / 2) - past;
// Set up the timer to scrobble
popupApi.planSubmit(min_time * 1000);
$('#form').fadeOut(0);
$('#valid').fadeIn(0);
}
};
popupApi.validate(artist, track, validateCB);
});
}
});