How WebBrowser control in .net handles ObjectForScripting - javascript

As far as I know we can call C# function from Javascript, that is loaded inside a WebBrowser control, following code shows how I usually do it.
Form1.cs
public partial class Form1 : Form{
private WebBrowser webBrowser1;
public ApplicationWindow(){
InitializeComponent();
WebBrowser webBrowser1 = new WebBrowser();
//some code follows
webBrowser1.ObjectForScripting = new ScriptManager();
this.webBrowser1.Url = new Uri("file:///d:/ui/application.html");
}
}
}
ScriptManager.cs
namespace WindowsFormsApplication10 {
[ComVisible(true)]
public class ScriptManager{
public string GetAllDomains(){
string result=null;
//does something;
return result;
}
}
}
application.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var result = window.external.GetAllDomains();
//it works but this is what puzzles me.
});
</script>
The questions that intrigues me are
why we need ComVisible to be true for class whose object we are going to use as objectForScripting?
How Javascript object window.external has the same methods as in objectForScripting?
How they handle cross language type conversion?

I wonder why no one answered for so long. The answer to all your questions is COM - Component Object Model.
Windows is providing ability (using COM) for classes and functions from one program (exe) to be accessible outside the exe.
So
1) why we need ComVisible to be true for class whose object we are going to use as objectForScripting?
-> This tells windows to make the class and its methods visible to the webbrowser.
2) How Javascript object window.external has the same methods as in objectForScripting?
-> The javascript is calling methods of the class made visible in above answer.
3) How they handle cross language type conversion?
-> COM handles the types internally so methods in one programming language can be called from another programming language.

Related

Send Object from Javascript to Kotlin using Webview

I have loaded a webpage using WebView component and added a JavascriptInterface. Please check the code below,
val webview = WebView(this)
setContentView(webview)
webview.settings.javaScriptEnabled = true
webview.loadUrl(HOME_PAGE_URL)
webview.addJavascriptInterface(JavascriptInterface(),”javascript_bridge”)
And when I call the invoke from Javascript using window.javascript_bridge.showToast(“Information Saved”);
private inner class JavascriptInterface
{
#android.webkit.JavascriptInterface
fun showToast(text: String?)
{
Log.d("WEBVIEW", text);
}
}
I am able to call the method from Javascript to Kotlin without any trouble.
But now I want to pass an Object from Javascript to Kotlin like below,
var info = {
message: “Information Saved”,
ID: 123456
}
And when I call the invoke from Javascript using window.javascript_bridge.showToast(info);
I tried to change to the data type to Any, but the value passed from Javascript is null
private inner class JavascriptInterface
{
#android.webkit.JavascriptInterface
fun showToast(text: Any?)
{
Log.d("WEBVIEW", text.toString());
}
}
As far as i know, the javaScript interface methods only accepts primitive types of data as parameters (as discussed on this question).
If you still want to achieve that, you may serialize the object (to JSON format, for instance) in the javaScript and then Deserialize it in Java.
Hope it helps :)

GWT and JSInterop namespace

I've got some native JS test code here (mydialog.js)
var MyDialog = {
foo : function()
{
console.log("foo");
}
};
I'm injecting using the following code using GWT:
ScriptInjector.fromUrl("mydialog.js").setCallback(new Callback<Void, Exception>()
{
#Override
public void onFailure(Exception reason)
{
Window.alert("Dialog Injection Failed!" + reason);
}
#Override
public void onSuccess(Void result) {}
}).inject();
}
Then, I'm trying to set up a JSInterop class here:
#JsType(isNative=true, namespace=JsPackage.GLOBAL, name="MyDialog")
public class MyDialog
{
public static native void foo();
}
The problem, is that the namesoace JsPackage.GLOBAL isn't accurate. The injected code doesn't live under the global namespace, but rather the one generated by GWT and presumably inside the GWT iframe I believe. What is the namespace I need?
In other words, what should this be:
...namespace=???...
JsInterop assumes that the code it is reasoning about lives in the main window - this isn't the difference of a namespace, but a different global context that it runs under (with different Object, Array types, etc). In Java terms you might consider this not just "wrong package", but "wrong classloader", though in a way that you can't correct very nicely.
Instead, direct the ScriptInjector to put your created JS into the main page, and you can interact with it directly with setWindow:
ScriptInjector.fromUrl(...)
.setWindow(ScriptInjector.TOP_WINDOW)
.setCallback(...);
Alternatively, you can use the "magic" string of "<window>", which will mean "don't provide any namespace at all". This isn't an official part of JsInterop (the constant isn't declared in the jsinterop annotations themselves), but at least presently is a way you can work around this.

How to create an C#.Net DLL to use in JavaScript

I have created a .Net DLL with few simple classes. I have registered the DLL using RegAsm and I got a message that types were registered successfully.
RegAsm Syntax Used :
C:\Windows\Microsoft.NET\Framework\v4.0.30319>RegAsm.exe "D:\Projects\TestDLL.Core.dll"
C# Code:
namespace MyTestDLL.Core
{
public class PacketInfo
{
// constructor
public PacketInfo()
{
}
public string HostName { get; set; }
// and so on ......
}
}
I have set the ComVisible property to true in AssemblyInfo.cs file of this DLL.
// [assembly: ComVisible(true)]
However when I create an object out of it in JavaScript and run the script in Command prompt , I'm getting either it is not an object or null.
JS Code :
var testObj = new ActiveXObject(MyTestDLL.Core.PacketInfo);
testObj.HostName = "Test";
Can anyone advise me how to resolve this ?
You need to add a ProgId attribute to the class, something like this:
[Guid("some guid that you will never change")]
[ProgId("MyLib.PacketInfo")] // this is arbitrary
public class PacketInfo
{
....
}
The guid is optional, but if you don't set it yourself, it will be something you won't control, so it's better to define it. And of course I did not add the ComVisible because you already defined it at assembly level (I personnally prefer to set it at class level).
Then, once registered, you shall be able to use it like this (use the progid as a string):
var testObj = new ActiveXObject('MyLib.PacketInfo');
testObj.HostName = "Test";
I was able to achieve that by adding the following line to My DLL just above the class,
[Guid("A32AB771-9967-4604-B193-57FAA25557D4"), ClassInterface(ClassInterfaceType.None)]
Earlier I wasn't having the ClassInterfaceType part in my code.
Also ensure that each of your classes are having unique GUID.
FYI : You can create GUID using Visual Studio Tools GUID Creator GUI.

Android, extract javascript variable from webview using javascript interface

How can I extract this variable bellow from a website to my android code?
I guess it should work using javascript interface but how do I get it?
<script type="text/javascript">
var Ids = "[4161, 104, 121, 202, 1462]";
</script>
And I can't change the code on the website to a method that returns the value.
Any suggestions?
You can use the javascript: scheme in a webview.loadurl call. It will execute the javascript in the webview page.
From there you can make it call a function in your javascript interface.
webview.loadUrl("javascript:Android.getIds(Ids);");
Android being the name space used to declare your javascript interface.
//Add the javascript interface to your web view
this.addJavascriptInterface(new CustomJavaScriptInterface(webViewContext), "Android");
Beware that javascriptinterface only work with primitive types. So you actually can't pass directly an array. Just use the javascript scheme to loop through your array. I see it is not really an array so you should be fine with just :
public class CustomJavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
CustomJavaScriptInterface(Context c) {
mContext = c;
}
/** retrieve the ids */
public void getIds(final String myIds) {
//Do somethings with the Ids
}
}

Using Javascript to Call C++ in Internet Explorer

I have seen this for BHO extensions, where the JavaScript can call functions in the C++ BHO. But lets say I am not using a BHO, instead I have a C++ console application that creates an IE COM object like so:
HRESULT hr = CoCreateInstance(
CLSID_InternetExplorer,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2,
(void**)&_cBrowser);
I also have a class which "owns" the IWebBrowser2 object that comes back from this function.
class BrowserWrapper{
public:
CComPtr<IWebBrowser2> pBrowser;
void SomeFunction(...)
}
Is there a way to call a function like "SomeFunction" in the wrapper class from the JavaScript in the spawned IWebBrowser2 object?
You must implement the IDocHostUIHandler interface and set it to the web browser with a code similar to this (extracted from the doc):
ComPtr<IDispatch> spDocument;
hr = spWebBrowser2->get_Document(&spDocument);
if (SUCCEEDED(hr) && (spDocument != nullptr))
{
// Request default handler from MSHTML client site
ComPtr<IOleObject> spOleObject;
if (SUCCEEDED(spDocument.As(&spOleObject)))
{
ComPtr<IOleClientSite> spClientSite;
hr = spOleObject->GetClientSite(&spClientSite);
if (SUCCEEDED(hr) && spClientSite)
{
// Save pointer for delegation to default
m_spDefaultDocHostUIHandler = spClientSite;
}
}
// Set the new custom IDocHostUIHandler
ComPtr<ICustomDoc> spCustomDoc;
if (SUCCEEDED(spDocument.As(&spCustomDoc)))
{
// NOTE: spHandler is user-defined class
spCustomDoc->SetUIHandler(spHandler.Get());
}
}
You must specifically implement the GetExternal method
Now, in IE's javascript (or vbscript for that matter), you can access your host with a call like this:
var ext = window.external; // this will call your host's IDocHostUIHandler.GetExternal method
ext.SomeFunction(...); // implemented by your object
What you return in GetExternal must be an IDispatch object that you can design the way you want.
You need to implement the IDocHostUIHandler interface. This has a method called GetExternal - which you need to return an object that implements IDispatch.
In the javascript, you can call window.external.something() - which will cause the browser to query for your external implementation - the IDispatch object - and it will then use the IDispatch to execute something.

Categories

Resources