Bestaat er een goedgekeurde methode om Javascript toe te voegen voor de body sluit?

AI-generated summary

Jim_Starkweather asks about adding JavaScript before the </body> tag in Discourse, noting that his anti-adblock script isn’t working despite being correctly placed in the theme’s footer. The script is being loaded but fails due to a Content Security Policy (CSP) error: Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source. pmusaraj explains that while unsafe-eval can be whitelisted in the CSP settings to allow eval, this is not recommended due to security risks. rolando and Jim_Starkweather confirm the script is properly placed but not executing, highlighting the conflict between functionality and security.

I have researched this a bit and am familiar with the security reasoning, etc. But is has their been any method created for doing this yet? I have an anti-ad block script I would like to run and it’s suppose to be inserted prior to </body> and adding it to the theme yields nada. I assume the security routines are removing it? Either way it’s not loading for me in the </body> or “Footer” options in the theme customization area.

Fyi this is just pure js and not a remote link to a file, etc.

Thanks,
Jim

have you placed your js inside script tags in your < /body>?:

<script type="text/javascript">

 <<< JS >>

</script>

Yes I have already put the script in on the Desktop version of </body>.

<script type="text/javascript"  charset="utf-8">
eval(function....
</script>

Hmm… well I see that a file WITH the script is being put in the footer via:

<script src="[/theme-javascripts/854aca489ba48a598e9eefaa2e4e255ac2a445b7.js?__ws=mywebsite.com](https://mywebsite.com/theme-javascripts/854aca489ba48a598e9eefaa2e4e255ac2a445b7.js?__ws=mywebsite.com)"></script>

Going to that file (for me) displays the same JS I put in the customize themes area.

But as far as I can tell it’s not working as it normally does on other sites (being I have ad blocker activated).

Okay… so there is this exception/error in the JS console:

Uncaught EvalError: Refused to evaluate a string as JavaScript because ‘unsafe-eval’ is not an allowed source of script in the following Content Security Policy directive: "script-src…

So it there a way to make “eval” safe or authorize this particular usage?

You can whitelist evals in the content security policy site setting, see Mitigate XSS Attacks with Content Security Policy.

We do not recommend this though, it opens your site to security exploits.