private void Fire(string notification) { Deployment.Current.Dispatcher.BeginInvoke(() => { //替掉行符"\n"为"\\n",否则会引起js语句异常 notification = notification.Replace("\n", "\\n"); string jsString = string.Format("cordova.require('com.polyvi.xface.extension.push.PushNotification').fire('{0}');", notification); // Fire notification to js var Invoker = new XSafeBrowserScriptInvoker(); if (!Invoker.Exec(this.app.AppView.Browser, "eval", new string[] { jsString })) { XLog.WriteError("Failed Fire Push notification!"); } }); }
public bool HandleCommand(string commandStr) { string[] split = commandStr.Split('/'); if (split.Length > 3) { string api = split[0]; string type = split[1]; // localStorageHelper || sessionStorageHelper string command = split[2]; string param = split[3]; //key Dictionary<string, string> currentStorage = getStorageByType(type); XSafeBrowserScriptInvoker scriptInvoker = new XSafeBrowserScriptInvoker(); switch (command) { case "get": { if (currentStorage.Keys.Contains(param)) { string value = currentStorage[param]; string jsString = "window." + type + ".onResult('" + param + "','" + value + "');"; scriptInvoker.Exec(Browser, "execScript", new string[] { jsString }); } else { string jsString = "window." + type + ".onResult('" + param + "');"; scriptInvoker.Exec(Browser, "execScript", new string[] { jsString }); } } break; case "load": { string[] keys = currentStorage.Keys.ToArray(); string jsonString = JsonHelper.Serialize(keys); string callbackJS = "window." + type + ".onKeysChanged('" + jsonString + "');"; scriptInvoker.Exec(Browser, "execScript", new string[] { callbackJS }); } break; case "set": { // value should exist if (split.Length > 4) { currentStorage[param] = split[4];//value UserSettings.Save(); string[] keys = currentStorage.Keys.ToArray(); string jsonString = JsonHelper.Serialize(keys); string callbackJS = "window." + type + ".onKeysChanged('" + jsonString + "');"; scriptInvoker.Exec(Browser, "execScript", new string[] { callbackJS }); } } break; case "remove": currentStorage.Remove(param); UserSettings.Save(); break; case "clear": currentStorage = new Dictionary<string, string>(); UserSettings[type] = currentStorage; UserSettings.Save(); break; } } return true; }
public XJavaScriptEvaluator(XWebApplication app) { this.app = app; this.jsInvoker = new XSafeBrowserScriptInvoker(); }
public void InjectScript() { string script = @"(function(win, doc) { { var DOMStorage = function(type) { if (type == 'sessionStorageHelper') { this._type = type; } Object.defineProperty(this, 'length', { configurable: true, get: function() { return this.getLength(); } }); }; DOMStorage.prototype = { _type: 'localStorageHelper', _result: null, keys: null, onResult: function(key, valueStr) { if (!this.keys) { this.keys = []; } this._result = valueStr; }, onKeysChanged: function(jsonKeys) { this.keys = JSON.parse(jsonKeys); var key; for (var n = 0, len = this.keys.length; n < len; n++) { key = this.keys[n]; if (!this.hasOwnProperty(key)) { Object.defineProperty(this, key, { configurable: true, get: function() { return this.getItem(key); }, set: function(val) { return this.setItem(key, val); } }); } } }, initialize: function() { window.external.Notify('DOMStorage/' + this._type + '/load/keys'); }, getLength: function() { if (!this.keys) { this.initialize(); } return this.keys.length; }, key: function(n) { if (!this.keys) { this.initialize(); } if (n >= this.keys.length) { return null; } else { return this.keys[n]; } }, getItem: function(key) { if (!this.keys) { this.initialize(); } var retVal = null; window.external.Notify('DOMStorage/' + this._type + '/get/' + key); if (this._result) { retVal = window.unescape(decodeURIComponent(this._result)); this._result = null; } return retVal; }, setItem: function(key, value) { if (!this.keys) { this.initialize(); } window.external.Notify('DOMStorage/' + this._type + '/set/' + key + '/' + encodeURIComponent(window.escape(value))); }, removeItem: function(key) { if (!this.keys) { this.initialize(); } var index = this.keys.indexOf(key); if (index > -1) { this.keys.splice(index, 1); window.external.Notify('DOMStorage/' + this._type + '/remove/' + key); delete this[key]; } }, clear: function() { if (!this.keys) { this.initialize(); } for (var n = 0, len = this.keys.length; n < len; n++) { delete this[this.keys[n]]; } this.keys = []; window.external.Notify('DOMStorage/' + this._type + '/clear/'); } }; { Object.defineProperty(window, 'localStorageHelper', { writable: false, configurable: false, value: new DOMStorage('localStorageHelper') }); } { Object.defineProperty(window, 'sessionStorageHelper', { writable: false, configurable: false, value: new DOMStorage('sessionStorageHelper') }); } } })(window, document);"; XSafeBrowserScriptInvoker scriptInvoker = new XSafeBrowserScriptInvoker(); scriptInvoker.Exec(Browser, "execScript", new string[] { script }); }
/// <summary> /// app 页面加载完成 /// </summary> /// <param name="browser"></param> private void OnPageLoadCompleted(WebBrowser browser) { string param = (String) GetData(XConstant.TAG_APP_START_PARAMS); if (param != null) { RemoveData(XConstant.TAG_APP_START_PARAMS); } //由于app 的localstorage以appId目录存储,需要在onDeviceready fire前初始化privateModule //设置privateModule 的初值 string appId = AppInfo.AppId; string workspace = GetWorkSpace(); workspace = workspace.Replace('\\', '/'); workspace = "/" + workspace; var scriptInvoker = new XSafeBrowserScriptInvoker(); string appIdResult = "(function() { try { cordova.require('xFace/privateModule').initPrivateData([\"" + appId + "\",\"" + workspace + "\",\"" + param + "\"]);}catch(e){console.log('exception in initPrivateData:' + e);}})();"; if (!scriptInvoker.Exec(browser, "execScript", new string[] { appIdResult })) { XLog.WriteError("calling js to initPrivateData. Did you include cordova.js in your html script tag?"); return; } }