Skip to content

Instantly share code, notes, and snippets.

@Nilzor
Created May 5, 2012 14:08
Show Gist options
  • Select an option

  • Save Nilzor/2602731 to your computer and use it in GitHub Desktop.

Select an option

Save Nilzor/2602731 to your computer and use it in GitHub Desktop.
WebView InvokeScript-Notify-test
<Page
x:Class="GTWin8.TestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GTWin8"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundBrush}" RenderTransformOrigin="0.43299999833107,0.416000008583069">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="27*"/>
<ColumnDefinition Width="656*"/>
</Grid.ColumnDefinitions>
<CheckBox Content="Hello world" HorizontalAlignment="Left" Margin="9,151,0,0" VerticalAlignment="Top" Height="74" Width="227" Grid.Column="1" d:LayoutOverrides="Margin"/>
<Button x:Name="ClickButton" Content="Google.com" HorizontalAlignment="Left" Margin="9,31,0,0" VerticalAlignment="Top" Width="236" Height="39" Grid.Column="1" Click="GoogleButton_Click" d:LayoutOverrides="VerticalMargin"/>
<WebView x:Name="TheWebView" HorizontalAlignment="Left" Height="625" Margin="9,96,0,0" VerticalAlignment="Top" Width="1203" LoadCompleted="TheWebView_LoadCompleted" ScriptNotify="TheWebView_ScriptNotify" Grid.Column="1" d:LayoutOverrides="Margin" />
<Button x:Name="BashButton" Content="Bash.org" HorizontalAlignment="Left" Margin="263,31,0,0" VerticalAlignment="Top" Width="236" Height="39" Grid.Column="1" d:LayoutOverrides="VerticalMargin" Click="BashButton_Click"/>
<Button x:Name="GeneratedButton" Content="Generated content" HorizontalAlignment="Left" Margin="523,31,0,0" VerticalAlignment="Top" Width="236" Height="39" Grid.Column="1" d:LayoutOverrides="VerticalMargin" Click="GeneratedButton_Click"/>
<Button x:Name="NotifyButton" Content="Notify" Grid.Column="1" HorizontalAlignment="Left" Margin="1058,31,0,0" VerticalAlignment="Top" Width="154" Click="NotifyButton_Click" RenderTransformOrigin="2.66599988937378,0.358999997377396"/>
<Button x:Name="WithScriptTagButton" Content="With script tag" Grid.Column="1" HorizontalAlignment="Left" Margin="779,31,0,0" VerticalAlignment="Top" Width="192" Click="WithScriptTagButton_Click"/>
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GTBusiness;
using OAuth_November;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace GTWin8
{
public sealed partial class TestPage : Page
{
public TestPage()
{
this.InitializeComponent();
}
private void GoogleButton_Click(object sender, RoutedEventArgs e)
{
TheWebView.Navigate(new Uri("http://www.google.com"));
}
private void BashButton_Click(object sender, RoutedEventArgs e)
{
TheWebView.Navigate(new Uri("http://www.bash.org"));
}
private void GeneratedButton_Click(object sender, RoutedEventArgs e)
{
TheWebView.NavigateToString("<html><head></head><body>No script tag :(</body></html>");
}
private void WithScriptTagButton_Click(object sender, RoutedEventArgs e)
{
TheWebView.NavigateToString("<html><head><script></script></head><body>Yay! Script tags rule!</body></html>");
}
private void NotifyButton_Click(object sender, RoutedEventArgs e)
{
PerformNotifty();
}
private void PerformNotifty()
{
try
{
string javacode = "window.external.notify('Hello world');";
TheWebView.InvokeScript("eval", new string[] { javacode });
}
catch (Exception ex) { new MessageDialog(ex.Message, "Exception occured").ShowAsync(); }
}
private void TheWebView_LoadCompleted(object sender, NavigationEventArgs e)
{
PerformNotifty();
}
private void TheWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
new MessageDialog(e.Value, "Message received").ShowAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment