~webapps/webapps-applications/precise

« back to all changes in this revision

Viewing changes to src/facebook-apps.user.js.in

  • Committer: Ken VanDine
  • Date: 2012-03-09 21:17:01 UTC
  • mfrom: (80.1.94 webapps-applications)
  • Revision ID: ken.vandine@canonical.com-20120309211701-ul021ujf7ppegwd2
Tags: 0.9.1-0precise1
releasing version 0.9.1-0precise1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ==UserScript==
 
2
// @name          facebook-apps-launcher
 
3
// @include       http://apps.facebook.com/*/*
 
4
// @include       https://apps.facebook.com/*/*
 
5
// @version       @VERSION@
 
6
// @author        WebApps Team
 
7
// ==/UserScript==
 
8
 
 
9
 
 
10
function isCorrectPage() {
 
11
    if (!document.getElementById('pagelet_canvas_nav_content')) {
 
12
        return false;
 
13
    }
 
14
    if (parent.document !== document) {
 
15
        return false;
 
16
    }
 
17
    return true;
 
18
}
 
19
 
 
20
function urlToAppName(url) {
 
21
    var tmp = 'apps.facebook.com/';
 
22
    tmp = url.substr(url.indexOf(tmp) + tmp.length);
 
23
    return tmp.substr(0, tmp.indexOf('/'));
 
24
}
 
25
 
 
26
function registerApp() {
 
27
    var i, appName = window.location.pathname;
 
28
    appName = appName.substr(1, appName.indexOf('/', 1) - 1);
 
29
 
 
30
    var snapshot = document.evaluate('//div[@id="pagelet_canvas_nav_content"]/div/table/tbody/tr/td/div/div[@class="bookmark"]/a',
 
31
                                     document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
 
32
    var name, img, printableName;
 
33
    for (i = 0; i < snapshot.snapshotLength; i++) {
 
34
        var node = snapshot.snapshotItem(i);
 
35
        name = urlToAppName(node.href);
 
36
 
 
37
        if (name === appName) {
 
38
            img = node.lastChild.src;
 
39
            printableName = node.getAttribute('data-appname');
 
40
            if (!img) {
 
41
                return false;
 
42
            } else {
 
43
                break;
 
44
            }
 
45
        }
 
46
    }
 
47
 
 
48
    if (img && printableName) {
 
49
        Unity.toDataURL(img, function (aResult, uri) {
 
50
            Unity.init({ name: printableName,
 
51
                         iconUrl: uri,
 
52
                         homepage: 'http://apps.facebook.com/' + appName + '/',
 
53
                         onInit: null });
 
54
        });
 
55
        return true;
 
56
    }
 
57
    return false;
 
58
}
 
59
 
 
60
if (isCorrectPage()) {
 
61
    setTimeout(function retry() {
 
62
        if (!registerApp()) {
 
63
            setTimeout(retry, 10000);
 
64
        }
 
65
    }, 1000);
 
66
}