aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/vapi-common.js
blob: 2997a111c01692da969ab03016e9df5c58bd5db4 (plain)
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
// only for background and other extension pages
/* global chrome, Components, BeforeLoadEvent */

(function() {
'use strict';

window.vAPI = window.vAPI || {};

if (window.chrome) {
    var chrome = window.chrome;

    vAPI.getURL = function(path) {
        return chrome.runtime.getURL(path);
    };

    vAPI.i18n = function(s) {
        return chrome.i18n.getMessage(s) || s;
    };
} else if (window.safari) {
    vAPI.getURL = function(path) {
        return safari.extension.baseURI + path;
    };

    var xhr = new XMLHttpRequest;
    xhr.overrideMimeType('application/json;charset=utf-8');
    xhr.open('GET', './locales.json', false);
    xhr.send();
    vAPI.i18nData = JSON.parse(xhr.responseText);

    if (vAPI.i18nData[vAPI.i18n = navigator.language.replace('-', '_')]
        || vAPI.i18nData[vAPI.i18n = vAPI.i18n.slice(0, 2)]) {
        vAPI.i18nAlpha2 = vAPI.i18n;
    } else {
        vAPI.i18nAlpha2 = vAPI.i18nData._;
    }

    xhr = new XMLHttpRequest;
    xhr.overrideMimeType('application/json;charset=utf-8');
    xhr.open('GET', './_locales/' + vAPI.i18nAlpha2 + '/messages.json', false);
    xhr.send();
    vAPI.i18nData = JSON.parse(xhr.responseText);

    for (var i18nKey in vAPI.i18nData) {
        vAPI.i18nData[i18nKey] = vAPI.i18nData[i18nKey].message;
    }

    vAPI.i18n = function(s) {
        return this.i18nData[s] || s;
    };

    // update popover size to its content
    if (safari.self.identifier === 'popover' && safari.self) {
        vAPI.updatePopoverSize = function() {
            safari.self.width = document.body.clientWidth;
            safari.self.height = document.body.clientHeight;
        };

        setTimeout(vAPI.updatePopoverSize, 200);
    }
}

})();