How to inject Javascript in the WP7 WebBrowser control?
Unfortunately WebBrowser.Document
is not available on WP7. But you can create and call a JavaScript function using InvokeScript
. Have a look over here where I describe how.
In short: you don't use .Document
and C# but create a piece of JavaScript instead. You then call eval
with this script as parameter to invoke it. Like this:
webBrowser1.InvokeScript("eval", " ...code goes here... ");
Can I inject JavaScript object into WebBrowser?
I just encountered this myself.
The problem is that you are not sending in the parameter as a string.
eval takes a string as parameter and if you invoke this script with your current parameter it will translate into:
eval(function Player() { this.play = function(e) { window.external.notify(e); }})
when what you really want to do is:
eval("function Player() { this.play = function(e) { window.external.notify(e); }}")
So the correct this, try:
webbrowser.InvokeScript("eval", "\"function Player() { this.play = function(e) { window.external.notify(e); }}\"");
Inject/serve custom CSS in WP8 WebBrowser Control
Yes. By invoking WebBrowser.InvokeScript after WebBrowser.LoadCompleted you can execute arbitrary javascript code in a webbrowser control. Web apps can then use WebBrowser.ScriptNotify to talk back to C#.
See more @ How to inject Javascript in the WP7 WebBrowser control?
How to inject CSS in WebBrowser control?
I didn't try this myself but since CSS style rules can be included in a document using the <style>
tag as in:
<html>
<head>
<style type="text/css">
h1 {color:red}
p {color:blue}
</style>
</head>
you could try giving:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement styleEl = webBrowser1.Document.CreateElement("style");
IHTMLStyleElement element = (IHTMLStyleElement)styleEl.DomElement;
IHTMLStyleSheetElement styleSheet = element.styleSheet;
styleSheet.cssText = @"h1 { color: red }";
head.AppendChild(styleEl);
a go. You can find more info on the IHTMLStyleElement here.
Edit
It seems the answer is much much simpler than I originally thought:
using mshtml;
IHTMLDocument2 doc = (webBrowser1.Document.DomDocument) as IHTMLDocument2;
// The first parameter is the url, the second is the index of the added style sheet.
IHTMLStyleSheet ss = doc.createStyleSheet("", 0);
// Now that you have the style sheet you have a few options:
// 1. You can just set the content as text.
ss.cssText = @"h1 { color: blue; }";
// 2. You can add/remove style rules.
int index = ss.addRule("h1", "color: red;");
ss.removeRule(index);
// You can even walk over the rules using "ss.rules" and modify them.
I wrote a small test project to verify that this works. I arrived at this final result by doing a search on MSDN for IHTMLStyleSheet, upon which I happened across this page, this page and this one.
How does CefSharp inject JS into new pages opened in Windows.open
The window.open
call will create a new popup. You can get load events for popups (and main browser) by implementing LoadHandler.
A basic example would look something like:
public class CustomLoadHandler : CefSharp.Handler.LoadHandler
{
protected override void OnFrameLoadStart(IWebBrowser chromiumWebBrowser, FrameLoadStartEventArgs args)
{
// Execute script before DOM has started loading
// window.open will create a new popup, check IsPopup = true
// Add any additional checks like comparing the URL that meet your requirements
// Execute your script here on in OnFrameLoadEnd. Not both
if (args.Browser.IsPopup && args.Frame.Url.EndsWith("page2.html"))
{
string script = System.IO.File.ReadAllText(@"c:\my.js");
args.Frame.ExecuteJavaScriptAsync(script);
}
base.OnFrameLoadStart(chromiumWebBrowser, args);
}
protected override void OnFrameLoadEnd(IWebBrowser chromiumWebBrowser, FrameLoadEndEventArgs args)
{
// Execute script before DOM has started loading
// window.open will create a new popup, check IsPopup = true
// Add any additional checks like comparing the URL that meet your requirements
// Execute your script here on in OnFrameLoadStart. Not both
if (args.Browser.IsPopup && args.Frame.Url.EndsWith("page2.html"))
{
string script = System.IO.File.ReadAllText(@"c:\my.js");
args.Frame.ExecuteJavaScriptAsync(script);
}
base.OnFrameLoadEnd(chromiumWebBrowser, args);
}
}
chromiumWebBrowser.LoadHandler = new CustomLoadHandler();
Related Topics
Why Does "Dtoa.C" Contain So Much Code
Wpf Webbrowser (3.5 Sp1) Always on Top - Other Suggestion to Display HTML in Wpf
C# Linq Where Clause as a Variable
How to Add Attributes for C# Xml Serialization
Using C# 6 Features with Codedomprovider (Roslyn)
Android Emulator Not Connecting to Localhost API
Print HTML Document from Windows Service in C# Without Print Dialog
Java Equivalent of C# Async/Await
ASP.NET 5 Add Wcf Service Reference
.Net Class to Execute Remotely on Linux Over Ssh
ASP.NET Core Disable Authentication in Development Environment
Swift: Convert Nsdate to C# Ticks
With C# Use Chrome to Covert HTML to Pdf
Deserialize JSON Object Sent from Android App to Wcf Webservice