2014-12-16 00:15:01 +08: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-02-19 20:39:28 +08:00
|
|
|
var modulemapper = require('cordova/modulemapper');
|
2014-12-16 00:15:01 +08:00
|
|
|
|
|
|
|
var browserWrap,
|
|
|
|
popup,
|
|
|
|
navigationButtonsDiv,
|
|
|
|
navigationButtonsDivInner,
|
|
|
|
backButton,
|
|
|
|
forwardButton,
|
|
|
|
closeButton;
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
function attachNavigationEvents (element, callback) {
|
2014-12-16 00:15:01 +08:00
|
|
|
var onError = function () {
|
2017-07-27 20:55:11 +08:00
|
|
|
try {
|
2017-06-13 01:34:08 +08:00
|
|
|
callback({ type: 'loaderror', url: this.contentWindow.location.href }, {keepCallback: true}); // eslint-disable-line standard/no-callback-literal
|
2017-07-27 20:55:11 +08:00
|
|
|
} catch (err) {
|
|
|
|
// blocked by CORS :\
|
2017-06-13 01:34:08 +08:00
|
|
|
callback({ type: 'loaderror', url: null }, {keepCallback: true}); // eslint-disable-line standard/no-callback-literal
|
2017-07-27 20:55:11 +08:00
|
|
|
}
|
2014-12-16 00:15:01 +08:00
|
|
|
};
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
element.addEventListener('pageshow', function () {
|
2017-07-27 20:55:11 +08:00
|
|
|
try {
|
2017-06-13 01:34:08 +08:00
|
|
|
callback({ type: 'loadstart', url: this.contentWindow.location.href }, {keepCallback: true}); // eslint-disable-line standard/no-callback-literal
|
2017-07-27 20:55:11 +08:00
|
|
|
} catch (err) {
|
|
|
|
// blocked by CORS :\
|
2017-06-13 01:34:08 +08:00
|
|
|
callback({ type: 'loadstart', url: null }, {keepCallback: true}); // eslint-disable-line standard/no-callback-literal
|
2017-07-27 20:55:11 +08:00
|
|
|
}
|
2014-12-16 00:15:01 +08:00
|
|
|
});
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
element.addEventListener('load', function () {
|
2017-07-27 20:55:11 +08:00
|
|
|
try {
|
2017-06-13 01:34:08 +08:00
|
|
|
callback({ type: 'loadstop', url: this.contentWindow.location.href }, {keepCallback: true}); // eslint-disable-line standard/no-callback-literal
|
2017-07-27 20:55:11 +08:00
|
|
|
} catch (err) {
|
|
|
|
// blocked by CORS :\
|
2017-06-13 01:34:08 +08:00
|
|
|
callback({ type: 'loadstop', url: null }, {keepCallback: true}); // eslint-disable-line standard/no-callback-literal
|
2017-07-27 20:55:11 +08:00
|
|
|
}
|
2014-12-16 00:15:01 +08:00
|
|
|
});
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
element.addEventListener('error', onError);
|
|
|
|
element.addEventListener('abort', onError);
|
2014-12-16 00:15:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var IAB = {
|
|
|
|
close: function (win, lose) {
|
|
|
|
if (browserWrap) {
|
2017-07-27 20:55:11 +08:00
|
|
|
// use the "open" function callback so that the exit event is fired properly
|
2017-06-13 01:34:08 +08:00
|
|
|
if (IAB._win) IAB._win({ type: 'exit' });
|
2014-12-16 00:15:01 +08:00
|
|
|
|
|
|
|
browserWrap.parentNode.removeChild(browserWrap);
|
|
|
|
browserWrap = null;
|
|
|
|
popup = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
show: function (win, lose) {
|
|
|
|
if (browserWrap) {
|
2017-06-13 01:34:08 +08:00
|
|
|
browserWrap.style.display = 'block';
|
2014-12-16 00:15:01 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
open: function (win, lose, args) {
|
2017-06-13 01:34:08 +08:00
|
|
|
var strUrl = args[0];
|
|
|
|
var target = args[1];
|
|
|
|
var features = args[2];
|
2014-12-16 00:15:01 +08:00
|
|
|
|
2017-07-27 20:55:11 +08:00
|
|
|
IAB._win = win;
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
if (target === '_self' || !target) {
|
2014-12-16 00:15:01 +08:00
|
|
|
window.location = strUrl;
|
2017-06-13 01:34:08 +08:00
|
|
|
} else if (target === '_system') {
|
|
|
|
modulemapper.getOriginalSymbol(window, 'window.open').call(window, strUrl, '_blank');
|
2014-12-16 00:15:01 +08:00
|
|
|
} else {
|
|
|
|
// "_blank" or anything else
|
|
|
|
if (!browserWrap) {
|
2017-06-13 01:34:08 +08:00
|
|
|
browserWrap = document.createElement('div');
|
|
|
|
browserWrap.style.position = 'absolute';
|
|
|
|
browserWrap.style.top = '0';
|
|
|
|
browserWrap.style.left = '0';
|
|
|
|
browserWrap.style.boxSizing = 'border-box';
|
|
|
|
browserWrap.style.borderWidth = '40px';
|
|
|
|
browserWrap.style.width = '100vw';
|
|
|
|
browserWrap.style.height = '100vh';
|
|
|
|
browserWrap.style.borderStyle = 'solid';
|
|
|
|
browserWrap.style.borderColor = 'rgba(0,0,0,0.25)';
|
2014-12-16 00:15:01 +08:00
|
|
|
|
|
|
|
browserWrap.onclick = function () {
|
|
|
|
setTimeout(function () {
|
2017-07-27 20:55:11 +08:00
|
|
|
IAB.close();
|
2014-12-16 00:15:01 +08:00
|
|
|
}, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
document.body.appendChild(browserWrap);
|
|
|
|
}
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
if (features.indexOf('hidden=yes') !== -1) {
|
|
|
|
browserWrap.style.display = 'none';
|
2014-12-16 00:15:01 +08:00
|
|
|
}
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
popup = document.createElement('iframe');
|
|
|
|
popup.style.borderWidth = '0px';
|
|
|
|
popup.style.width = '100%';
|
2014-12-16 00:15:01 +08:00
|
|
|
|
|
|
|
browserWrap.appendChild(popup);
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
if (features.indexOf('location=yes') !== -1 || features.indexOf('location') === -1) {
|
|
|
|
popup.style.height = 'calc(100% - 60px)';
|
|
|
|
popup.style.marginBottom = '-4px';
|
2014-12-16 00:15:01 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
navigationButtonsDiv = document.createElement('div');
|
|
|
|
navigationButtonsDiv.style.height = '60px';
|
|
|
|
navigationButtonsDiv.style.backgroundColor = '#404040';
|
|
|
|
navigationButtonsDiv.style.zIndex = '999';
|
2014-12-16 00:15:01 +08:00
|
|
|
navigationButtonsDiv.onclick = function (e) {
|
|
|
|
e.cancelBubble = true;
|
|
|
|
};
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
navigationButtonsDivInner = document.createElement('div');
|
|
|
|
navigationButtonsDivInner.style.paddingTop = '10px';
|
|
|
|
navigationButtonsDivInner.style.height = '50px';
|
|
|
|
navigationButtonsDivInner.style.width = '160px';
|
|
|
|
navigationButtonsDivInner.style.margin = '0 auto';
|
|
|
|
navigationButtonsDivInner.style.backgroundColor = '#404040';
|
|
|
|
navigationButtonsDivInner.style.zIndex = '999';
|
2014-12-16 00:15:01 +08:00
|
|
|
navigationButtonsDivInner.onclick = function (e) {
|
|
|
|
e.cancelBubble = true;
|
|
|
|
};
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
backButton = document.createElement('button');
|
|
|
|
backButton.style.width = '40px';
|
|
|
|
backButton.style.height = '40px';
|
|
|
|
backButton.style.borderRadius = '40px';
|
2014-12-16 00:15:01 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
backButton.innerHTML = '←';
|
|
|
|
backButton.addEventListener('click', function (e) {
|
|
|
|
if (popup.canGoBack) { popup.goBack(); }
|
2014-12-16 00:15:01 +08:00
|
|
|
});
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
forwardButton = document.createElement('button');
|
|
|
|
forwardButton.style.marginLeft = '20px';
|
|
|
|
forwardButton.style.width = '40px';
|
|
|
|
forwardButton.style.height = '40px';
|
|
|
|
forwardButton.style.borderRadius = '40px';
|
2014-12-16 00:15:01 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
forwardButton.innerHTML = '→';
|
|
|
|
forwardButton.addEventListener('click', function (e) {
|
|
|
|
if (popup.canGoForward) { popup.goForward(); }
|
2014-12-16 00:15:01 +08:00
|
|
|
});
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
closeButton = document.createElement('button');
|
|
|
|
closeButton.style.marginLeft = '20px';
|
|
|
|
closeButton.style.width = '40px';
|
|
|
|
closeButton.style.height = '40px';
|
|
|
|
closeButton.style.borderRadius = '40px';
|
2014-12-16 00:15:01 +08:00
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
closeButton.innerHTML = '✖';
|
|
|
|
closeButton.addEventListener('click', function (e) {
|
2014-12-16 00:15:01 +08:00
|
|
|
setTimeout(function () {
|
2017-07-27 20:55:11 +08:00
|
|
|
IAB.close();
|
2014-12-16 00:15:01 +08:00
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// iframe navigation is not yet supported
|
|
|
|
backButton.disabled = true;
|
|
|
|
forwardButton.disabled = true;
|
|
|
|
|
|
|
|
navigationButtonsDivInner.appendChild(backButton);
|
|
|
|
navigationButtonsDivInner.appendChild(forwardButton);
|
|
|
|
navigationButtonsDivInner.appendChild(closeButton);
|
|
|
|
navigationButtonsDiv.appendChild(navigationButtonsDivInner);
|
|
|
|
|
|
|
|
browserWrap.appendChild(navigationButtonsDiv);
|
|
|
|
} else {
|
2017-06-13 01:34:08 +08:00
|
|
|
popup.style.height = '100%';
|
2014-12-16 00:15:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// start listening for navigation events
|
|
|
|
attachNavigationEvents(popup, win);
|
|
|
|
|
|
|
|
popup.src = strUrl;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
injectScriptCode: function (win, fail, args) {
|
2017-06-13 01:34:08 +08:00
|
|
|
var code = args[0];
|
|
|
|
var hasCallback = args[1];
|
2014-12-16 00:15:01 +08:00
|
|
|
|
|
|
|
if (browserWrap && popup) {
|
|
|
|
try {
|
|
|
|
popup.contentWindow.eval(code);
|
2016-02-19 20:39:28 +08:00
|
|
|
if (hasCallback) {
|
|
|
|
win([]);
|
|
|
|
}
|
2017-06-13 01:34:08 +08:00
|
|
|
} catch (e) {
|
2014-12-16 00:15:01 +08:00
|
|
|
console.error('Error occured while trying to injectScriptCode: ' + JSON.stringify(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
injectScriptFile: function (win, fail, args) {
|
2015-04-02 07:34:36 +08:00
|
|
|
var msg = 'Browser cordova-plugin-inappbrowser injectScriptFile is not yet implemented';
|
2014-12-16 00:15:01 +08:00
|
|
|
console.warn(msg);
|
2016-02-19 20:39:28 +08:00
|
|
|
if (fail) {
|
|
|
|
fail(msg);
|
|
|
|
}
|
2017-06-13 01:34:08 +08:00
|
|
|
},
|
2014-12-16 00:15:01 +08:00
|
|
|
|
|
|
|
injectStyleCode: function (win, fail, args) {
|
2015-04-02 07:34:36 +08:00
|
|
|
var msg = 'Browser cordova-plugin-inappbrowser injectStyleCode is not yet implemented';
|
2014-12-16 00:15:01 +08:00
|
|
|
console.warn(msg);
|
2016-02-19 20:39:28 +08:00
|
|
|
if (fail) {
|
|
|
|
fail(msg);
|
|
|
|
}
|
2014-12-16 00:15:01 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
injectStyleFile: function (win, fail, args) {
|
2015-04-02 07:34:36 +08:00
|
|
|
var msg = 'Browser cordova-plugin-inappbrowser injectStyleFile is not yet implemented';
|
2014-12-16 00:15:01 +08:00
|
|
|
console.warn(msg);
|
2016-02-19 20:39:28 +08:00
|
|
|
if (fail) {
|
|
|
|
fail(msg);
|
|
|
|
}
|
2014-12-16 00:15:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = IAB;
|
|
|
|
|
2017-06-13 01:34:08 +08:00
|
|
|
require('cordova/exec/proxy').add('InAppBrowser', module.exports);
|