~justinmcp/unity-webapps-lastfm-radio/fix-module-imports

1 by Alexandre Abreu
first
1
// ==UserScript==
2
// @include       http://last.fm/*
3
// @include       http://*.last.fm/*
4
// @include       https://last.fm/*
5
// @include       https://*.last.fm/*
21 by Alexandre Abreu
Fix integration script
6
// @include       http://lastfm.de/*
7
// @include       http://*.lastfm.de/*
8
// @include       https://lastfm.de/*
9
// @include       https://*.lastfm.de/*
1 by Alexandre Abreu
first
10
// @require       utils.js
11
// ==/UserScript==
12
27 by Robert Bruce Park
Add gettext placeholder, whitespace cleanup.
13
// This placeholder gets munged with real data at build time.
30.1.1 by Robert Bruce Park
Add missing manifest.
14
var WebappsGettextDict = JSON.parse(unescape(
27 by Robert Bruce Park
Add gettext placeholder, whitespace cleanup.
15
    "%7B%22GETTEXT%22%3A%22PLACEHOLDER%22%7D"
16
));
17
1 by Alexandre Abreu
first
18
window.Unity = external.getUnityObject(1);
19
20
function isCorrectPage() {
21
    var i, ids = ['nowPlayingMeta', 'radioControlPlay', 'radioControlPause', 'radioControlSkip', 'webRadio'];
22
23
    for (i = 0; i < ids.length; i++) {
24
        if (!document.getElementById(ids[i])) {
25
            return false;
26
        }
27
    }
28
29
    return true;
30
}
31
32
function trim(str) {
33
    return str.replace(/^\s+|\s+$/g, '');
34
}
35
36
function getTrackInfo() {
37
    var title = document.title;
38
    var artLocation = null;
39
    var album = null;
40
    var artist = null;
41
    try {
42
        var meta = document.getElementById('nowPlayingMeta');
43
        artLocation = document.evaluate('div/h1/a/img', meta, null,
44
                                        XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.src;
45
46
        title = document.title.match(/.+(?= - Last\.fm$)/)[0];
47
48
        artist = document.evaluate('div/h1/a[2]', meta, null,
49
                                   XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML;
50
        artist = trim(artist);
51
52
        if (title.substr(0, artist.length + 3) === artist + ' - ') {
53
            title = title.substr(artist.length + 3);
54
55
            var trackAlbum = document.getElementById('trackAlbum');
56
            album = document.evaluate('ul/li/div/strong/a', trackAlbum, null,
57
                                      XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent;
58
            trim(album);
59
        } else {
60
            artist = null;
61
        }
62
    } catch (x) {}
63
64
    if (!artLocation) {
65
        return null;
66
    }
67
68
    return {
69
        title: title,
70
        album: album,
71
        artist: artist,
72
        artLocation: artLocation
73
    };
74
}
75
76
function musicPlayerSetup() {
77
    if (isCorrectPage() === false) {
78
        return;
79
    }
80
    Unity.MediaPlayer.init("last.fm");
81
    Unity.MediaPlayer.setCanGoPrevious(false);
82
83
    setTimeout(wrapCallback(function retry() {
84
        var trackInfo = getTrackInfo();
85
86
        setTimeout(retry, 1500);
87
88
        if (trackInfo) {
89
            Unity.MediaPlayer.setTrack(trackInfo);
90
        }
21 by Alexandre Abreu
Fix integration script
91
        var paused = document.getElementById('webRadio').className === 'paused';
92
        if (paused) {
93
            Unity.MediaPlayer.setPlaybackState(Unity.MediaPlayer.PlaybackState.PAUSED);
94
        } else {
95
            Unity.MediaPlayer.setPlaybackState(Unity.MediaPlayer.PlaybackState.PLAYING);
96
        }
27 by Robert Bruce Park
Add gettext placeholder, whitespace cleanup.
97
        /*
1 by Alexandre Abreu
first
98
        var playLists = document.evaluate('//a[@data-analytics-action="PlayRadioButton"]',
99
                                          document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
100
        var i, node, res = [];
101
        for (i = 0; i < playLists.snapshotLength; i++) {
102
            node = playLists.snapshotItem(i);
21 by Alexandre Abreu
Fix integration script
103
        }*/
1 by Alexandre Abreu
first
104
    }), 1000);
105
106
    Unity.MediaPlayer.onPlayPause(wrapCallback(function () {
107
        var paused = document.getElementById('webRadio').className === 'paused';
108
        var pause = document.getElementById('radioControlPause');
109
        var play = document.getElementById('radioControlPlay');
110
        if (paused) {
111
            launchClickEvent(play);
112
        } else {
113
            launchClickEvent(pause);
114
        }
115
116
        Unity.MediaPlayer.setPlaybackState(!paused);
117
    }));
118
119
    Unity.MediaPlayer.onNext(wrapCallback(function () {
120
        launchClickEvent(document.getElementById('radioControlSkip'));
121
    }));
122
}
123
124
Unity.init({ name: "last.fm",
11 by Alexandre Abreu
Update/fix installed icons; update copyright
125
             iconUrl: "icon://unity-webapps-last-fm",
21 by Alexandre Abreu
Fix integration script
126
             domains: [['last.fm', 'http://www.last.fm/'],
127
                       ['lastfm.de', 'http://www.lastfm.de/']],
1 by Alexandre Abreu
first
128
             onInit: wrapCallback(musicPlayerSetup) });