From e2865db2eae80a2ab37e951856a39540f6534e16 Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Wed, 1 Apr 2015 16:57:58 +0300 Subject: [PATCH] CB-7689 Adds insertCSS support for windows platform --- README.md | 1 + src/windows/InAppBrowserProxy.js | 43 +++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a7c2df..31a3c72 100644 --- a/README.md +++ b/README.md @@ -377,6 +377,7 @@ Due to [MSDN docs](https://msdn.microsoft.com/en-us/library/windows.ui.xaml.cont - Amazon Fire OS - Android - iOS +- Windows ### Quick Example diff --git a/src/windows/InAppBrowserProxy.js b/src/windows/InAppBrowserProxy.js index cee0de9..817516e 100644 --- a/src/windows/InAppBrowserProxy.js +++ b/src/windows/InAppBrowserProxy.js @@ -213,7 +213,7 @@ var IAB = { IAB.close(win); }, 0); }); - + if (!isWebViewAvailable) { // iframe navigation is not yet supported backButton.disabled = true; @@ -277,9 +277,50 @@ var IAB = { }); }); } + }, + + injectStyleCode: function (win, fail, args) { + var code = args[0], + hasCallback = args[1]; + + if (isWebViewAvailable && browserWrap && popup) { + injectCSS(popup, code, hasCallback && win); + } + }, + + injectStyleFile: function (win, fail, args) { + var filePath = args[0], + hasCallback = args[1]; + + filePath = filePath && urlutil.makeAbsolute(filePath); + + if (isWebViewAvailable && browserWrap && popup) { + var uri = new Windows.Foundation.Uri(filePath); + Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) { + return Windows.Storage.FileIO.readTextAsync(file); + }).done(function (code) { + injectCSS(popup, code, hasCallback && win); + }, function () { + // no-op, just catch an error + }); + } } }; +function injectCSS (webView, cssCode, callback) { + // This will automatically escape all thing that we need (quotes, slashes, etc.) + var escapedCode = JSON.stringify(cssCode); + var evalWrapper = "(function(d){var c=d.createElement('style');c.innerHTML=%s;d.head.appendChild(c);})(document)" + .replace('%s', escapedCode); + + var op = webView.invokeScriptAsync("eval", evalWrapper); + op.oncomplete = function() { + callback && callback([]); + }; + op.onerror = function () { }; + op.start(); +} + module.exports = IAB; require("cordova/exec/proxy").add("InAppBrowser", module.exports);