-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebview.js
70 lines (55 loc) · 1.71 KB
/
webview.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
// JS library for Unity webview plugin 1.0.4 Copyright (C) 2014 Yuya Hashimoto, zlib License.
// See https://github.com/yuya/unity-webview-plugin
;(function (global, document) {
var URL_SCHEME = "webviewbridge://",
queue = [],
body = document.body,
iframeBase = document.createElement("iframe"),
inlineCSS = "position: absolute; width: 1px; height: 1px; border: none; visibility: hidden;",
isAndroid = /Android/.test(navigator.userAgent),
WebViewMediator;
iframeBase.setAttribute("style", inlineCSS);
function each(collection, iterator) {
var i = 0, len, ary, key;
if (Array.isArray(collection)) {
len = collection.length;
for (; len; ++i, --len) {
iterator(collection[i], i);
}
}
else {
ary = Object.keys(collection);
len = ary.length;
for (; len; ++i, --len) {
key = ary[i];
iterator(key, collection[key]);
}
}
}
function callCustomURLScheme() {
var iframe = iframeBase.cloneNode(false);
iframe.src = URL_SCHEME;
body.appendChild(iframe);
body.removeChild(iframe);
iframe = null;
}
WebViewMediator = {
Call: function (path, args) {
var message = isAndroid ? URL_SCHEME + path : path,
stack;
if (args) {
stack = [];
each(args, function (key) {
stack.push(key + "=" + encodeURIComponent(args[key]));
});
message += "?" + stack.join("&");
}
queue.push(message);
callCustomURLScheme();
},
ShiftQueue: function () {
return queue.length ? queue.shift() : "";
}
};
global.WebViewMediator = WebViewMediator;
})(this, this.document);