Ad

Is There A Way To Hide The Value Of A 'global' Variable Passed Through Event Listener, While Still Letting The Script Run?

- 1 answer

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

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad