-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebBrowserHelper.cs
101 lines (95 loc) · 3.7 KB
/
WebBrowserHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Copyright (c) 2011 Microsoft Corporation. All rights reserved.
// Use of this sample source code is subject to the terms of the Microsoft license
// agreement under which you licensed this sample source code and is provided AS-IS.
// If you did not accept the terms of the license agreement, you are not authorized
// to use this sample source code. For the terms of the license, please see the
// license agreement between you and Microsoft.
//
//
using System;
using System.Text;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Tasks;
namespace WordPressStarterKit
{
//credit to: http://www.ben.geek.nz for this WebBrowserHelper class to open WebBrowser Control links into full IE browser.
public class WebBrowserHelper
{
public static string NotifyScript
{
get
{
return @"<script>
window.onload = function()
{
a = document.getElementsByTagName('a');
for(var i=0; i < a.length; i++){
var msg = a[i].href;
a[i].onclick = function() {notify(msg);};
}
}
function notify(msg)
{
window.external.Notify(msg);
event.returnValue=false;
return false;
}
</script>";
}
}
public static string WrapHtml(string htmlSubString, double viewportWidth)
{
var html = new StringBuilder();
html.Append("<html>");
html.Append(HtmlHeader(viewportWidth));
html.Append("<body>");
html.Append(htmlSubString);
html.Append("</body>");
html.Append("</html>");
return html.ToString();
}
public static string HtmlHeader(double viewportWidth)
{
var head = new StringBuilder();
head.Append("<head>");
head.Append("<meta name=\"viewport\" content=\"width=" + viewportWidth + ", initial-scale=1, maximum-scale=1\">");
head.Append("<style>");
head.Append(string.Format(
"body {{background-color:{0};color:{1};font-family:'Segoe WP';margin:0;padding:0 }}",
GetBrowserColor("WebBrowserBackgroundColor"),
GetBrowserColor("WebBrowserForegroundColor")));
head.Append(string.Format(
"a {{color:{0}}} a:link {{color:white}} a:visited {{color:white}} a:active{{color:white}}",
GetBrowserColor("PhoneAccentColor")));
head.Append("</style>");
head.Append(NotifyScript);
head.Append("</head>");
return head.ToString();
}
public static void OpenBrowser(string url)
{
WebBrowserTask webBrowserTask = new WebBrowserTask { Uri = new Uri(url) };
webBrowserTask.Show();
}
private static string GetBrowserColor(string sourceResource)
{
var item = Application.Current.Resources[sourceResource];
if (item.GetType() == typeof(Color))
{
var color = (Color)item;
return "#" + color.ToString().Substring(3, 6);
}
else if (item.GetType() == typeof(SolidColorBrush))
{
var brush = (SolidColorBrush)item;
return "#" + brush.Color.ToString().Substring(3, 6);
}
else
{
throw new InvalidCastException("invalid color or brush: " + sourceResource);
}
}
}
}