How to call a Javascript function (and register a callback function to be executed after the method execution from web view is done) inside an webview on nativescript?
First the bad new, nothing is "built" in to do this. This requires you to add some code to communicate back from the webview. In the nativescript-webworkers plugins that I wrote; the way I did it was I used the onJsConfirm function on Android. On iOS I used the userContentControllerDidReceiveScriptMessage callback to communicate back. If you want to develop your own code; just grab a copy of my webworkers plugin and you can see the way to do it.
Now to the good news, is that it appears there is already a plugin called "nativescript-webview-interface" which gives you this facility so you don't have to build all that binding code yourself. It looks like all you need to do is include it and you will have bi-directional communications with your webview on both iOS and Android.
Note; to see if a plugin exists in the NativeScript community; your best bet is to look on http://plugins.nativescript.rocks, that is how I found out about the above listed plugin.
Related
I have read the two other questions on SO regarding this and I wanted to know if there is a good solution for that now / best practice.
Long story short, we use an SDK which is written natively and we've wrapped it so that it works on Xamarin.Android and Xamarin.iOS. It has asynchronous callback methods. I need to call a method in the shared code when a callback is received in the Android project for instance.
There's a lot of info for doing the opposite - using DependencyService. How about in my scenario? Does anyone have experience with an app like this and what's the best approach to keep code clean and do this using MVVM?
The options I know are:
Using a static App instance - this is what we currently do.
MessagingCenter
Anything else?
Actually I've never seen anyone recommend usage of MessagingCenter for anything else than communication between ViewModels so I am not sure it is recommended here. Also, I need to know the sender object type so I need a reference to the class in the platform specific project.
I would recommend you to use messagingCenter to pass data or call method between shared project and platform project. You can just send a new object instead of the class in the platform specific project.
Also, have a look at using eventhandler as I mentioned in this answer may help someone who want to call from the shared project into the platform specific one.
BTW, I mean you can even pass an object as TSender if it is not necessary to use:
MessagingCenter.Send<Object>(new object(), "Hi");
MessagingCenter.Subscribe<Object>(new object(), "Hi", (sender) =>
{
// Do something whenever the "Hi" message is received
});
We are trying to use Cobalt (20.stable) browser as the browser of our web SPA application.
My requirement is to be able to change URL at runtime, what I was able to find in the code:
Is:
starboard::shared::starboard::Application::Link(const char* link_data)
which ends up sending:
kSbEventTypeLink
Unfortunately this is not working, as code is ignoring the call; the handling reaches the point:
// TODO: Remove this when terminal application states are properly handled.
if (deep_link_event->IsH5vccLink()) {
browser_module_->Navigate(GURL(deep_link_event->link()));
}
In my case I m trying to change the URL to let say https://www.example.com.
There should be a way to do that as when navigating we can always have a link that will cause the browser to go to some URL?
Porting layer is not supposed to control navigation directly. Instead, your Starboard implementation may send a deep link event which could be intercepted by a web app which will perform a navigation. See h5vcc_runtime.idl for Web API.
That said, if you are building an SPA, why do you even need to change a URL? Initial URL of a web app is controlled by --url command line switch.
When you say runtime are you looking to change the initial URL when the app is first launched? If so, you could just use the --url parameter.
So you could do the following:
cobalt --url="https://www.example.com"
I did a patch to allow changing the URL.
I just need to call starboard::shared::starboard::Application::Link("https://www.example.com").
Inside this call a DeepLinkEvent is posted.
Patch : https://gofile.io/?c=9GvNHX
Cobalt does not navigate for you. The JavaScript receives the deeplink with the function it sets on h5vcc.runtime.onDeepLink and then does whatever it wants with that. As a SPA, it will parse the URL and load new content from its server in its own internal data format (e.g. protocol buffers, JSON, etc.) which it uses to update its own DOM to show new content.
Navigating is not the point of a SPA since that makes it not be a single page application. However, there may be cases such as a loader app that will want to make some initial decisions then load the actual SPA. That loader app would have to have the appropriate CSP rules in place, then set window.location to the URL of the page to navigate to.
Note: The code you found in Application::OnDeepLinkEvent() is a remnant that previously supported the H5vccURLHandler, which was removed in Cobalt 20. It's not meant to navigate to arbitrary deeplinks.
I have encountered a strange problem when using angular 2 beta RC.
Events get postponed if I include an external script I wrote into any angular 2 project:
<script>
(function(r,a,k,e,y,o,u){r['RakrWidgetObject']=y;r[y]=r[y]||function(){
(r[y].q=r[y].q||[]).push(arguments)},r[y].l=1*new Date();o=a.createElement(k),
u=a.getElementsByTagName(k)[0];o.async=1;o.src=e;u.parentNode.insertBefore(o,u)
})(window,document,'script','//cht.technology/rakr.js','rakr');
rakr('//localhost:3000', 'RAKR-000001');
</script>
Take github project thelgevold/angular-2-samples for example, once I add the below script as I did in https://github.com/tan9/angular-2-samples/tree/event-postponed branch.
The angular 2 application starts to behave strangely, some event changes won't be taking into account by angular until I trigger another event manually, I have to click a button twice to get correct rendering as this recording I uploaded to imgur
I don't know what's happening to the external script I wrote, it's a simple project that only depends on html2canvas and es6-promise, which can capture screenshot using html2canvas and send it to another web service. The bundle was built by webpack, and I tried to build the bundle by using browersify, with no luck.
This is a caveat with how Zone.js works. Zone.js patches async browser events and provides an API in which Angular uses to determine when changes happen and when to run change detection in order to update the UI.
In the case of your third-party library that uses the browser Promise API, it needs to be loaded before Zone.js is loaded via script tag. This is so that async events are patched so it will be run in the "zone" that Angular runs in. Events running outside the zone won't be picked up unless change detection is run manually, or the event is run in the context of the Angular zone.
As explained by brandon, make the code run inside Angulars zone
inject NgZone
constructor(private ngZone:NgZone){}
...
this.zone.run(() => ... /* code here that modifies Angulars model from the outside */);
You can also get the zone outside Angular
bootstrap(AppComponent, ...).then((ref => ref.instance.injector.get(NgZone));
(Not sure if this is 100% correct, I'm just on my phone and looking up is cumbersome. Please post a comment if you can't make it work.
I try to run js pre/post processor to modify webpage after finish share extension, but finalize method failed to run.
In Apple document it said that both Share extensions and Action extensions can benefit from this js processor, but my finalize method not get called. Only run method get called. Anyone know how to make this work ?
Accessing a Webpage
In Share extensions (on both platforms) and Action extensions (iOS
only), you can give users access to web content by asking Safari to
run a JavaScript file and return the results to the extension ...
https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html
Here is sample project: https://app.box.com/s/9ild73l9gbmfazrmjerk
In your code you have a typo in function type, should be "finalize" instead of "finalizea".
I am trying to make a NPAPI plugin for chrome on Mac. I have written a basic npapi plugin and a basic manifest.json and background.html to load it. My background.html has a embed tag to fetch the plugin by Mimetype.
Now when I load my unpackaged extension from Chrome and try to debug the c++ code (in xcode4), I found that the functions are getting called in following order:
NP_Initialize
NP_GetEntryPoints
NPP_New
NPP_Destroy
After this when I click on the extension icon, the popup.html should get executed. My popup.html has these lines:
line 1:
var pluginObj = document.getElementById("pluginId");
line 2:
pluginObj.Myfunction();
But on line 1, NP_Getvalue() function doesnt get called and so a "scriptable NPObject" is not instantiated. On line 2, the Chrome JavaScript console says:
Error in event handler for 'undefined': Object #<HTMLEmbedElement> has no method 'Myfunction' TypeError: Object #<HTMLEmbedElement> has no method 'Myfunction'
Why does the NPP_Destroy functions gets called immediately after the NPP_New function?
Have you done drawing and event model negotiation in your plugin? Starting with Chrome 22 for Mac, the long-deprecated QuickDraw and Carbon models are no longer supported, and if your plugin doesn't negotiate modern models, it will be destroyed just after init. See here for example code that does this.
(Yes, it's unfortunate that the default models for 32-bit plugins are the old, deprecated models, but there's no way to change that in the spec due to all the existing plugins that expect the old behavior.)