public static async Task <BrowserInformation> GetBrowserInformation() { if (browserInfoFunction == null) { browserInfoFunction = await WebSharp.CreateJavaScriptFunction( @"return function (data, callback) { try { let browserInfo = {}; browserInfo['appName'] = navigator.appName; //browserInfo['appVersion'] = navigator.appVersion; browserInfo['appVersion'] = process.versions.chrome; browserInfo['userAgent'] = navigator.userAgent; browserInfo['platform'] = navigator.platform; browserInfo['cookieEnabled'] = navigator.cookieEnabled; browserInfo['appCodeName'] = navigator.appCodeName; callback(null, browserInfo); } catch (e) { callback(e, null); } }"); } var eval = await browserInfoFunction(null); if (eval != null) { var bi = new BrowserInformation(); ScriptObjectHelper.DictionaryToScriptableType((IDictionary <string, object>)eval, bi); return(bi); } return(default(BrowserInformation)); }
protected async Task <Func <object, Task <object> > > CreateScriptObject(string javascriptObject, params object[] args) { object[] parms = args; if (args != null) { parms = ScriptObjectUtilities.WrapScriptParms(args); } Func <object, Task <object> > scriptProxy = await WebSharp.CreateJavaScriptFunction(createScript.Replace("$$$$javascriptObject$$$$", javascriptObject)); await CreateScriptObject(scriptProxy, args); return(scriptProxy); }
private async Task Initialize(string title, NotificationOptions options) { Func <object, Task <object> > notification = await WebSharp.CreateJavaScriptFunction(@" return function (options, callback) { let notification = new Notification(options.title, options); let proxy = {}; proxy.get_title = function (data, cb) { cb(null, notification.title); }; proxy.get_dir = function (data, cb) { cb(null, notification.dir); }; proxy.get_lang = function (data, cb) { cb(null, notification.lang); }; proxy.get_body = function (data, cb) { cb(null, notification.body); }; proxy.get_tag = function (data, cb) { cb(null, notification.tag); }; proxy.get_image = function (data, cb) { cb(null, notification.image); }; proxy.get_icon = function (data, cb) { cb(null, notification.icon); }; proxy.get_badge = function (data, cb) { cb(null, notification.badge); }; proxy.get_sound = function (data, cb) { cb(null, notification.sound); }; proxy.get_timestamp = function (data, cb) { cb(null, notification.timestamp); }; proxy.get_renotify = function (data, cb) { cb(null, notification.renotify); }; proxy.get_silent = function (data, cb) { cb(null, notification.silent); }; proxy.get_requireInteraction = function (data, cb) { cb(null, notification.requireInteraction); }; proxy.get_data = function (data, cb) { cb(null, notification.data); }; // [SameObject] readonly attribute FrozenArray<unsigned long> vibrate; // [SameObject] readonly attribute FrozenArray<NotificationAction> actions; if (options.onclick) { if (options.onclick.formatter) { notification.onclick = function (evt) { let event = {}; event.Event = {}; event.SrcElement = proxy; event.Handler = options.onclick.handler; // very simple serialization of event for ( name in evt ) { event.Event[name] = evt[ name ]; }; options.onclick.formatter(event, null); } } else notification.onclick = options.onclick.handler; } if (options.onshow) { if (options.onshow.formatter) { notification.onshow = function (evt) { let event = {}; event.Event = {}; event.SrcElement = proxy; event.Handler = options.onshow.handler; // very simple serialization of event for ( name in evt ) { event.Event[name] = evt[ name ]; }; options.onshow.formatter(event, null); } } else notification.onshow = options.onshow.handler(null, null); } if (options.onerror) { if (options.onerror.formatter) { notification.onerror = function (evt) { let event = {}; event.Event = {}; event.SrcElement = proxy; event.Handler = options.onerror.handler; // very simple serialization of event for ( name in evt ) { event.Event[name] = evt[ name ]; }; options.onerror.formatter(event, null); } } else notification.onerror = options.onerror.handler; } //console.log(notification); callback(null, proxy); } "); if (!string.IsNullOrEmpty(title)) { options.Title = title; } _notificationProxy = await notification(options.AsDictionary()); }
public async Task <object> AddNotifications(dynamic input) { Func <object, Task <object> > consoleLog = await WebSharp.CreateJavaScriptFunction(@" return function (data, callback) { console.log(data); callback(null, null); } "); Func <object, Task <object> > addEventListener = await WebSharp.CreateJavaScriptFunction(@" return function (element, callback) { // Add an EventListener to the DOM element // We only need the evt.srcElement.Id document.getElementById(element.Id).addEventListener('click', function click (evt) { element.click(evt.srcElement.id); }); callback(null, null); } "); Func <object, Task <object> > eventFormatter = await WebSharp.CreateJavaScriptFunction(@" return function (event, callback) { //console.log(event); // We only pass in the SrcElement to be interrogated. event.Handler(event.SrcElement, null); callback(null, null); } "); try { // Create our Notification options for each button var options = new NotificationOptions[] { new NotificationOptions() { Title = "Basic Notification", Body = "Short message part", NotificationDirection = NotificationDirection.ltr, OnClick = new EventHandler( (Func <object, Task <object> >)(async(evt) => { var not = await Notification.FromObject((dynamic)evt); consoleLog($"Notification : {await not.GetTitle()} clicked."); //consoleLog(evt); //var dic = (Dictionary<string, object>)evt; // foreach(KeyValuePair<string, object> kv in (dynamic)evt) // consoleLog($"field {kv.Key} - {kv.Value}" ); return(null); }), eventFormatter // Custom formatter to marshal the needed information back to EventHandler Handler function. ), OnShow = new EventHandler( (Func <object, Task <object> >)(async(evt) => { consoleLog("On Show"); return(null); })) }, new NotificationOptions() { Title = "Content-Image Notification", Body = "Short message plus a custom content image", Image = (string)input.dirname + "/icon.png", OnClick = new EventHandler( (Func <object, Task <object> >)(async(evt) => { consoleLog($"message clicked"); return(null); }), eventFormatter ), OnShow = new EventHandler( (Func <object, Task <object> >)(async(evt) => { consoleLog("On Show"); return(null); })) } }; var doNotify = (Func <object, Task <object> >)(async(srcElementId) => { consoleLog($"C# callback: Clicked {srcElementId}"); if ((string)srcElementId == "basic") { var not = await Notification.Create(options[0].Title, options[0]); consoleLog($"Notification Title {await not.GetTitle()}"); } else { var not = await Notification.Create(options[1].Title, options[1]); consoleLog($"Notification Title {await not.GetTitle()}"); } return(null); }); var basicListener = new { Id = "basic", click = doNotify, }; var imageListener = new { Id = "image", click = doNotify, }; addEventListener(basicListener); addEventListener(imageListener); } catch (Exception exc) { consoleLog($"Exception: {exc.Message}"); } return(null); }