~webapps/unity-webapps-googleplusgames/trunk

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
// ==UserScript==
// @include       http://plus.google.com/*
// @include       https://plus.google.com/*
// @require       utils.js
// ==/UserScript==

window.Unity = external.getUnityObject(1);

function getAppNameNode() {
    return document.evaluate('//div[@id="contentPane"]/div/nav/div/div/div[3]',
                             document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
}

function isCorrectPage() {
    if (!document.getElementById('contentPane')) {
        return false;
    }
    return true;
}

function getImg(appName) {
    var params = "numNotificationsToFetch=20&sp=%5B9%2C2%2Cnull%2Cnull%2Cnull%2C20%2Cnull%2Cnull%2C%5B%5D%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%5B%5D%5D&hl=en&at=AObGSAjzAjXMPD4pE9JoHEDsiL82mpcUkw%3A1337256020000&";
    var request = new XMLHttpRequest();

    request.onreadystatechange = wrapCallback(function () {
        if (request.readyState === 4) {
            var text = request.responseText;
            var pos = text.indexOf(appName);
            while (text[pos] !== '[' && pos > 0) {
                pos--;
            }
            var startPos = pos;
            pos++;
            var depth;
            for (depth = 1; depth && pos < text.length; pos++) {
                if (text[pos] === '[') {
                    depth++;
                }
                if (text[pos] === ']') {
                    depth--;
                }
            }
            var desc = text.substr(startPos, pos - startPos);

            pos = desc.indexOf(',');
            while (desc.indexOf(',', pos + 1) !== -1) {
                pos = desc.indexOf(',', pos + 1);
            }

            var img = desc.substr(pos + 2);
            img = img.substr(0, img.length - 2);
            if (img.indexOf('https://') !== -1) {
                img = 'http://' + img.substr('https://'.length);
            }

            Unity.toDataURL(img, function (aResult, uri) {
                Unity.init({ name: appName,
                             iconUrl: uri,
                             domain: 'plus.google.com',
                             homepage: String(window.location),
                             onInit: null });
            });
        }
    });
    request.open("POST", "https://plus.google.com/_/games/getGamesModel?_reqid=3432&rt=j", true);

    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", params.length);
    request.setRequestHeader("Connection", "close");

    request.send(params);
}


if (isCorrectPage()) {
    setInterval(wrapCallback(function wait() {
        // It's change location without reloading page
        if (!getAppNameNode() || !getAppNameNode().textContent.length) {
            return;
        }
        var appName = getAppNameNode().textContent;

        if (!localStorage.getItem(appName)) {
            getImg(appName);
            localStorage.setItem(appName, true);
        }
    }), 2000);
}