Forum Moderators: phranque

Message Too Old, No Replies

Method to add code to every site without physically adding it

         

csdude55

4:54 am on Dec 19, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I began using Ezoic awhile back, and they push their code onto my pages without me actually doing anything to the code. I changed my nameservers so that they go through Cloudflare, then I connected Ezoic to my Cloudflare, and voila! It works.

Do you know of a way that a smaller company can do something similar? For example, if I wanted every site in my account to set 2 JavaScript variables and then load an external JavaScript file, but I didn't want to add the JS code to the header?

Real world usage, I'd like to offer free website hosting with a single banner ad on the page. But I don't want them to be able to remove the code for the banner.

I'm looking at Amazon Cloudfront and it SOUNDS like it might do what I need, but the docs are pretty vague.

[aws.amazon.com...]

phranque

9:14 am on Dec 19, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



in an Apache environment you can do this using an output filter such as mod_substitute, or if you need to do something more complicated you could use mod_ext_filter.

csdude55

6:28 pm on Dec 19, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're thinking something like this?

<Location "/">
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|(<body.*?>)|<script>var foo = 1, bar = 14;</script><script src="//example.com/lorem.js"></script>\n$1|iq"
</Location>


That's pretty awesome and I can definitely work with that! :-) Thanks, @phranque!

phranque

2:17 am on Dec 20, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



don't you want to reinsert your body tag in the substitution string prior to the script element?

csdude55

4:54 am on Dec 20, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Probably, yes. But I thought that was specific to the tag I was inserting! LOL How did you guess that?