~abreu-alexandre/webapps-applications/fix-migration-script-icon-order

« back to all changes in this revision

Viewing changes to src/AmazonCloudReader.user.js.in

  • Committer: Ken VanDine
  • Date: 2012-06-21 16:51:22 UTC
  • mfrom: (80.1.212 webapps-applications)
  • Revision ID: ken.vandine@canonical.com-20120621165122-ejgx1uo1pdn4snqu
Tags: 1.4.0-0precise1
releasing version 1.4.0-0precise1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ==UserScript==
 
2
// @name           amazon-cloud-reader
 
3
// @include        https://read.amazon.com/*
 
4
// @version        @VERSION@
 
5
// @author         WebApps Team
 
6
// @require        utils.js
 
7
// ==/UserScript==
 
8
 
 
9
window.Unity = external.getUnityObject(1);
 
10
 
 
11
function getBookList() {
 
12
    var res = [];
 
13
    try {
 
14
        var doc = document.getElementById('KindleLibraryIFrame').contentDocument;
 
15
        var books = doc.getElementById('titles_inner_wrapper').children;
 
16
        var i;
 
17
 
 
18
        for (i = 0; i < books.length; i++) {
 
19
            var title = doc.evaluate('div/img', books[i], null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue.title;
 
20
            var link = 'https://read.amazon.com/?asin=' + books[i].id;
 
21
            var count = books.length - i;
 
22
 
 
23
            res.push({ name: title,
 
24
                       url: link,
 
25
                       count: count });
 
26
        }
 
27
        localStorage.setItem(FAVORITES, JSON.stringify(res));
 
28
        return res;
 
29
    } catch (x) {
 
30
        if (localStorage.getItem(FAVORITES)) {
 
31
            res = JSON.parse(localStorage.getItem(FAVORITES));
 
32
        }
 
33
    }
 
34
 
 
35
    return res;
 
36
}
 
37
 
 
38
function unityLoaded() {
 
39
    var count = 3;
 
40
    setTimeout(function self() {
 
41
        var list = getBookList();
 
42
        if (!list.length && count < 0) {
 
43
            list = JSON.parse(localStorage.getItem(FAVORITES));
 
44
            if (!list) {
 
45
                list = [];
 
46
            }
 
47
        }
 
48
        if (list.length) {
 
49
            addFavoritesInLauncher(list);
 
50
            return;
 
51
        }
 
52
        setTimeout(self, 6000);
 
53
        count--;
 
54
    }, 2000);
 
55
}
 
56
 
 
57
Unity.toDataURL('http://upload.wikimedia.org/wikipedia/commons/6/62/Amazon.com-Logo.svg', function (aResult, uri) {
 
58
    Unity.init({ name: "Amazon Cloud Reader",
 
59
                 iconUrl: uri,
 
60
                 onInit: wrapCallback(unityLoaded) });
 
61
});