简体   繁体   中英

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.

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

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:

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.

You can not do this with JQuery due to cross site scripting restrictions.

I suggest you write a PHP script that downloads the URL specified by the user and includes your widget code and then echo it back to the user.

I recommend using bookmarklets. I've made a bookmarklet generator for adding jQuery-enabled bookmarklets to a page to make development easier.

There's a caliper bookmarklet on the page that you can mess around with just to show an example of it working.

Full disclosure, this is something I've made, I'm not trying to be spammy as I think it's relevant: zbooks

你可以创建一个iframe页面,它在iframe中加载他们的页面,并使用javascript将你的代码注入iframe。

Here is my approach...

http://jsfiddle.net/L2kEf/

html

<iframe src="http://www.bing.com"></iframe>
<div>I am div</div>

css

div { background: red; position: absolute; top: 20px; width: 100px; left:20px;}
iframe{width: 100%; height: 500px;}

you can add javascript/jquery too, so you could do something like,

jQuery //not 100% sure it would work coz of cross browser thingy, but you know, worth a try.

$('div').click(function (){
    $('iframe').contents().html('changed');///
});

if this can't change any of the contents, you can display a dialog, to say it would normally work if it was in your website, then use @syserr0r approach for bookmarked users, for better results, since you are offering this kinda services, to developers, im sure they would know about bookmarking, my approach would be rarely used :) so hope it helps.

I had a problem of a similiar nature, and the main obstacle is the cross-domain policy.

You have to ask the user to put your code in a <script src="..."> or create a proxy solution that would add your code for them.

I went for the proxy and here are my observations:

  • it's easy to create a basic proxy in php - there are some php proxies on sourceforge and Ben Alman has created a simple php proxy for AJAX. Based on those I was able to create a php proxy altering the content properly in one day.
  • after that I spent a lot of time making it work with more and more sites with issues. You can never create a perfect proxy.

As an alternative (sa long as you are non-commercial) you can use http://www.jmarshall.com/tools/cgiproxy/ and put the site in an iframe and then do whatever you want to do with the iframes document, as it's in your domain thanks to the proxy. You can access iframeDOMnode.contentWindow.document then, etc.

You can create a Crossrider extension which your users can download. Then simply add this to your App/Extension code:

appAPI.dom.addRemoteJS("http://yourdomain.com/file.js")

Your users can then download the extension (it works cross-browser for Internet Explorer, Chrome and Firefox) and it will load your JS code on every page load.

You can get an approximation of what it will look like using a iframe. Take a look at that link for an example.

http://jsfiddle.net/jzaun/5PjRy/

The issue with this appoch is that you can't move your DIV(s) when the page scrolls, they are in effect just floating over the iframe. There is no way around this as cross-domain scripting wont let you access the iframe's document to monitor scroll events.

The only other option you have for a better fitting example would be to load the page from the server side in whatever scripting language you are using and load that into the iframe (or into a div, etc.) and you can use javascript all you want as the page is coming from your domain.

For your example of what will your widget look like I imagine floating your DIV(s) over an iframe would give enough of an idea.

Please note the example you gave is using the server side method, not the iframe method.

I agree with the bookmarklet strategy.

I'm a fan of http://bookmarklets.heroku.com/ , which lets you generate bookmarklets easily, inject jQuery, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM