Serve HTML and other content from a remote machine to a script
Anyone familiar with affiliate marketing can appreciate the simplicity of dropping a pre-configured
widget into their website and having a plethora of resulting content available on their site. Companies
such as WidgetBucks and DataFeedFiles are just two of many providing clients with a simple solution
to what would otherwise require complex coding practices. Grab some script and drop it in your website and
you get a complete online store with millions of products. The advantage to the end user is the simplicity
(they don't have to be coders), the advantage to the company is complete control over the content and a
means to monopolize on their services to a massive audience.
A few weeks ago I thought I had a brilliant idea that could make use of this kind of technology. I thought
"If only I could provide this level of functionality in a simple one-line script, I could make completely
cash in!" So, I did some research and did some tests until FINALLY I was able to mimic the functionality
of services providing content via one-line scripts. Unfortunately, I didn't think of one important thing...
search engines see the SCRIPT file, not the content! So, you get absolutely zero (none, zip, zilch) SEO
benefit from the scripts. That killed my idea, but I'm sure somewhere down the road I'll resurrect it and
take it from a different angle. In any case, here is the code to serve some content to a remote website
that requests it via a single line of JavaScript.
Source page setup
The first thing to do is to create a source page that will be called from a remote script. The source page
should be nothing more than an empty .ASPX page. The code-behind will be responsible for generating and
pushing the content to the requestor.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Source.aspx.cs" Inherits="Source" %>
Namespaces required
Since I personally hate when code examples do not include the appropriate namespaces for accessing the
libraries, I've decided to make it a point to add a section for namespaces. Here they are for this project.
using System;
using System.Web.UI;
using System.Text;
using System.IO;
using System.Net;
The heart of the application
/// FUNCTION Enquote Public Domain 2002 JSON.org
/// @author JSON.org
/// @version 0.1
/// Ported to C# by Are Bjolseth, teleplan.no
public static string Enquote(string s)
{
if (s == null || s.Length == 0)
{
return "\"\"";
}
char c;
int i;
int len = s.Length;
StringBuilder sb = new StringBuilder(len + 4);
string t;
sb.Append('"');
for (i = 0; i < len; i += 1)
{
c = s[i];
if ((c == '\\') || (c == '"') || (c == '>'))
{
sb.Append('\\');
sb.Append(c);
}
else if (c == '\b')
sb.Append("\\b");
else if (c == '\t')
sb.Append("\\t");
else if (c == '\n')
sb.Append("\\n");
else if (c == '\f')
sb.Append("\\f");
else if (c == '\r')
sb.Append("\\r");
else
{
if (c < ' ')
{
//t = "000" + Integer.toHexString(c);
string tmp = new string(c, 1);
t = "000" + int.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
sb.Append("\\u" + t.Substring(t.Length - 4));
}
else
{
sb.Append(c);
}
}
}
sb.Append('"');
return sb.ToString();
}
Providing the content to render to the client
/// Generate and push content from local server to requesting page
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello World!");
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
base.Render(hw);
Response.Clear();
Response.ContentType = "text/javascript";
string html = string.Format("window.document.write({0});", Enquote(sb.ToString()));
writer.Write(html);
}
Calling the code from the client
Now all we need to do is make a simple JavaScript call and we get instant content displayed on a remote website
that we have 100% control of! You can even have clients pass in special params to customize the data being
returned.
<script type="text/javascript" src="http://www.DropInCode.com/Source.aspx?PartnerID=1002&template=1223"></script>
You can copy-and-paste the JavaScript line from above into your website to see a working
example. For kicks, right-click and "view source" on the page after you run it. You won't see the
"Hello World!" content... you'll see only the JavaScript call, hence the reason it gives you zero
SEO benefit.
With some level of ingenuity and forward-thinking, I'm certain someone can take this code and make a gadget
or widget that can make them so good money. If you do, make sure to let me know about it. ;)
More information
Drop-In Code