No9 / harmon

middleware for node-http-proxy to modify the remote website response with trumpet
Other
424 stars 62 forks source link

how to repace inline script or css #28

Open epubreader opened 9 years ago

epubreader commented 9 years ago

I want to replace url in script and css. how to repace inline script or css

No9 commented 9 years ago

Hi @eimhee There is a sample in doge.js on how you can replace a tag that has different content. https://github.com/No9/harmon/blob/master/examples/doge.js#L27

Your request should be quite simple to map to that but let me know if you have other issues.

epubreader commented 9 years ago

thanks, I want to replace js file , not just html.

how to replace url in the application.js?

No9 commented 9 years ago

Hi @eimhee I am not quite following here. The sample shows

<img id="logo" src="/images/logo.svg" alt="node.js">

Being replaced with

<img id="logo" src="http://i.imgur.com/LKShxfc.gif" alt="node.js">

If you want to replace the js returned by the server then you need to intercept the request here https://github.com/No9/harmon/blob/master/examples/doge.js#L57

epubreader commented 9 years ago

Thanks, No9, but I don't want to replace the tag, I want to replace the content in the js file. for example,

https://assets-cdn.github.com/assets/frameworks-06e65f5639cc52d1aaada53115a54614b60fa90ab446a673e3e1818df167663b.js

some content in this file, var i = e.document, o = e.SelectorSet, a = t.event.add, s = t.event.remove, c = {}; if (!o) throw "SelectorSet undefined - https://github.com/josh/jquery-selector-set"; t.event.add = function(e, n, s, u, l) { if (e !== i || n.match(/./) || u || !l) a.call(this, e, n, s, u, l); else for (var f = n.match(/\S+/g), d = f.length; d--;) { var p = f[d], h = t.event.special[p] || {}; p = h.delegateType || p; var m = c[p]; m || (m = c[p] = { handler: r, selectorSet: new o }, m.selectorSet.match

I want to replace https://github.com/josh/jquery-selector-set to https://xxx.com/josh/jquery-selector-set

No9 commented 9 years ago

ok so at the line I mentioned above https://github.com/No9/harmon/blob/master/examples/doge.js#L57 You probably want to do something like

  function (req, res) {
     if (req.url === 'https://github.com/josh/jquery-selector-set ') { 
       http.get('https://xxx.com/josh/jquery-selector-set', function (getres) {
          getres.pipe(res)
       })
     } else {
       proxy.web(req, res);
     }
  }

See https://nodejs.org/api/http.html#http_http_get_options_callback and http://dailyjs.com/2012/11/19/streams-part-2/ as a guide N.B. This code probably won't run but should give you an idea of what needs to be done

dlei commented 8 years ago

do we support modifying JSON response?

No9 commented 8 years ago

We don't right now and my feeling is that it should probably be a separate library as people who want a tag parser may not want a JSON parser and vica versa.