Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing a demo of my CSS on any website

I have developed a small component which can be put in to any website. Now, I want to develop a code that could demonstrate how would my component look like on any website.

So, the person would come to my page and put in his URL and then my code should embed my custom JS/CSS in to the downloaded HTML and display it. Something like this.

Here, like the feedback tab, I want to show my component any where on that page.

like image 978
Jayesh Avatar asked Oct 21 '11 12:10

Jayesh


3 Answers

In the example you linked, they are requesting the page specified in the url querystring parameter on the server, and then doing more or less the following steps:

  1. In the <head> tag they are adding a <base href="url" /> tag to the document. The base tag will make any relative links in the document treat the value in the href attribute as their root. This is how they are getting around broken css / images. (The base tag is supported by all browsers)
  2. At the end of the document (IE the </body> tag) they are injecting the javascript that runs their demos.
  3. They serve the modified HTML requested to the browser.

All of this is pretty straight forward in implementation. You could use regular expressions to match the <head> and </body> tags for steps 1 and 2 respectively. Depending on the server platform how you actually request the page will vary, but here are some links to get you started:

  • C# - HttpWebRequest object documentation
  • PHP - HttpRequest::send
like image 31
Nathan Anderson Avatar answered Nov 11 '22 05:11

Nathan Anderson


Try a bookmarklet.

Create a piece of javascript that adds your code into the page such as the following:

javascript:(function(){var%20script=document.createElement('script');script.src='http://www.example.org/js/example.js';document.getElementsByTagName('head')[0].appendChild(script);})()

Add it as the href of a link like so:

<a href="javascript:(function(){var%20script=document.createElement('script');script.src='http://www.example.org/js/example.js';document.getElementsByTagName('head')[0].appendChild(script);})()">Link Text Here</a>

Tell your users to drag the link to their bookmark toolbar and click on it on different websites to try your code out.

Some examples: http://www.reclaimprivacy.org/, http://www.readability.com/bookmarklets

like image 186
syserr0r Avatar answered Nov 11 '22 05:11

syserr0r


Nathan's answer is the closest to how we have done the demo feature at WebEngage. To make such a demo functional, you'll need to create a Javascript widget that can be embedded on third party sites. syserr0r's answer on creating a bookmarklet is the simplest approach to do so. Our's is a JAVA backend and we use HttpClient to fetch the responses. As Nathan suggested, we parse the response, sanitize it and add our widget Javascript to the response. The widget JS code takes it on from there to render the Feedback tab and load a demo short survey.

Disclosure: I am a co-founder and ceo at WebEngage.

like image 9
avlesh Avatar answered Nov 11 '22 06:11

avlesh