-
Notifications
You must be signed in to change notification settings - Fork 3
/
injectBetaDeezer.js
107 lines (87 loc) · 3.45 KB
/
injectBetaDeezer.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
function fireTrackEvent(data) {
function getTrack( optionalData )
{
var track = {};
track.title = dzPlayer.songTitle ;
track.artist = dzPlayer.artistName;
track.album = dzPlayer.albumTitle ;
track.percent = dzPlayer.position/dzPlayer.duration;
track.isPlaying = dzPlayer.playing ;
track.position = dzPlayer.position ;
// console.log( "GETINFO : " + track.artist + " - " + track.title + " - isPlaying : " + track.isPlaying + " - percent : " + track.percent ) ;
return track ;
}
// Context of the page
var hiddenDiv = document.getElementById('LikeFMComm');
var myTrack = getTrack(data) ;
hiddenDiv.textContent = JSON.stringify(myTrack);
hiddenDiv.dispatchEvent(trackEvent);
}
function LikeFMInject () {
// Comm link with content script
trackEvent = document.createEvent('Event');
trackEvent.initEvent('myTrackEvent', true, true);
oldDoAction = playercontrol.callBackPlayer ;
function newDoAction(propName, val) {
// console.log( "action : " + propName ) ;
fireTrackEvent(propName != "finish") ;
return oldDoAction.call(this, propName, val) ;
}
function bind() {
try {
playercontrol.callBackPlayer=newDoAction ;
} catch (e) {
setTimeout(bind,1200);
}
}
bind();
}
// Below is in the context of content script
// Injected script
if (!document.getElementById("LikeFMInject")) {
var script = document.createElement('script');
script.setAttribute('id','LikeFMInject');
script.appendChild(document.createTextNode('var LikeFM = {}; var trackEvent;' + fireTrackEvent + '('+ LikeFMInject +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
}
// Comm link medium div
if (!document.getElementById("LikeFMComm")) {
var comm = document.createElement('div');
comm.setAttribute('id','LikeFMComm');
comm.style.display = "none";
(document.body || document.documentElement).appendChild(comm);
// Comm link with injected script
document.getElementById('LikeFMComm').addEventListener('myTrackEvent', function() {
var track = {};
var myTrack = JSON.parse(document.getElementById('LikeFMComm').textContent);
var percent = myTrack.percent ; // dzPlayer.position/dzPlayer.duration;
track.lsource = 'Deezer.com';
track.source = 'P';
if (myTrack.isPlaying && myTrack.position == 0
&& (
(LikeFM.currentTrack
&& ( myTrack.title != LikeFM.currentTrack.title ||
myTrack.artist != LikeFM.currentTrack.artist)
) || !LikeFM.currentTrack
)
) {
track.title = myTrack.title;
track.artist = myTrack.artist;
track.album = myTrack.album;
track.type = 'touch';
// Send message to background process
// console.log( "TOUCH : " + track.artist + " - " + track.title ) ;
chrome.extension.sendRequest({messageType:"track",data:track});
LikeFM.currentTrack = track;
} else if (myTrack.isPlaying && percent > 0.8 && LikeFM.currentTrack) {
track.title = myTrack.title;
track.artist = myTrack.artist;
track.album = myTrack.album;
track.type = 'finish';
// Send message to background process
// console.log( "FINISH : " + track.artist + " - " + track.title ) ;
chrome.extension.sendRequest({messageType:"track",data:track});
LikeFM.currentTrack = null;
}
});
}