Injecting arbitrary Javascript on all my Rails view

I'm trying to implement a javascript tracker that i need to inject arbitrarily on all of the views rendered by my application, just like the newrelic client instrumentation works.

My app allows user to edit their liquid html templates, so the idea doing this is to inject the script in a way that the user is uncapable to remove it (auto added)

I look the code in the newrelic gem but is too confusing and i wondering if there is a more simple way to do it.

Thanks in advance!

Upvotes: 0

Views: 118

Answers (1)

Viren
Viren

Reputation: 5962

Well I have a solution for this you could write a middleware where you could just check

if the request is for html page or (css or javascript)

if the request is for the html page

append the javascript to the html page before sending the response for the server

here the catch you need very sure what you are doing this I have ran into this problem

  1. Make sure your middleware placement is correct since the development everything is single thread and wrong placement would result in deadlock error

  2. When HTML page is consider what if the request is an ajax request what then you have to be very specific on that regards

Hope This help

Upvotes: 1

Related Questions