/usr/share/unity-webapps/userscripts/common/webapp.js is in unity-webapps-common 2.4.17+15.10.20150616-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | function WebApp(appInfo, callbacks) {
this._init(appInfo, callbacks);
}
WebApp.match = { FIRST: 0, ANY: 1, ALL: 2 };
WebApp.prototype = {
_init: function (appInfo, callbacks) {
this._appInfo = appInfo;
this._totalWeight = 0.0;
this._validItems = [];
this._callbacks = callbacks;
this._retries = 0;
},
_unityLoaded: function () {
try {
var i, nodeFunction = function () {
return document.evaluate("(" + this.node + ")[" + this.index + "]",
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null).snapshotItem(0);
};
for (i = 0; i < this._validItems.length; i++) {
var item = this._validItems[i];
if (item.install !== undefined) {
item.install(nodeFunction.bind(item));
}
}
} catch (e) {
console.log("Exception attempting item install = " + e);
}
var indicatorsController = new Indicators(function () {
return this._unityCallback();
}.bind(this));
if (this._callbacks.loaded !== undefined) {
this._reportInfo("calling loaded callback");
this._callbacks.loaded(indicatorsController);
}
},
setupPage: function () {
function nodeValue(node) {
if (typeof node === 'string') {
var resultSet = document.evaluate(node,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null);
var i, arrayized = [];
for (i = 0; i < resultSet.snapshotLength; i++) {
arrayized.push(resultSet.snapshotItem(i));
}
return arrayized;
} else if (typeof node === 'function') {
return node();
}
return [];
}
// Can we skip?
if (this._totalWeight > 0.0) {
if (this._appInfo.validator(this._totalWeight)) {
return true;
}
this._totalWeight = 0.0;
}
var pageData = {};
var i = 0, j = 0, k = 0;
// Try and find login
// Always collect login if available
if (this._appInfo.login !== undefined) {
var loginTests = this._appInfo.login;
for (i = 0; i < loginTests.nodes.length; i++) {
var nodeTest = {
name: loginTests.name,
query: loginTests.nodes[i],
validator: loginTests.validator,
fragment: loginTests.fragment,
value: loginTests.value
};
var loginNode = validatedNode(nodeTest);
if (loginNode !== null) {
this._login = validatedNodeValue(nodeTest, loginNode);
}
}
if (this._login === undefined) {
if (this._shouldRetry()) {
this._reportWarning("Failed to find login will retry");
return false;
} else {
this._reportError("Unable to obtain login information");
return true;
}
} else {
this._reportInfo("Found login: " + this._login);
}
}
// Check all items
var itemsTests = this._appInfo.items;
var valueFunction = function () {
var result = document.evaluate("(" + this.node + ")[" + this.index + "]",
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
return this.nativeValue(result);
};
for (i = 0; i < itemsTests.length; i++) {
var found = false;
var item = itemsTests[i];
for (j = 0; j < item.nodes.length; j++) {
var nodeSet = nodeValue(item.nodes[j]);
for (k = 0; k < nodeSet.length; k++) {
var node = nodeSet[k];
if (item.validator(node)) {
if (item.weight) {
this._totalWeight += item.weight;
}
var itemData = {
name: item.name,
node: item.nodes[j],
index: k + 1
};
if (item.value !== undefined) {
itemData.nativeValue = item.value;
itemData.value = valueFunction;
}
if (item.install !== undefined) {
itemData.install = item.install;
}
// Save the node
this._validItems.push(itemData);
this._reportInfo("Found item - " + item.name);
found = true;
if (item.match === WebApp.match.FIRST || item.match === WebApp.match.ANY) {
break;
}
}
}
if (found) {
break;
}
}
if (!found) {
if (item.fragment !== undefined) {
var fragmentOk, resultSet, testNode = document.createElement("div");
testNode.innerHTML = item.fragment;
resultSet = document.evaluate(item.nodes[0], testNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
fragmentOk = resultSet.snapshotLength > 0 && item.validator(resultSet.snapshotItem(0));
this._reportWarning("Item not found " + item.name + " fragment check " + (fragmentOk ? "OK" : "failed"));
} else {
this._reportWarning("Item not found: " + item.name);
}
}
}
// Should we continue?
if (!this._appInfo.validator(this._totalWeight)) {
if (this._shouldRetry()) {
this._reportWarning("Did not retrieve enough information to instantiate app, will retry");
return false;
} else {
this._reportFailure(this._totalWeght);
return true;
}
}
// Build Pagedata for Unity.init()
pageData.name = this._valueFromField(this._appInfo.name);
pageData.iconUrl = this._valueFromField(this._appInfo.iconUrl);
pageData.homepage = this._valueFromField(this._appInfo.homepage);
pageData.domain = this._valueFromField(this._appInfo.domain);
pageData.login = this._login;
pageData.onInit = function () { this._unityLoaded(); }.bind(this);
if (this._callbacks.success !== undefined) {
this._reportInfo("calling success callback");
this._callbacks.success(pageData);
}
return true;
},
_unityCallback: function () {
// Collect all values
var indicators = [];
var i = 0;
for (i = 0; i < this._validItems.length; i++) {
var item = this._validItems[i];
if (item.value !== undefined) {
indicators.push(item.value());
}
}
return indicators;
},
_valueFromField: function (value) {
// Field can either be a value or a function to compute the value
if (typeof value === 'function') {
return value();
}
return value;
},
_reportInfo: function (msg) {
if (this._callbacks.report) {
this._callbacks.report("REPORT: INFO: " + msg);
}
},
_reportWarning: function (msg) {
if (this._callbacks.report) {
this._callbacks.report("REPORT: WARNING: " + msg);
}
},
_reportError: function (msg) {
if (this._callbacks.report) {
this._callbacks.report("REPORT: ERROR: " + msg);
}
},
_reportFailure: function (weight) {
if (this._callbacks.report) {
this._callbacks.report("REPORT: ERROR: Failed to pass sufficient tests to continue " + weight);
}
},
_shouldRetry: function () {
if (this._appInfo.maxRetries !== undefined) {
return this._retries++ < this._appInfo.maxRetries;
}
return true;
}
};
|