Is there a way to hide the value of a 'global' variable passed through Event listener, while still letting the script run?

captiveCorsair :

I am creating a plugin on OctoberCMS, where the user can input a code into a field in the plugin Settings menu. I've saved this input into a global variable, which I've pasted on the template for the pages.

In Plugin.php, the code for saving it into a global variable:

 public function boot()
{
    Event::listen('cms.page.beforeDisplay', function($controller, $url, $page) {

        $settings = Settings::instance();

        $script = $settings->script; 

        $controller->vars['script'] = $script;
    });
}

In the template.htm, calling the variable in the tags:

{{ script | raw }}

which then displays the code that was inputted, when inspecting the page.

My question is, is there a way for the actual value of the variable to be hidden but for the script to still run? So that any users who view the website won't be able to 'inspect' it and see what the script is. Thanks

Hardik Satasiya :

If you want to do like Google Analytics script does then you need to make script file.

your-javascript-file.js now host it anywhere on other domain or on same site and copy its url.

now in your setting $settings->script for this variable, from back-end settings you can set value like.

<script src="https://your_site/something/your-javascript-file.js"></script>

so in your web page it will show something ^ like this but not actual code. user need to dig one step more to see actual code. but yes in your web-page's source code only this single line will appear not js code it self.

if any doubts please comment.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=346881&siteId=1