2013-10-17 23:01:13 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*jslint sloppy:true */
|
|
|
|
/*global Windows:true, require, document, setTimeout, window, module */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var cordova = require('cordova'),
|
2014-10-15 03:07:15 +04:00
|
|
|
channel = require('cordova/channel'),
|
|
|
|
urlutil = require('cordova/urlutil');
|
2013-10-17 23:01:13 +02:00
|
|
|
|
|
|
|
var browserWrap,
|
2014-09-19 10:33:53 +04:00
|
|
|
popup;
|
|
|
|
|
|
|
|
// x-ms-webview is available starting from Windows 8.1 (platformId is 'windows')
|
|
|
|
// http://msdn.microsoft.com/en-us/library/windows/apps/dn301831.aspx
|
|
|
|
var isWebViewAvailable = cordova.platformId == 'windows';
|
|
|
|
|
|
|
|
function attachNavigationEvents(element, callback) {
|
|
|
|
if (isWebViewAvailable) {
|
|
|
|
element.addEventListener("MSWebViewNavigationStarting", function (e) {
|
|
|
|
callback({ type: "loadstart", url: e.uri}, {keepCallback: true} );
|
|
|
|
});
|
|
|
|
element.addEventListener("MSWebViewNavigationCompleted", function (e) {
|
|
|
|
callback({ type: e.isSuccess ? "loadstop" : "loaderror", url: e.uri}, {keepCallback: true});
|
|
|
|
});
|
|
|
|
element.addEventListener("MSWebViewUnviewableContentIdentified", function (e) {
|
|
|
|
// WebView found the content to be not HTML.
|
|
|
|
// http://msdn.microsoft.com/en-us/library/windows/apps/dn609716.aspx
|
|
|
|
callback({ type: "loaderror", url: e.uri}, {keepCallback: true});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var onError = function () {
|
|
|
|
callback({ type: "loaderror", url: this.contentWindow.location}, {keepCallback: true});
|
|
|
|
};
|
|
|
|
|
|
|
|
element.addEventListener("unload", function () {
|
|
|
|
callback({ type: "loadstart", url: this.contentWindow.location}, {keepCallback: true});
|
|
|
|
});
|
|
|
|
element.addEventListener("load", function () {
|
|
|
|
callback({ type: "loadstop", url: this.contentWindow.location}, {keepCallback: true});
|
|
|
|
});
|
|
|
|
|
|
|
|
element.addEventListener("error", onError);
|
|
|
|
element.addEventListener("abort", onError);
|
2014-07-11 21:43:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 23:01:13 +02:00
|
|
|
var IAB = {
|
|
|
|
close: function (win, lose) {
|
|
|
|
if (browserWrap) {
|
2014-09-19 10:33:53 +04:00
|
|
|
if (win) win({ type: "exit" });
|
2013-10-17 23:01:13 +02:00
|
|
|
|
|
|
|
browserWrap.parentNode.removeChild(browserWrap);
|
|
|
|
browserWrap = null;
|
|
|
|
popup = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
show: function (win, lose) {
|
|
|
|
if (browserWrap) {
|
|
|
|
browserWrap.style.display = "block";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
open: function (win, lose, args) {
|
|
|
|
var strUrl = args[0],
|
|
|
|
target = args[1],
|
|
|
|
features = args[2],
|
|
|
|
url;
|
|
|
|
|
|
|
|
if (target === "_system") {
|
|
|
|
url = new Windows.Foundation.Uri(strUrl);
|
|
|
|
Windows.System.Launcher.launchUriAsync(url);
|
2014-10-15 02:52:28 +04:00
|
|
|
} else if (target === "_self" || !target) {
|
|
|
|
window.location = strUrl;
|
|
|
|
} else {
|
|
|
|
// "_blank" or anything else
|
2013-10-17 23:01:13 +02:00
|
|
|
if (!browserWrap) {
|
|
|
|
browserWrap = document.createElement("div");
|
|
|
|
browserWrap.style.position = "absolute";
|
|
|
|
browserWrap.style.borderWidth = "40px";
|
2014-09-19 10:33:53 +04:00
|
|
|
browserWrap.style.width = "calc(100% - 80px)";
|
|
|
|
browserWrap.style.height = "calc(100% - 80px)";
|
2013-10-17 23:01:13 +02:00
|
|
|
browserWrap.style.borderStyle = "solid";
|
|
|
|
browserWrap.style.borderColor = "rgba(0,0,0,0.25)";
|
|
|
|
|
|
|
|
browserWrap.onclick = function () {
|
|
|
|
setTimeout(function () {
|
|
|
|
IAB.close();
|
|
|
|
}, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
document.body.appendChild(browserWrap);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (features.indexOf("hidden=yes") !== -1) {
|
|
|
|
browserWrap.style.display = "none";
|
|
|
|
}
|
|
|
|
|
2014-09-19 10:33:53 +04:00
|
|
|
popup = document.createElement(isWebViewAvailable ? "x-ms-webview" : "iframe");
|
2014-07-11 21:43:37 +02:00
|
|
|
popup.style.borderWidth = "0px";
|
2014-09-19 10:33:53 +04:00
|
|
|
popup.style.width = "100%";
|
|
|
|
popup.style.height = "100%";
|
2014-10-15 02:52:28 +04:00
|
|
|
|
|
|
|
if (isWebViewAvailable) {
|
|
|
|
strUrl = strUrl.replace("ms-appx://", "ms-appx-web://");
|
|
|
|
}
|
2013-10-17 23:01:13 +02:00
|
|
|
popup.src = strUrl;
|
|
|
|
|
2014-09-19 10:33:53 +04:00
|
|
|
// start listening for navigation events
|
|
|
|
attachNavigationEvents(popup, win);
|
2013-10-17 23:01:13 +02:00
|
|
|
|
|
|
|
browserWrap.appendChild(popup);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
injectScriptCode: function (win, fail, args) {
|
|
|
|
var code = args[0],
|
2014-09-19 10:33:53 +04:00
|
|
|
hasCallback = args[1];
|
2013-10-17 23:01:13 +02:00
|
|
|
|
2014-09-19 10:33:53 +04:00
|
|
|
if (isWebViewAvailable && browserWrap && popup) {
|
2013-10-17 23:01:13 +02:00
|
|
|
var op = popup.invokeScriptAsync("eval", code);
|
|
|
|
op.oncomplete = function () { hasCallback && win([]); };
|
|
|
|
op.onerror = function () { };
|
|
|
|
op.start();
|
|
|
|
}
|
|
|
|
},
|
2014-10-15 03:07:15 +04:00
|
|
|
|
2013-10-17 23:01:13 +02:00
|
|
|
injectScriptFile: function (win, fail, args) {
|
2014-10-15 03:07:15 +04:00
|
|
|
var filePath = args[0],
|
2014-09-19 10:33:53 +04:00
|
|
|
hasCallback = args[1];
|
2013-10-17 23:01:13 +02:00
|
|
|
|
2014-10-15 03:07:15 +04:00
|
|
|
if (!!filePath) {
|
|
|
|
filePath = urlutil.makeAbsolute(filePath);
|
|
|
|
}
|
|
|
|
|
2014-09-19 10:33:53 +04:00
|
|
|
if (isWebViewAvailable && browserWrap && popup) {
|
2014-10-15 03:07:15 +04:00
|
|
|
var uri = new Windows.Foundation.Uri(filePath);
|
|
|
|
Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).done(function (file) {
|
|
|
|
Windows.Storage.FileIO.readTextAsync(file).done(function (code) {
|
|
|
|
var op = popup.invokeScriptAsync("eval", code);
|
|
|
|
op.oncomplete = function () { hasCallback && win([]); };
|
|
|
|
op.onerror = function () { };
|
|
|
|
op.start();
|
|
|
|
});
|
2013-10-17 23:01:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = IAB;
|
|
|
|
|
2014-09-19 10:33:53 +04:00
|
|
|
require("cordova/exec/proxy").add("InAppBrowser", module.exports);
|