Giter Club home page Giter Club logo

node-iframe-replacement's Introduction

node-iframe-replacement

Shippable branch npm

Motivation

Imagine the scenario: you've built a lovely web app for a business, who later insists it goes live within an iframe of their corp website. You resist, but you're ultimatley forced to bastardize your app to make it work.

node-iframe-replacement is the compromise. A small node server that grabs the source code of an external website and injects your content - thus allowing you to host your app on a subdomain and without an iframe. Changes to the external site (navigation etc) are automatically picked up every 5 minutes and reflected in your app.

You can now argue that your web app can remain synced with the corperate website look/feel without an iframe.

Installation

$ npm install --save node-iframe-replacement

Usage

Example server

The example below will request the BBC News home page and replace the contents of div[data-entityid="container-top-stories#1"] with the contents of our fake-news view:

express logic excluded to improve readability, you can view the full example implementation here

var iframeReplacement = require('node-iframe-replacement');

// add iframe replacement to express as middleware (adds res.merge method)
app.use(iframeReplacement);

// create a regular express route
app.get('/', function(req, res){

    // respond to this request with our fake-news content embedded within the BBC News home page
    res.merge('fake-news', {
        // external url to fetch
       sourceUrl: 'http://www.bbc.co.uk/news',
       // css selector to inject our content into
       sourcePlaceholder: 'div[data-entityid="container-top-stories#1"]',
       // pass a function here to intercept the source html prior to merging
       transform: null
    });
});

If you pass a function as the transform property transform: function($, model) {} - the function will be called prior to rendering, allowing you to inject title tags, html attributes etc. The $ passed to your transform method is a JQuery like object using cheerio, removing the need to parse strings.

Example fake-news html

<div style="margin: 0 0 0 20px; overflow: auto;">
    <div style="width: 210px; float: left;">
        <a href="https://github.com/john-doherty/node-iframe-replacement" target="_blank">
            <h3 style="color: #222; font-size: 32px; font-weight: bold; margin: 0 0 10px 0;">iframe replacement Node.js module lanuched</h3>
        </a>
        <p style="padding: 0; font-size: 14px;line-height: 22px">
            Open source project not only has the potential to replace iframes, but also brings a whole new meaning to the concept of fake news.
        </p>
    </div>
    <img src="https://cdn.colorlib.com/wp/wp-content/uploads/sites/2/nodejs-frameworks.png" alt="Node js logo" style="width: 500px; float: right;"/>
</div>

Example input

alt text

Example output

alt text

Run the example

You can try the example for yourself using the following commands:

git clone https://github.com/john-doherty/node-iframe-replacement
cd node-iframe-replacement
npm install
npm start

Then visit http://localhost:8080 in your browser.

Auto base tag injection

To ensure the source scripts, styles and images load within your site a base tag is automatically injected <base href="http://www.bbc.co.uk/news" />.

Because of this, you need to ensure all the links within your conent are absolute.

Contributing

Everyone is welcome to contribute to this project. You can contribute just by submitting bugs or suggest improvements by opening an issue on GitHub.

History

This project is a rethought, rebranded, relaunched version of my badly named express-render-template module released in Arpil 2016.

License

Licensed under ISC License © John Doherty

node-iframe-replacement's People

Contributors

john-doherty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-iframe-replacement's Issues

Implementation with next.js?

const express = require('express')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
var iframeReplacement = require('./lib/node-iframe-replacement.js');

app.prepare()
.then(() => {
  const server = express()
  app.use(iframeReplacement);

  server.get('/a', (req, res) => {
    return app.render(req, res, '/b', req.query)
  })

  server.get('/b', (req, res) => {
    return app.render(req, res, '/a', req.query)
  })

  server.get('*', (req, res) => {
    return handle(req, res)
  })

  server.get('/bbc', function(req, res) {
    return handle(res.merge('fake-news', {
          sourceUrl: 'http://www.bbc.co.uk/news',                             // external url to fetch
          sourcePlaceholder: 'div[data-entityid="container-top-stories#1"]'   // css selector to inject our content into
      });
  }));

  server.listen(3000, (err) => {
    if (err) throw err
    console.log('> Ready on http://localhost:3000')
  })
})

A page gets rendered at pages/bbc.js that includes a react component with title BBC.
Any ideas on how to get these two projects integrated?

PHP?

Would something like this be possible with PHP instead of node? I hate relying on iFrames for my apps. :)

How to render a page with in a specified element ?

First of all, this is a very nice node iframe replacement. Is it possible to render the particular web page lets say bbc.com inside any element? For instant,

 <div or iframe src="parsed.html">
       Content for [bbc.com](http://www.bbc.com/http://www.bbc.com/) 
 </iframe>

So that, the exact need of iframe can be replaced ? Please find the attachment for the details.
screen shot 2017-11-04 at 12 14 51 pm

iframes

The other day I was trying to make an iframe for this link for an affiliate program (http://freebitco.in/?r=9171729&tag=wonderland), and I did a test page (http://wonderlandcoin.com/bitcoin/2.php) and the test uses the Example Domain (example.com) and that works, but when I put in the real URL above, the result is a blank page (http://wonderlandcoin.com/bitcoin/) My question is, did they block the ability for anyone to display their site in an iframe? If so, would your thing help me at all?

showing error

C:\Users\ADMIN\myapp\node_modules\node-iframe-replacement\example>node server.js
Server running... Visit http://localhost:8080 in your browser
C:\Users\ADMIN\myapp\node_modules\cheerio\lib\parse.js:55
var oldParent = node.parent || node.root,
^

TypeError: Cannot read property 'parent' of undefined
at Function.exports.update (C:\Users\ADMIN\myapp\node_modules\cheerio\lib\parse.js:55:26)
at module.exports (C:\Users\ADMIN\myapp\node_modules\cheerio\lib\parse.js:17:11)
at Function.exports.load (C:\Users\ADMIN\myapp\node_modules\cheerio\lib\static.js:19:14)
at C:\Users\ADMIN\myapp\node_modules\node-iframe-replacement\lib\node-iframe-replacement.js:37:41
at Immediate. (C:\Users\ADMIN\myapp\node_modules\express-handlebars\lib\utils.js:18:13)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)

Add routes ?

Hello,

Thanks you for this code, it is really nice.

I have a header that I want to hide on all the pages of a website for example "https://www.website.com". I can do it with you code for the homepage.
I would like server.js to reroute the links within the app so it still hides the header on "https://www.website.com/about">
Do you think there is a way to do that ? Do you know how I could hide the header on all the pages ?

Regards,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.