~justinmcp/unity-webapps-gmail/bug-1062890-2

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ==UserScript==
// @include        https://mail.google.com/*
// @require        utils.js
// @require        google-common.js
// ==/UserScript==

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

window.Unity = external.getUnityObject(1);
var pane = null;

/**
 * Validates the page for functional tests.
 *
 */
function selfTest() {
    if (!getComposeMessageNode()) {
        return;
    }
    if (!checkMessagesCount().length) {
        return;
    }

    reportTestState('PASS SELF TEST');
};

function getComposeMessageNode() {
    return document.evaluate('//div[@role="navigation"]/div[1]/div[1]/div[1][@role="button"]', document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
}

function isCorrectPage() {
    var i, ids = ['hist_frame', 'js_frame'];

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

    return true;
}

function getNumber(str) {
    try {
        var onlyNumber = str.match(/\(([0-9]|,|\s)+\)/)[0].match(/([0-9]|,|\s)+/)[0];

        return onlyNumber.replace(/(,|\s)/g, "");
    } catch (x) {
        return '0';
    }
}

function getLabels() {
    var i, res = [];

    var snapshot = document.evaluate('//div[2]/div/div/div[5]/div/*/div/div/div[2]/span/a',
                                     pane, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

    for (i = 0; i < snapshot.snapshotLength; i++) {
        var node = snapshot.snapshotItem(i);

        res.push({ name: unescape(node.href.match(/#label\/(.+)$/)[1]),
                   count: getNumber(node.textContent),
                   link: node.href });
    }

    return res;
}

function doMatrixIntegration() {
    doMainMenuIntegration(document);

    var i, labels = getLabels();
    for (i = 0; i < labels.length; i++) {
        Unity.addAction('/' + labels[i].name, makeRedirector(labels[i].link));
    }
}

function checkMessagesCount() {
    var tag = document.evaluate('//div/div[2]/div/div/div/div/div[@role="button"]/span', document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;

    if (tag.textContent != 'Gmail' && tag.textContent != 'Mail') {
        throw new Error();
    }

    var indicators = [];

    var inboxLink = document.evaluate('//div[@role="navigation"]/div/div/div/div/div/div/div/div/div/span/a', document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;

    if (!inboxLink || !inboxLink.href || !inboxLink.text) {
        return indicators;
    }
    var numMessages = getNumber(inboxLink.text);

    indicators.push({ name: _("Inbox"),
                      count: numMessages,
                      callback: makeRedirector(inboxLink.href) });
    var i, labels = getLabels();
    for (i = 0; i < labels.length; i++) {
        indicators.push({ name: trim(labels[i].name), count: labels[i].count, callback: makeRedirector(labels[i].link) });
    }

    return indicators;
}

function unityLoaded() {
    var composeNewMessage = wrapCallback(function () {
        var compose = getComposeMessageNode();
        click(compose);
    });

    Unity.MessagingIndicator.addAction(_("Compose New Message"),
                                       composeNewMessage);
    Unity.Launcher.addAction(_("Compose New Message"),
                             composeNewMessage);

    var indicatorsController = new Indicators(checkMessagesCount);

    window.onpopstate = function () {
        var hash = window.location.hash;
        var label = null;
        if (hash.match(/#label\/(.+)/)) {
            label = unescape(hash.match(/#label\/(.+)/)[1]);
        } else if (hash === '#inbox') {
            label = _("Inbox");
        } else {
            return;
        }
        indicatorsController.visited(label);
    };

    doMatrixIntegration();

    selfTest();
}

if (isCorrectPage()) {
    setTimeout(wrapCallback(function wait() {
        pane = document.evaluate('//div[@role="navigation"]',
                                 document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;

        if (!pane || !document.getElementsByClassName("nU")) {
            setTimeout(wait, 1000);
            return;
        }
        try {
            var gmaillogin = document.evaluate('//div[@role="navigation"]/div/div[1]/div[3]/div/ol/li[4]/div/div/div/div[2]/span[2]', document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue.textContent;
        } catch(err) {
            var gmaillogin = "";
        }

        try {
            var gappslogin = document.evaluate('//div[@role="navigation"]/div/div[1]/div[3]/div/ol/li[4]/div/div/div/div[3]/span[2]', document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue.textContent;
        }catch(err) {
            var gappslogin = "";
        }
        login = gmaillogin.indexOf("@") != -1 ? gmaillogin : gappslogin.indexOf("@") != -1 ? gappslogin : "";

        Unity.init({ name: "GMail",
                     login: login,
                     iconUrl: "icon://unity-webapps-gmail",
                     homepage: 'https://mail.google.com',
                     domain: 'mail.google.com',
                     onInit: wrapCallback(unityLoaded) });
    }), 2000);
}