Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
// ---------------------------------------------
public void Init (HttpApplication app)
{
app.ReleaseRequestState += new EventHandler(InstallResponseFilter);
}
// ---------------------------------------------
private void InstallResponseFilter(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
if(response.ContentType == "text/html")
response.Filter = new PageFilter (response.Filter);
}
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
namespace AspNetResources.Web
{
/// <summary>
/// PageFilter does all the dirty work of tinkering the
/// outgoing HTML stream. This is a good place to
/// enforce some compilancy with web standards.
/// </summary>
public class PageFilter : Stream
{
Stream responseStream;
long position;
StringBuilder responseHtml;
public PageFilter (Stream inputStream)
{
responseStream = inputStream;
responseHtml = new StringBuilder ();
}
#region Filter overrides
public override bool CanRead
{
get { return true;}
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Close()
{
responseStream.Close ();
}
public override void Flush()
{
responseStream.Flush ();
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get { return position; }
set { position = value; }
}
public override long Seek(long offset, SeekOrigin origin)
{
return responseStream.Seek (offset, origin);
}
public override void SetLength(long length)
{
responseStream.SetLength (length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return responseStream.Read (buffer, offset, count);
}
#endregion
#region Dirty work
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = System.Text.UTF8Encoding.UTF8.«
GetString (buffer, offset, count);
// ---------------------------------
// Wait for the closing </html> tag
// ---------------------------------
Regex eof = new Regex ("</html>", RegexOptions.IgnoreCase);
if (!eof.IsMatch (strBuffer))
{
responseHtml.Append (strBuffer);
}
else
{
responseHtml.Append (strBuffer);
string finalHtml = responseHtml.ToString ();
[b]
//here's where you'd manipulate the response.
finalHtml = finalHtml.Replace( "blah", "whatever" ); [/b]
byte[] data = System.Text.UTF8Encoding.UTF8.«
GetBytes (finalHtml);
responseStream.Write (data, 0, data.Length);
}
}
#endregion