浏览代码

Loads analytics js dynamically by searching for it next to the library or by using a custom script url option passed to the library on initialisation.

dev1
damencho 8 年前
父节点
当前提交
be22ab769b
共有 4 个文件被更改,包括 32 次插入7 次删除
  1. 1
    0
      doc/API.md
  2. 7
    4
      modules/statistics/statistics.js
  3. 22
    2
      modules/util/ScriptUtil.js
  4. 2
    1
      package.json

+ 1
- 0
doc/API.md 查看文件

@@ -51,6 +51,7 @@ The ```options``` parameter is JS object with the following properties:
51 51
     10. disableSimulcast - boolean property. Enables/disables simulcast.
52 52
     11. enableWindowOnErrorHandler - boolean property (default false). Enables/disables attaching global onerror handler (window.onerror).
53 53
     12. disableThirdPartyRequests - if true - callstats will be disabled and the callstats API won't be included.
54
+    13. analyticsScriptUrl - (optional) custom url to search for the analytics lib, if missing js file will be expected to be next to the library file (the location it is sourced from) 
54 55
 
55 56
 * ```JitsiMeetJS.JitsiConnection``` - the ```JitsiConnection``` constructor. You can use that to create new server connection.
56 57
 

+ 7
- 4
modules/statistics/statistics.js 查看文件

@@ -41,11 +41,14 @@ function loadCallStatsAPI() {
41 41
 // download the API asynchronously. Additionally, Google Analytics will download
42 42
 // its implementation asynchronously anyway so it makes sense to append the
43 43
 // loading on our side rather than prepend it.
44
-function loadAnalytics() {
44
+function loadAnalytics(customScriptUrl) {
45
+    // if we have a custom script url passed as parameter we don't want to
46
+    // search it relatively near the library
45 47
     JitsiMeetJS.util.ScriptUtil.loadScript(
46
-        'analytics.js?v=1',
48
+        customScriptUrl ? customScriptUrl : 'analytics.js',
47 49
         /* async */ true,
48
-        /* prepend */ false);
50
+        /* prepend */ false,
51
+        /* relativeURL */ customScriptUrl ? false : true);
49 52
 }
50 53
 
51 54
 /**
@@ -96,7 +99,7 @@ Statistics.init = function (options) {
96 99
     Statistics.disableThirdPartyRequests = options.disableThirdPartyRequests;
97 100
 
98 101
     if (Statistics.disableThirdPartyRequests !== true)
99
-        loadAnalytics();
102
+        loadAnalytics(options.analyticsScriptUrl);
100 103
 }
101 104
 
102 105
 function Statistics(xmpp, options) {

+ 22
- 2
modules/util/ScriptUtil.js 查看文件

@@ -1,3 +1,6 @@
1
+var currentExecutingScript = require("current-executing-script");
2
+
3
+
1 4
 /**
2 5
  * Implements utility functions which facilitate the dealing with scripts such
3 6
  * as the download and execution of a JavaScript file.
@@ -12,21 +15,38 @@ var ScriptUtil = {
12 15
      * @param prepend true to schedule the loading of the script as soon as
13 16
      * possible or false to schedule the loading of the script at the end of the
14 17
      * scripts known at the time
18
+     * @param relativeURL whether we need load the library from url relative
19
+     * to the url that lib-jitsi-meet was loaded. Useful when sourcing the
20
+     * library from different location than the app that is using it
15 21
      */
16
-    loadScript: function (src, async, prepend) {
22
+    loadScript: function (src, async, prepend, relativeURL) {
17 23
         var d = document;
18 24
         var tagName = 'script';
19 25
         var script = d.createElement(tagName);
20 26
         var referenceNode = d.getElementsByTagName(tagName)[0];
21 27
 
22 28
         script.async = async;
29
+
30
+        if (relativeURL) {
31
+            // finds the src url of the current loaded script
32
+            // and use it as base of the src supplied argument
33
+            var scriptEl = currentExecutingScript();
34
+            if(scriptEl) {
35
+                var scriptSrc = scriptEl.src;
36
+                var baseScriptSrc
37
+                    = scriptSrc.substring(0, scriptSrc.lastIndexOf('/') + 1);
38
+                if (scriptSrc && baseScriptSrc)
39
+                    src = baseScriptSrc + src;
40
+            }
41
+        }
42
+
23 43
         script.src = src;
24 44
         if (prepend) {
25 45
             referenceNode.parentNode.insertBefore(script, referenceNode);
26 46
         } else {
27 47
             referenceNode.parentNode.appendChild(script);
28 48
         }
29
-    },
49
+    }
30 50
 };
31 51
 
32 52
 module.exports = ScriptUtil;

+ 2
- 1
package.json 查看文件

@@ -27,7 +27,8 @@
27 27
     "jitsi-meet-logger": "git+https://github.com/jitsi/jitsi-meet-logger.git",
28 28
     "strophe": "^1.2.2",
29 29
     "strophejs-plugins": "^0.0.6",
30
-    "socket.io-client": "1.3.6"
30
+    "socket.io-client": "1.3.6",
31
+    "current-executing-script": "*"
31 32
   },
32 33
   "devDependencies": {
33 34
     "browserify": "11.1.x",

正在加载...
取消
保存