~justinmcp/unity-webapps-cnn-news/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
// ==UserScript==
// @include       http://edition.cnn.com/*
// @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 = ['hdr-banner-title', 'hdr-search'];

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

    return true;
}

function markAsRead(items) {
    var i;
    for (i = 0; i < items.length; i++) {
        localStorage.setItem(items[i], true);
    }
}

function getHeaders(doc) {
    var res = [], i;
    var snapshot = doc.evaluate('//ul[@class="cnn_bulletbin"]/li/a',
                                doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

    reportTestState('total:' + snapshot.snapshotLength);
    for (i = 0; i < snapshot.snapshotLength; i++) {
        var node = snapshot.snapshotItem(i);
        if (node.textContent.length && !localStorage.getItem(node.textContent)) {
            res.push(node.textContent);
        }
    }
    reportTestState('new:' + res.length);
    return res;
}

function showNotifications(items) {
    var i;
    for (i = 0; i < items.length; i++) {
        Unity.Notification.showNotification("CNN", items[i], null);
    }
    markAsRead(items);
}

function messagingIndicatorSetup() {
    var iframe = document.createElement('iframe');
    iframe.width = 0;
    iframe.height = 0;
    iframe.src = "http://edition.cnn.com/?sdas";
    document.body.appendChild(iframe);
    markAsRead(getHeaders(document));

    function retry() {
        iframe.src = "http://edition.cnn.com/?sda";
    }
    iframe.onload = wrapCallback(function () {
        showNotifications(getHeaders(iframe.contentWindow.document));
        setTimeout(retry, 5000);
    });
    retry();
}

if (isCorrectPage()) {
    Unity.init({ name: "cnn-news",
                 homepage: 'http://edition.cnn.com/',
                 domain: 'cnn.com',
                 iconUrl: 'http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/intl/hdr-globe-central.gif',
                 crop: true,
                 onInit: wrapCallback(messagingIndicatorSetup) });
}