-
Notifications
You must be signed in to change notification settings - Fork 3
/
skin.js
85 lines (77 loc) · 2.95 KB
/
skin.js
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
82
83
84
85
; (function (exports, doc) {
var skinPayloadUrl = "out/payload.js",
onLoadLib,
skinPayLoadTag = doc.createElement("script"),
detector = /(?:skin\.js\??([^\/].*?)$)/i,
scriptTags = doc.getElementsByTagName("script"),
libParamMatches,
paramPairs = [0],
appId = "",
authToken = "",
authCodeSrc = "",
callBackSrc = "",
authenticateAndSkin,
finalCallBack,
tmpParamValPair = [0],
tmpParam = "",
tmpParamValue = "";
// Find ourselves among the other scripts
for (var i=0, scriptTag; scriptTag=scriptTags[i++];) {
if (scriptTag.src) {
if (libParamMatches = detector.exec(scriptTag.src)) {
break;
}
}
}
// Build callback invocation source and authentication calls
if (libParamMatches.length == 2) {
paramPairs = (libParamMatches[1]).split("&");
for (var i=0, j=paramPairs.length; i < j; i++) {
tmpParamValPair = paramPairs[i].split("=");
if (tmpParamValPair.length == 2) {
tmpParam = tmpParamValPair[0];
tmpParamValue = tmpParamValPair[1];
if (tmpParam && tmpParamValue) {
switch(tmpParam.toLowerCase()) {
case "callback":
callBackSrc = tmpParamValue + "();";
break;
case "appid":
appId = tmpParamValue;
break;
case "token":
authToken = tmpParamValue;
break;
}
}
}
}
}
// Build post jsl-load script source
if (appId.length > 0 && authToken.length > 0) {
authCodeSrc = "nokia.Settings.set(\"appId\", \"" + appId + "\");\n" +
"nokia.Settings.set(\"authenticationToken\", \"" + authToken + "\");\n";
}
authenticateAndSkin = function () {
(new Function(authCodeSrc))(); // invoke auth with Nokia
// appendSkinScript
doc.head.appendChild(skinPayLoadTag);
}
// Prepare callback when everything else is done
finalCallBack = new Function(callBackSrc);
// Prepare skin lib script
skinPayLoadTag.setAttribute("type", "text/javascript");
skinPayLoadTag.setAttribute("src", skinPayloadUrl);
// Detect when the skin script has loaded and invoke the final callback specified in the skin.js url declaration
if (skinPayLoadTag.readyState) {
script.onreadystatechange = function() {
if (skinPayLoadTag.readyState === 'loaded' || dropInLibTag.readyState === 'complete') {
skinPayLoadTag.onreadystatechange = null;
finalCallBack();
}
};
} else {
skinPayLoadTag.onload = finalCallBack;
}
authenticateAndSkin();
})(window, document);