Page 1 sur 1

Externaliser deux scripts

Posté : 15 juin 2020, 11:03
par Thatoo
Afin de pouvoir mieux répondre au test ecoindex.fr/ et aux tests de sécurité de Mozilla, https://observatory.mozilla.org/, j'aimerais externaliser les deux inline scripts d'un plugin de PluXml, si possible en les regroupant dans un seul fichier de script.

Actuellement, nous sommes obligé d'écrire

Code : Tout sélectionner

Header set Content-Security-Policy "default-src 'self'; font-src 'self'; img-src https:; script-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com; style-src 'self' https://cdnjs.cloudflare.com; object-src 'none'"
alors qu'il faudrait supprimer 'unsafe-inline' pour renforcer la sécurité d'après les explications ici : https://infosec.mozilla.org/guidelines/ ... ity-policy.

Voici les deux scripts dont je parle.

Code : Tout sélectionner

<script type="text/javascript"> window.addEventListener('load', function(){ window.cookieconsent.initialise({ palette: { popup: { background: '#252e39' }, button: { background: '#14a7d0' } }, theme: 'classic', content: { message: "<?php $this->lang('L_CC_MESSAGE'); ?>", dismiss: "<?php $this->lang('L_CC_DISMISS'); ?>", 'link': "<?php $this->lang('L_CC_LINK'); ?>", href: '<?= $href; ?>' } }) }); </script>
et

Code : Tout sélectionner

<script type="text/javascript"> // share_me plugin (function(theClassName) { 'use strict'; // building some useful constants const ACCOUNTS = { <?php if(count($accounts) > 0) { echo "\t\t".implode(",\n\t\t", $accounts)."\n"; } ?> }; const NETWORKS = { <?php echo "\t\t".implode(",\n\t\t", $networksList)."\n"; ?> }; // callback function in response on click event function popup(event) { if(event.target.tagName == 'IMG') { var network = event.target.getAttribute('data'); if(network != null) { if(network in NETWORKS) { event.preventDefault(); var datas = this.dataset; var nw = NETWORKS[network]; var href = nw.url; var matches = href.match(/#\w+#/g); if(matches != null) { matches.forEach(function(tag) { if(tag == '#account#') { // for some network, it's better to give an account, e.g.: Twitter, Facebook if(network in ACCOUNTS) { href = href.replace(tag, ACCOUNTS[network], href); } } else { var key = tag.substring(1, tag.length-1); var newValue = (key in datas) ? datas[key] : ''; href = href.replace(tag, newValue); } }); href = encodeURI(href); var top = (screen.height - nw.h) / 2, left = (screen.width - nw.w) / 2; var options = 'menubar=no, toolbar=no, resizable=yes, scrollbars=no, width='+nw.w+', height='+nw.h+', top='+top+', left='+left; window.open(href, '', options); } } else { console.log('Unknown social network: ' + network); } } } }; // Add eventListeners at every element with theClassName class const toolbars = document.getElementsByClassName(theClassName); if(toolbars.length > 0) { for(var i=0, iMax=toolbars.length; i<iMax; i++) { toolbars.item(i).addEventListener('click', popup); } } })('social-buttons'); </script>
Est-ce possible de les externaliser avec <script src ="file."></script>? comment? dans un fichier .js? dans un fichier .php?
J'ai essayé dans un fichier .php en enlevant les balises <script> et en ajoutant au début

Code : Tout sélectionner

Header("content-type: application/x-javascript");
mais ça ne fonctionne pas.