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

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
// ==UserScript==
// @include       http://last.fm/*
// @include       http://*.last.fm/*
// @include       https://last.fm/*
// @include       https://*.last.fm/*
// @include       http://lastfm.de/*
// @include       http://*.lastfm.de/*
// @include       https://lastfm.de/*
// @include       https://*.lastfm.de/*
// @require       utils.js
// ==/UserScript==

// This placeholder gets munged with real data at build time.
var WebappsGettextDict = JSON.parse(unescape(
    "%7B%22GETTEXT%22%3A%22PLACEHOLDER%22%7D"
));

window.Unity = external.getUnityObject(1);

function isCorrectPage() {
    var i, ids = ['nowPlayingMeta', 'radioControlPlay', 'radioControlPause', 'radioControlSkip', 'webRadio'];

    for (i = 0; i < ids.length; i++) {
        if (!document.getElementById(ids[i])) {
            return false;
        }
    }

    return true;
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}

function getTrackInfo() {
    var title = document.title;
    var artLocation = null;
    var album = null;
    var artist = null;
    try {
        var meta = document.getElementById('nowPlayingMeta');
        artLocation = document.evaluate('div/h1/a/img', meta, null,
                                        XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.src;

        title = document.title.match(/.+(?= - Last\.fm$)/)[0];

        artist = document.evaluate('div/h1/a[2]', meta, null,
                                   XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML;
        artist = trim(artist);

        if (title.substr(0, artist.length + 3) === artist + ' - ') {
            title = title.substr(artist.length + 3);

            var trackAlbum = document.getElementById('trackAlbum');
            album = document.evaluate('ul/li/div/strong/a', trackAlbum, null,
                                      XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent;
            trim(album);
        } else {
            artist = null;
        }
    } catch (x) {}

    if (!artLocation) {
        return null;
    }

    return {
        title: title,
        album: album,
        artist: artist,
        artLocation: artLocation
    };
}

function musicPlayerSetup() {
    if (isCorrectPage() === false) {
        return;
    }
    Unity.MediaPlayer.init("last.fm");
    Unity.MediaPlayer.setCanGoPrevious(false);

    setTimeout(wrapCallback(function retry() {
        var trackInfo = getTrackInfo();

        setTimeout(retry, 1500);

        if (trackInfo) {
            Unity.MediaPlayer.setTrack(trackInfo);
        }
        var paused = document.getElementById('webRadio').className === 'paused';
        if (paused) {
            Unity.MediaPlayer.setPlaybackState(Unity.MediaPlayer.PlaybackState.PAUSED);
        } else {
            Unity.MediaPlayer.setPlaybackState(Unity.MediaPlayer.PlaybackState.PLAYING);
        }
        /*
        var playLists = document.evaluate('//a[@data-analytics-action="PlayRadioButton"]',
                                          document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        var i, node, res = [];
        for (i = 0; i < playLists.snapshotLength; i++) {
            node = playLists.snapshotItem(i);
        }*/
    }), 1000);

    Unity.MediaPlayer.onPlayPause(wrapCallback(function () {
        var paused = document.getElementById('webRadio').className === 'paused';
        var pause = document.getElementById('radioControlPause');
        var play = document.getElementById('radioControlPlay');
        if (paused) {
            launchClickEvent(play);
        } else {
            launchClickEvent(pause);
        }

        Unity.MediaPlayer.setPlaybackState(!paused);
    }));

    Unity.MediaPlayer.onNext(wrapCallback(function () {
        launchClickEvent(document.getElementById('radioControlSkip'));
    }));
}

Unity.init({ name: "last.fm",
             iconUrl: "icon://unity-webapps-last-fm",
             domains: [['last.fm', 'http://www.last.fm/'],
                       ['lastfm.de', 'http://www.lastfm.de/']],
             onInit: wrapCallback(musicPlayerSetup) });